안드로이드 개발 질문/답변
(글 수 45,052)
package not.sy;
import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List;
import net.htmlparser.jericho.Element; import net.htmlparser.jericho.HTMLElementName; import net.htmlparser.jericho.MasonTagTypes; import net.htmlparser.jericho.MicrosoftTagTypes; import net.htmlparser.jericho.PHPTagTypes; import net.htmlparser.jericho.Source;
class HtmlParser { ArrayList<NoticeList> arNotice; HtmlParser(){ } public ArrayList getList(String sourceUrlString) { int index, indexEnd, hIndex, hIndexEnd; String str = null, strHref = null; //String sourceUrlString = "http://www.syu.ac.kr/aboard/board.asp?bidx=314&mode=list&page=";
MicrosoftTagTypes.register(); PHPTagTypes.register(); PHPTagTypes.PHP_SHORT.deregister(); // remove PHP short tags for this // example otherwise they override // processing instructions MasonTagTypes.register(); try{ Source source = new Source(new URL(sourceUrlString));
// Call fullSequentialParse manually as most of the source will be // parsed. source.fullSequentialParse(); List rootList = source.getAllElements(HTMLElementName.A); for (Iterator it = rootList.iterator(); it.hasNext();) { Element linkElement = (Element) it.next(); strHref += linkElement.getAttributeValue("href") + '\n'; if (strHref == null) continue; } index = strHref.indexOf("board"); indexEnd = strHref.indexOf('\n', index); String[] href = new String[50]; for(int i=0; i<50; i++){ href[i] = new String(); if(i==0) href[i] = "http://www.xxx.ac.kr/aboard/" + strHref.substring(index, indexEnd); else{ index = indexEnd+1; indexEnd = strHref.indexOf('\n', index); href[i] = "http://www.xxx.ac.kr/aboard/" + strHref.substring(index, indexEnd); } } str = source.getRenderer().setIncludeHyperlinkURLs(false).toString(); arNotice = new ArrayList<NoticeList>(); String num, title, hit, date; index = str.indexOf("공지"); indexEnd = index+1; index = str.indexOf("공지", indexEnd); NoticeList nt; for (int i = 0; i < 50; i++) { nt = new NoticeList(); if(i == 0){ indexEnd = str.indexOf('\n', index); nt.num = str.substring(index, indexEnd-1); } else{ index = indexEnd+3; indexEnd = str.indexOf('\n', index); nt.num = str.substring(index, indexEnd-1); } index = indexEnd+3; indexEnd = str.indexOf('\n', index); nt.title = str.substring(index, indexEnd-1); index = indexEnd+3; indexEnd = str.indexOf('\n', index); nt.hit = str.substring(index, indexEnd-1); index = indexEnd+3; indexEnd = str.indexOf('\n', index); nt.date = str.substring(index, indexEnd-1); nt.link = href[i].toString(); arNotice.add(nt); } }catch(MalformedURLException e){ e.getStackTrace(); }catch(IOException e){ e.getStackTrace(); } return arNotice; } public String getContent(String sourceUrlString, String date){ int index, indexEnd, hIndex, hIndexEnd; String str = null, result = "";
MicrosoftTagTypes.register(); PHPTagTypes.register(); PHPTagTypes.PHP_SHORT.deregister(); // remove PHP short tags for this // example otherwise they override // processing instructions MasonTagTypes.register(); try{ Source source = new Source(new URL(sourceUrlString));
source.fullSequentialParse(); str = source.getRenderer().setIncludeHyperlinkURLs(false).toString().replace("\r",""); index = str.indexOf(date); index += 10; result += str.substring(index); }catch(MalformedURLException e){ e.getStackTrace(); }catch(IOException e){ e.getStackTrace(); } return result; } public String getContentEuc(String sourceUrlString){ int index, indexEnd, hIndex, hIndexEnd; String str = null, result = "";
MicrosoftTagTypes.register(); PHPTagTypes.register(); PHPTagTypes.PHP_SHORT.deregister(); // remove PHP short tags for this // example otherwise they override // processing instructions MasonTagTypes.register(); try{ URL sUrl= new URL(sourceUrlString); InputStream is = sUrl.openStream(); Source source=new Source(new InputStreamReader(is,"euc-kr"));
source.fullSequentialParse(); result = source.getRenderer().setIncludeHyperlinkURLs(false).toString(); }catch(MalformedURLException e){ e.getStackTrace(); }catch(IOException e){ e.getStackTrace(); } return result; } }
class NoticeList { String num, title, hit, date, link; }
package not.sy;
import java.util.ArrayList;
import com.sy.*;
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView;
public class Notice extends Activity { ArrayList<NoticeItem> arItem; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.listtest); TextView topText = (TextView) this.findViewById(R.id.toptext); topText.setText("공지사항"); HtmlParser parser = new HtmlParser(); NoticeList nt = new NoticeList(); String str = null; ArrayList<NoticeList> arNotice = parser.getList("http://xxxxx"); arItem = new ArrayList<NoticeItem>(); NoticeItem mi;
for(int i=0; i<arNotice.size(); i++){ nt = arNotice.get(i); mi = new NoticeItem(nt.num, nt.title, nt.date, nt.hit, nt.link); arItem.add(mi); }
NoticeAdapter MyAdapter = new NoticeAdapter(this, R.layout.main_notice, arItem);
ListView MyList; MyList = (ListView) findViewById(R.id.list); MyList.setAdapter(MyAdapter);
MyList.setOnItemClickListener(new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub TabGroupActivity parentActivity = (TabGroupActivity)getParent(); Intent intent = new Intent(getBaseContext(), not.sy.NoticeView.class); intent.putExtra("Url", arItem.get(position).Link); intent.putExtra("Date", arItem.get(position).Date); parentActivity.startChildActivity("NoticeView", intent); } }); } }
class NoticeItem { NoticeItem(String aNum, String aName, String aDate, String aHit, String aLink) { Num = aNum; Name = aName; Date = aDate; Hit = aHit; Link = aLink; }
String Num; String Name; String Date; String Hit; String Link; }
class NoticeAdapter extends BaseAdapter {
Context maincon; LayoutInflater Inflater; ArrayList<NoticeItem> arSrc; int layout;
public NoticeAdapter(Context context, int alayout, ArrayList<NoticeItem> aarSrc) { maincon = context; Inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); arSrc = aarSrc; layout = alayout; }
@Override public int getCount() { return arSrc.size(); }
@Override public Object getItem(int position) { // TODO Auto-generated method stub return arSrc.get(position).Name; }
@Override public long getItemId(int position) { // TODO Auto-generated method stub return position; }
@Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final int pos = position; if (convertView == null) { convertView = Inflater.inflate(layout, parent, false); }
TextView num = (TextView) convertView.findViewById(R.id.notice_num); num.setText(arSrc.get(position).Num);
TextView title = (TextView) convertView.findViewById(R.id.notice_title); title.setText(arSrc.get(position).Name); TextView date = (TextView) convertView.findViewById(R.id.notice_date); date.setText("등록일 : " + arSrc.get(position).Date); TextView hit = (TextView) convertView.findViewById(R.id.notice_hit); hit.setText("조회수 : " + arSrc.get(position).Hit);
return convertView; }
}
class MyView extends View{
public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } public void init(){ TextView tv = new TextView(getContext()); tv.setText("asdfsadfsdxcvxzcvasdfqewrsdf"); } }
package not.sy;
import com.sy.*;
import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; import net.htmlparser.*;
public class NoticeView extends Activity { String url, date, str; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.notice_view); HtmlParser parser = new HtmlParser(); Intent intent = getIntent(); url = intent.getStringExtra("Url"); // TextView tv = (TextView)findViewById(R.id.notice_view_text); // tv.setText(url); date = intent.getStringExtra("Date"); if(url.indexOf("공지") != -1){ int index, indexEnd; index = url.indexOf("공지"); url = url.substring(0, index) + "%B0%F8%C1%F6"; } str = parser.getContent(url, date); //str.trim(); TextView tv = (TextView)findViewById(R.id.notice_view_text); tv.setText(str); } private ProgressDialog loadingDialog; void createThreadAndDialog(){ loadingDialog = ProgressDialog.show(this, "로딩중...", "페이지 로딩중입니다", true, false); Thread thread = new Thread(new Runnable(){ public void run(){ handler.sendEmptyMessage(0); } }); thread.start(); } private Handler handler = new Handler(){ public void handleMessage(Message msg){ loadingDialog.dismiss(); HtmlParser parser = new HtmlParser(); Intent intent = getIntent(); url = intent.getStringExtra("Url"); TextView tv = (TextView)findViewById(R.id.notice_view_text); tv.setText(url); url = url.substring(0, url.length()-1); url = "http://www.xxxx"; date = intent.getStringExtra("Date"); if(url.indexOf("공지") != -1){ int index, indexEnd; index = url.indexOf("공지"); url = url.substring(0, index) + "%B0%F8%C1%F6"; } str = parser.getContent(url, date); str.trim(); TextView tv1 = (TextView)findViewById(R.id.notice_view_text); tv1.setText(str); } }; }
오류좀 봐주세요.ㅠㅠ
이건 로그입니다.
02-07 06:15:58.394: DEBUG/dalvikvm(429): GC_EXPLICIT freed 1288 objects / 69200 bytes in 207ms 02-07 06:16:05.163: INFO/ActivityManager(362): Starting activity: Intent { cmp=com.sy/.MenuTab } 02-07 06:16:06.374: INFO/ActivityManager(362): Displayed activity com.sy/.MenuTab: 1056 ms (total 1056 ms) 02-07 06:16:07.623: INFO/ActivityManager(362): Starting activity: Intent { cmp=com.sy/not.sy.Notice } 02-07 06:16:07.623: WARN/ActivityManager(362): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=com.sy/not.sy.Notice } 02-07 06:16:07.804: DEBUG/AndroidRuntime(687): Shutting down VM 02-07 06:16:07.804: WARN/dalvikvm(687): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): FATAL EXCEPTION: main 02-07 06:16:07.823: ERROR/AndroidRuntime(687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sy/not.sy.Notice}: java.lang.NullPointerException 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.os.Handler.dispatchMessage(Handler.java:99) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.os.Looper.loop(Looper.java:123) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at java.lang.reflect.Method.invokeNative(Native Method) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at java.lang.reflect.Method.invoke(Method.java:521) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at dalvik.system.NativeStart.main(Native Method) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): Caused by: java.lang.NullPointerException 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at not.sy.Notice.onCreate(Notice.java:40) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 02-07 06:16:07.823: ERROR/AndroidRuntime(687): ... 11 more 02-07 06:16:07.854: WARN/ActivityManager(362): Force finishing activity com.sy/not.sy.Notice 02-07 06:16:07.863: WARN/ActivityManager(362): Force finishing activity com.sy/.MenuTab 02-07 06:16:08.364: WARN/ActivityManager(362): Activity pause timeout for HistoryRecord{43fde248 com.sy/not.sy.Notice} 02-07 06:16:09.823: INFO/Process(687): Sending signal. PID: 687 SIG: 9 02-07 06:16:09.866: INFO/ActivityManager(362): Process com.sy (pid 687) has died. 02-07 06:16:09.895: INFO/WindowManager(362): WIN DEATH: Window{43f74dd8 com.sy/com.sy.Intro paused=false} 02-07 06:16:09.904: INFO/WindowManager(362): WIN DEATH: Window{44019490 com.sy/com.sy.MenuTab paused=true} 02-07 06:16:10.023: INFO/UsageStats(362): Unexpected resume of com.android.launcher while already resumed in com.sy 02-07 06:16:10.195: WARN/InputManagerService(362): Got RemoteException sending setActive(false) notification to pid 687 uid 10049