将屏幕信息填充到列表中

时间:2011-11-09 00:08:55

标签: android eclipse listview screen-scraping listadapter

我想将网站上的屏幕抓取信息填充到自定义列表中,我已使用此网站的jsoup解析数据:

www.soccerway.com

但我无法启动我的应用程序,这会导致运行时异常,这里是代码和错误跟踪:

public class ScoresActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scoresactivity);

    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.textViewhometeam,
               getResources().getStringArray(R.array.pls)));

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                  // When clicked, show a toast with the TextView text
                    //int pos = lv.getSelectedItemPosition();
                  //if(pos == 0){
                      //Intent intent = new Intent(Legends.this, l1.class);
                      //startActivity(intent);
                  //}

         }
    });

}

public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, textViewResourceId, strings);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_item3, parent, false);

        //String[] items = getResources().getStringArray(R.array.legends);

        TextView tv1 = (TextView) row.findViewById(R.id.textViewhometeam);
        TextView tv2 = (TextView) row.findViewById(R.id.textViewfinalscore);
        TextView tv3 = (TextView) row.findViewById(R.id.textViewawayteam);

        Document doc;
        try {
            doc = (Document) Jsoup.connect("http://www.soccerway.com/teams/saudi-arabia/al-nasr-riyadh/matches/").get();
            Elements team_a = ((Element) doc).select("td.team-a");
            for (Element e : team_a) {
                CharSequence ta = e.text();
                tv1.setText(ta);
                //System.out.println("team a is: " + ta);
            }

            Elements team_b = ((Element) doc).select("td.team-b");
            for (Element e : team_b) {
                CharSequence tb = e.text();
                tv3.setText(tb);
                //System.out.println("team b is: " + tb);
            }

            Elements result = ((Element) doc).select("td.score");
            for (Element e : result) {
                CharSequence re = e.text();
                tv2.setText(re);
                //System.out.println("score is: " + re);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }




        return row;

    }


}

}

scoresactivity.xml只是一个列表视图 list_item3.xml是我的自定义列表,它只包含主队,客场球队和得分的3个文本视图。

这是错误:

11-09 02:33:24.379: ERROR/AndroidRuntime(325): FATAL EXCEPTION: main
11-09 02:33:24.379: ERROR/AndroidRuntime(325): java.lang.RuntimeException: Unable to      start activity ComponentInfo{com.nassr.alnassrfc/com.nassr.alnassrfc.Main}:   java.lang.RuntimeException: Your content must have a TabHost whose id attribute is   'android.R.id.tabhost'
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.os.Looper.loop(Looper.java:123)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at java.lang.reflect.Method.invoke(Method.java:521)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at dalvik.system.NativeStart.main(Native Method)
11-09 02:33:24.379: ERROR/AndroidRuntime(325): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.TabActivity.onContentChanged(TabActivity.java:105)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.Activity.setContentView(Activity.java:1647)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at com.nassr.alnassrfc.Main.onCreate(Main.java:19)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-09 02:33:24.379: ERROR/AndroidRuntime(325):     ... 11 more
11-09 02:33:24.399: WARN/ActivityManager(59):   Force finishing activity com.nassr.alnassrfc/.Main
11-09 02:33:24.899: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{45073020 com.nassr.alnassrfc/.Main}
11-09 02:33:26.921: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@450773b8

这是我的Main类扩展TabActivity并使用TabHost

public class Main extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custome_title);

    Resources res = getResources();
    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    TabHost.TabSpec spec;  
    Intent intent;  // Reusable Intent for each tab
    tabHost.getTabWidget();


    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ClubActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("club").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_alnassr))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, ScoresActivity.class);
    spec = tabHost.newTabSpec("scores").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_alnassr1))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TableActivity.class);
    spec = tabHost.newTabSpec("table").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_alnassr2))
                  .setContent(intent);
    tabHost.addTab(spec);

 // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, MessageList.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("News").setIndicator("",
                      res.getDrawable(android.R.drawable.star_on))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, YoutubeActivity.class);
    spec = tabHost.newTabSpec("youtube").setIndicator("",
                      res.getDrawable(R.drawable.ic_tab_alnassr3))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

} 谢谢你们。

1 个答案:

答案 0 :(得分:1)

从日志中可以清楚地看出,你的活动com.nassr.alnassrfc.Main无法加载,因为它期望android.R.id.tabhost用于tabhost。我会检查你的com.nassr.alnassrfc.Main的布局和活动。该活动是否扩展了tabhost,如果它为什么布局中没有tabhost?

我认为您发布的代码与错误无关。