07-28 11:36:48.010: W/dalvikvm(31346): threadid=1: thread exiting with uncaught exception (group=0x41daf960)
07-28 11:36:48.020: E/AndroidRuntime(31346): FATAL EXCEPTION: main
07-28 11:36:48.020: E/AndroidRuntime(31346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.NullPointerException
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.ActivityThread.access$700(ActivityThread.java:157)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.os.Looper.loop(Looper.java:176)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.ActivityThread.main(ActivityThread.java:5317)
07-28 11:36:48.020: E/AndroidRuntime(31346): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 11:36:48.020: E/AndroidRuntime(31346): at java.lang.reflect.Method.invoke(Method.java:511)
07-28 11:36:48.020: E/AndroidRuntime(31346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-28 11:36:48.020: E/AndroidRuntime(31346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-28 11:36:48.020: E/AndroidRuntime(31346): at dalvik.system.NativeStart.main(Native Method)
07-28 11:36:48.020: E/AndroidRuntime(31346): Caused by: java.lang.NullPointerException
07-28 11:36:48.020: E/AndroidRuntime(31346): at com.example.MainActivity.onCreate(MainActivity.java:18)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.Activity.performCreate(Activity.java:5326)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
07-28 11:36:48.020: E/AndroidRuntime(31346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
07-28 11:36:48.020: E/AndroidRuntime(31346): ... 11 more
07-28 11:36:49.612: I/Process(31346): Sending signal. PID: 31346 SIG: 9
package com.example;我該如何解決NullPointerException?
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final EditText txt =(EditText) findViewById(R.id.editText1);
Button btn =(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Intent m = new Intent(MainActivity.this,Main2.class);
m.putExtra("a",txt.getText().toString());
}
});
}
}
E/AndroidRuntime(31346):顯示java.lang.NullPointerException 11月7日至28日:由造成36:48.020: 此行是你的實際的錯誤和在它旁邊發生。 –
在這一行中,您將得到異常'com.example.MainActivity.onCreate(MainActivity.java:18)':引起:java.lang.NullPointerException –
錯誤是因爲您指的是控件而沒有將視圖設置爲活動。使用'setcontentview()'方法指向你的xml佈局,你的控件實際上是 –