在学习android时,我正在制作一个ICMP ping程序。我添加了一个 AsyncTask 线程来防止“主网络中的网络”,现在我的编程正在抛出这个运行时异常(标题中的那个)。我是安卓新手;如果你们能帮助我,那就太好了。这里有问题的 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 方法来自另一个类,它返回一个字符串对象并接受一个字符串参数。它使用 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);
}
}
}