Issue
Hi I'm pretty new to android dev however I have searched these forums and found nothing conclusive. I am trying to display HTML from a site in a text view using ASyncTask. Here is the code I'm using below.
package com.uad1001.theredux;
//imports
public class Htmlfromurl extends Activity {
Button btnSend;
EditText PhoneNo;
TextView t = new TextView(this);
String html;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_htmlfromurl);
t =(TextView)findViewById(R.id.IPDisplay);
// will implement later
//PhoneNo = (EditText) findViewById(R.id.phone);
//btnSend = (Button) findViewById(R.id.send);
String IPurl = "http://myexternalip.com/raw"; //URL I want to scrape
new event().execute(IPurl);
}
public class event extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String...url) {
// TODO Auto-generated method stub
String urls = url[0];
try
{
Thread.sleep(4000);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
return page;
}
catch (ClientProtocolException e)
{
e.printStackTrace();
return "Error";
}
catch (IOException e)
{
e.printStackTrace();
return "Error";
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "Error";
}
}
@Override
protected void onPostExecute(String page){
t.setText(page);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.htmlfromurl, menu);
return true;
}
}
The program crashes instantly. The logcat shows: (also at http://pastebin.com/3sTENx2q)
08-05 19:38:54.082: W/dalvikvm(11472): threadid=1: thread exiting with uncaught exception (group=0x410942a0)
08-05 19:38:54.092: E/AndroidRuntime(11472): FATAL EXCEPTION: main
08-05 19:38:54.092: E/AndroidRuntime(11472): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.uad1001.theredux/com.uad1001.theredux.Htmlfromurl}: java.lang.NullPointerException
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2016)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.access$700(ActivityThread.java:134)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.os.Looper.loop(Looper.java:137)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.main(ActivityThread.java:4867)
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 19:38:54.092: E/AndroidRuntime(11472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
08-05 19:38:54.092: E/AndroidRuntime(11472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
08-05 19:38:54.092: E/AndroidRuntime(11472): at dalvik.system.NativeStart.main(Native Method)
08-05 19:38:54.092: E/AndroidRuntime(11472): Caused by: java.lang.NullPointerException
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.view.View.<init>(View.java:3296)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.view.View.<init>(View.java:3364)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.widget.TextView.<init>(TextView.java:584)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.widget.TextView.<init>(TextView.java:579)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.widget.TextView.<init>(TextView.java:575)
08-05 19:38:54.092: E/AndroidRuntime(11472): at com.uad1001.theredux.Htmlfromurl.<init>(Htmlfromurl.java:23)
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.Class.newInstanceImpl(Native Method)
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.Class.newInstance(Class.java:1319)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2007)
08-05 19:38:54.092: E/AndroidRuntime(11472): ... 11 more
Any help would be appreciated.
Solution
I believe your error is being caused by the TextView t = new TextView(this);
. I don't think you want to be instantiating it like that. Instead just leave it as TextView t;
in your instance variable and instantiate it by finding it by ID in your onCreate
method as you are doing. That should solve the problem. The TextView
constructor is supposed to take a Context, which is more an abstract object, so instantiating it using the constructor is generally best to not do.
Answered By - mattpic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.