I have just started Android Development, I want to make thread in my application which reads XML document and returns a view which is to be displayed by createTabContent of tabHost.
I tried it but getting exception
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;
}
}
}
Well, you haven’t posted your code yet, but I am still going to take a stab at this, since you said you are new at developing apps. Are you using the
Threadclass? InAndroid, you need to use AsyncTask instead if you want to update the UI from your Thread process. Be sure to read up on developer.android.com, too. Hope this helps.