在学习android我正在制作ICMP ping程序。我添加了一个AsyncTask线程来防止“在main中联网”,现在我的编程抛出了这个运行时异常(标题中的那个)。我是android的新手;如果你们能帮助我,那真的很棒。这里有问题的logcat错误:
05-30 13:52:49.644: E/AndroidRuntime(620): FATAL EXCEPTION: main
05-30 13:52:49.644: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.icmpping.Joe/com.icmpping.Joe.PingAppActivity}: java.lang.NullPointerException
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.os.Looper.loop(Looper.java:137)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-30 13:52:49.644: E/AndroidRuntime(620): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 13:52:49.644: E/AndroidRuntime(620): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 13:52:49.644: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 13:52:49.644: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 13:52:49.644: E/AndroidRuntime(620): at dalvik.system.NativeStart.main(Native Method)
05-30 13:52:49.644: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.Activity.findViewById(Activity.java:1794)
05-30 13:52:49.644: E/AndroidRuntime(620): at com.icmpping.Joe.PingAppActivity.<init>(PingAppActivity.java:15)
05-30 13:52:49.644: E/AndroidRuntime(620): at java.lang.Class.newInstanceImpl(Native Method)
05-30 13:52:49.644: E/AndroidRuntime(620): at java.lang.Class.newInstance(Class.java:1319)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-30 13:52:49.644: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
05-30 13:52:49.644: E/AndroidRuntime(620): ... 11 more
我很确定这与AsyncTask有关。 ICMP_Ping.ping方法来自另一个类,它返回一个String对象并获取一个String参数。它使用java.net.InetAddress和System.currentTimeMillis()。无论如何,这是我的主要课程:
////
package com.icmpping.Joe;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class PingAppActivity extends Activity {
private TextView console = (TextView) findViewById(R.id.TEXT_STATUS_ID);
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
Log.d("pingactivity", "Hey LOGCAT2");
EditText urlbox = (EditText) findViewById(R.id.url_edittext);
String url = urlbox.getText().toString();
Log.d("pingactivity", "Hey LOGCAT3");
new AsyncPing().execute(url);
Log.d("pingactivity", "Hey LOGCAT4");
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("pingactivity", "Hey LOGCAT0");
Button pingbutton = (Button) findViewById(R.id.ping_button);
pingbutton.setOnClickListener(listener);
Log.d("pingactivity", "Hey LOGCAT1");
}
private class AsyncPing extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... url) {
return ICMP_Ping.ping(url[0]);
}
//protected void onProgressUpdate(Integer... progress) {}
protected void onPostExecute(String pingresult) {
console.append(pingresult);
}
}
}
答案 0 :(得分:3)
这一行TextView console = (TextView) findViewById(R.id.yourtextid);
放在setContentView(R.layout.main);
TextView console;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("pingactivity", "Hey LOGCAT0");
Button pingbutton = (Button) findViewById(R.id.ping_button);
console = (TextView) findViewById(R.id.yourtextid);
}