0
在學習android我做了一個ICMP ping程序。我添加了一個AsyncTask線程來阻止「主要網絡」,現在我的編程正在拋出這個運行時異常(標題中的那個)。我是新來的android;如果你們能幫助我,那真是太棒了。這裏有問題的logcat錯誤:無法實例化活動ComponentInfo {...}:java.lang.NullPointerException
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);
}
}
}
謝謝你的作品!儘管我有一個基本的Java問題。 TextView控制檯如何處於類體中並且沒有可見性修飾符。它不應該是私人的TextView控制檯;? – J50
只有在聲明的類中有'private'字段才能訪問...其他類不能訪問它 –