簡體   English   中英

Android線程Nullpointer異常

[英]Android thread Nullpointer exception

我剛剛開始Android開發,我想在我的應用程序中創建線程,該線程讀取XML文檔並返回一個視圖,該視圖將由tabHostcreateTabContent顯示。

我嘗試了但是遇到異常

 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;
    }


}

}

好的,您尚未發布代碼,但是由於您說您是開發應用程序的新手,因此我將繼續對此進行討論。 您正在使用Thread類嗎? Android ,如果要從Thread進程更新UI,則需要使用AsyncTask 請務必也閱讀developer.android.com。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM