2015-07-28 146 views
-1
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()); 
      } 
     }); 

    } 

} 
+1

E/AndroidRuntime(31346):顯示java.lang.NullPointerException 11月7日至28日:由造成36:48.020: 此行是你的實際的錯誤和在它旁邊發生。 –

+1

在這一行中,您將得到異常'com.example.MainActivity.onCreate(MainActivity.java:18)':引起:java.lang.NullPointerException –

+0

錯誤是因爲您指的是控件而沒有將視圖設置爲活動。使用'setcontentview()'方法指向你的xml佈局,你的控件實際上是 –

回答

2

您錯過了設置您的內容視圖。

super.onCreate(savedInstanceState);

setContentView(R.layout.your_layout_name); 
+0

非常感謝。我這樣做,但還沒有工作。 –

+0

當我按下按鈕「不幸已停止」發生。我必須做什麼? –

+0

'公共類MAIN2延伸活動{ @覆蓋 保護無效的onCreate(捆綁savedInstanceState){ \t // TODO自動生成方法存根 \t super.onCreate(savedInstanceState); \t setContentView(R.layout.main2); \t EditText a =(EditText)findViewById(R.id.textView1); ()。getText()。getExtras()。getString(「a」)); \t \t \t } }' –

0

你沒有設置你的活動佈局引用添加下面的線。這就是爲什麼您需要在onCreate方法中的super.onCreate(savedInstanceState);之後添加setContentView(R.layout.yourXMLFile);。希望它能幫助

1

只需設置setContentView(R.layout.xxxxxx);

@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.xxxxxx); 
     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(getApplicationContext(),Main2.class); 
       m.putExtra("a",txt.getText().toString()); 

       startActivity(intent); 
      } 
     }); 

    } 

} 
相關問題