Android线程Nullpointer异常

时间:2011-12-08 05:13:35

标签: android

我刚刚开始Android开发,我想在我的应用程序中创建一个读取XML文档的线程,并返回一个由 tabHost createTabContent 显示的视图。

我试了但是得到了异常

12-08 10:33:52.684: E/AndroidRuntime(422): FATAL EXCEPTION: main
12-08 10:33:52.684: E/AndroidRuntime(422): java.lang.NullPointerException
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:622)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.widget.TabHost.setCurrentTab(TabHost.java:323)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.view.View.performClick(View.java:2408)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.view.View$PerformClick.run(View.java:8816)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.os.Handler.handleCallback(Handler.java:587)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.os.Looper.loop(Looper.java:123)
12-08 10:33:52.684: E/AndroidRuntime(422):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-08 10:33:52.684: E/AndroidRuntime(422):    at java.lang.reflect.Method.invokeNative(Native Method)
12-08 10:33:52.684: E/AndroidRuntime(422):    at java.lang.reflect.Method.invoke(Method.java:521)
12-08 10:33:52.684: E/AndroidRuntime(422):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-08 10:33:52.684: E/AndroidRuntime(422):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-08 10:33:52.684: E/AndroidRuntime(422):    at dalvik.system.NativeStart.main(Native Method)
package com.mahesh;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TextView;

public class CricTask extends TabActivity implements TabHost.TabContentFactory{
    TabHost tabHost;
    Context context;
     ScrollView scroll;
     LinearLayout layout;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
         tabHost = getTabHost();
        // MaheshActivity main=new MaheshActivity();
         //scroll = new ScrollView(this);
      context=getApplicationContext();
       //layout=new LinearLayout(context);
        tabHost.addTab(tabHost.newTabSpec("Current")
                .setIndicator("Current")
                .setContent(new Intent(this, List1.class)));


        tabHost.addTab(tabHost.newTabSpec("Result")
                .setIndicator("Result").setContent(this));
                //.setContent(new Intent(this, MaheshActivity.class).putExtra("link", "http://www.espncricinfo.com/rss/livescores.xml")));

        tabHost.addTab(tabHost.newTabSpec("News")
                .setIndicator("News")
                .setContent(new Intent(this,MaheshActivity.class)
                        .putExtra("link", "http://www.espncricinfo.com/rss/content/story/feeds/0.xml")));




    }



@Override
public View createTabContent(String tag) {


    if(ta..equals("Current")){
      new Thread(new Runnable() { 
          public void run(){

              XmlHandler handle = null;

                TextView name[];
                TextView website[];

                URL web;

                //setContentView(R.layout.layout);

                 scroll = new ScrollView(context);
                 layout=new LinearLayout(context);
                layout.setOrientation(1);
                //ScrollView scroll = new ScrollView(this);

                try {

                    web = new URL("http://www.espncricinfo.com/rss/livescores.xml");

                        SAXParserFactory factory = SAXParserFactory.newInstance();
                        SAXParser sax = factory.newSAXParser();
                        XMLReader xmlread = sax.getXMLReader();
                        handle = new XmlHandler();
                        xmlread.setContentHandler(handle);
                        xmlread.parse(new InputSource(web.openStream()));

                    }           
                    catch(NullPointerException n){
                                n.getMessage();
                                }

                catch (Exception e) {

                  e.getMessage();

                }
                SitesList sitesList = handle.sitesList;

                name = new TextView[sitesList.getName().size()];
                website = new TextView[sitesList.getName().size()];

                for (int i = 1; i < sitesList.getName().size(); i++) {

                    name[i] = new TextView(context);
                    name[i].setText(sitesList.getName().get(i));
                    name[i].setTextSize(20);
                    name[i].setTextColor(Color.GREEN);
                    website[i] = new TextView(context);

                    String str = "\"" + sitesList.getWebsite().get(i) + "\"";
                    //String link = sitesList.getWebsite().get(i);
                    String html = "<a" + " " + "href" + "=" + "\\" + str + ">"
                            + str + "</a>";

                    website[i].setText(Html.fromHtml(html));
                    website[i].setAutoLinkMask(BIND_AUTO_CREATE);
                    website[i].setTextColor(Color.YELLOW);
                    website[i].setMovementMethod(LinkMovementMethod.getInstance());

                    TextView tv = new TextView(context);
                    tv.setTextColor(Color.CYAN);
                    layout.addView(tv);
                    layout.addView(name[i]);
                    layout.addView(website[i]);

                }
                scroll.addView(layout);
               }


          }).start();

        return scroll;
    }


}

}

1 个答案:

答案 0 :(得分:0)

好吧,你还没有发布你的代码,但是我仍然会考虑这个问题,因为你说你是开发应用程序的新手。你在使用Thread班吗?在Android中,如果要从线程进程更新UI,则需要使用AsyncTask。一定要阅读developer.android.com。希望这会有所帮助。