我写了一个函数来读取URL上的文本文件。当我在命令行中使用java(而不是android)编译和运行它时,它工作。但是当我将该函数移植到Android studio中的AsyncTask时。它只是崩溃了。 这是代码。你能告诉我发生了什么:(。非常感谢。
private class getjstr extends AsyncTask<String,Void,String> {
@Override
protected void onPreExecute(){
MainActivity.this.tv1.setText("STart"); //This is a TextView
}
@Override
protected String doInBackground(String... URLArr){
String result="";
String textURL=URLArr[0];
try{
URL URLObj = new URL(textURL);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(URLObj.openStream()));
String tmpStr;
while((tmpStr=bufReader.readLine())!=null){
result=result + tmpStr;
}
bufReader.close();
}
catch (Exception e){
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result){
MainActivity.this.JString = result;
}
}
logcat的
05-23 17:12:20.591 32581-32581/com.momo.tobedelete E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.momo.tobedelete, PID: 32581
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.momo.tobedelete/com.momo.tobedelete.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at com.momo.tobedelete.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6575)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
05-23 17:24:00.786 2033-2033/com.momo.tobedelete E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.momo.tobedelete, PID: 2033
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.momo.tobedelete/com.momo.tobedelete.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at com.momo.tobedelete.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6575)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
MainActivity中的 onCreate
:
public class MainActivity extends AppCompatActivity {
List<Country> lc;
String JString;
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv100);
String jurl = "https://dl.dropboxusercontent.com/u/98107627/feature_list_json";
new getjstr().execute(jurl);
this.tv1.setText(this.JString);
}
/*AsyncTask goes here*/
}
据我所知,似乎AsyncTask未能为 this.JString
分配新值答案 0 :(得分:2)
this.tv1.setText(this.JString);
此行导致错误。从onCreate()中删除它。
在postExecute中设置文本而不是在onCreate();
中答案 1 :(得分:0)
据我了解您的代码,您正在尝试根据AsyncTask的结果更新文本视图,因此您 AsyncTask 类定义应该类似于:
new AsyncTask<String ,Void, String >()
其中result参数是String;然后在你的 onPostExecute 方法中,你会有类似的东西:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
您可以更新您的观点!
请注意,您应该在 doInBackground()方法中返回要放在textview中的字符串!