1

我一直在關注Android的教程和創建MyFirstApp(見http://developer.android.com/training/basics/firstapp/index.html),我可以啓動應用程序確定在模擬器上,但在進入的消息,打「發送」我得到IllegalStateException異常在下面的代碼在該行所引起的NullPointerException異常Android的 - 上的EditText場NullPointerException異常

String message = editText.getText().toString(); 

經調試,很顯然,EDITTEXTnull在這一點上,但我看不出爲什麼,或者我在教程中錯過了什麼。

MainActivity類:

package com.example.myfirstapp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View;  
import android.widget.EditText; 


/** 
* <p>Main Activity class for the App</p> 
* 
* @author - thebloodguy 
*/ 
public class MainActivity extends Activity { 

    //~ ---------------------------------------------------------------------------------------------------------------- 
    //~ Static fields/initializers 
    //~ ---------------------------------------------------------------------------------------------------------------- 

    /** Key to the extra message data in the sendMessage intent */ 
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 

    //~ ---------------------------------------------------------------------------------------------------------------- 
    //~ Methods 
    //~ ---------------------------------------------------------------------------------------------------------------- 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     /* Inflate the menu; this adds items to the action bar if it is present */ 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    /** 
    * <p>Send a message using the data in the {@link View}</p> 
    * 
    * @param view - the {@link View} object representing the state of the view when the message is sent 
    */ 
    public void sendMessage(View view) { 
     Intent intent = new Intent(this, DisplayMessageActivity.class); 
     EditText editText = (EditText) view.findViewById(R.id.edit_message); 
     String message = editText.getText().toString(); // This is the guilty line 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivity(intent); 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

} 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    android:orientation="horizontal" > 

    <EditText android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button_send" 
     android:onClick="sendMessage" /> 

</LinearLayout> 

logcat的輸出:

12-21 10:02:32.210: E/Trace(621): error opening trace file: No such file or directory (2) 
12-21 10:02:32.250: W/ActivityThread(621): Application com.example.myfirstapp is waiting for the debugger on port 8100... 
12-21 10:02:32.270: I/System.out(621): Sending WAIT chunk 
12-21 10:02:32.280: I/dalvikvm(621): Debugger is active 
12-21 10:02:32.480: I/System.out(621): Debugger has connected 
12-21 10:02:32.480: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:32.680: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:32.889: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:33.091: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:33.290: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:33.534: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:33.796: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:33.990: I/System.out(621): waiting for debugger to settle... 
12-21 10:02:34.204: I/System.out(621): debugger has settled (1353) 
12-21 10:02:35.429: D/gralloc_goldfish(621): Emulator without GPU emulation detected. 
12-21 10:05:11.510: I/Choreographer(621): Skipped 94 frames! The application may be doing too much work on its main thread. 
12-21 10:05:13.850: I/Choreographer(621): Skipped 35 frames! The application may be doing too much work on its main thread. 
12-21 10:05:15.571: I/Choreographer(621): Skipped 38 frames! The application may be doing too much work on its main thread. 
12-21 10:06:53.724: D/AndroidRuntime(621): Shutting down VM 
12-21 10:06:53.724: W/dalvikvm(621): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
12-21 10:06:53.840: E/AndroidRuntime(621): FATAL EXCEPTION: main 
12-21 10:06:53.840: E/AndroidRuntime(621): java.lang.IllegalStateException: Could not execute method of the activity 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3591) 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View.performClick(View.java:4084) 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$PerformClick.run(View.java:16966) 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.handleCallback(Handler.java:615) 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Looper.loop(Looper.java:137) 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.app.ActivityThread.main(ActivityThread.java:4745) 
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511) 
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-21 10:06:53.840: E/AndroidRuntime(621): at dalvik.system.NativeStart.main(Native Method) 
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.reflect.InvocationTargetException 
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511) 
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3586) 
12-21 10:06:53.840: E/AndroidRuntime(621): ... 11 more 
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.NullPointerException 
12-21 10:06:53.840: E/AndroidRuntime(621): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:61) 
12-21 10:06:53.840: E/AndroidRuntime(621): ... 14 more 

回答

5

改變這一行

EditText editText = (EditText) view.findViewById(R.id.edit_message); 

EditText editText = (EditText)findViewById(R.id.edit_message); 
+0

天才!謝謝你解決它! – TheBloodGuy

+1

然後接受答案。 – mihail

+1

它不會讓我再等43秒 – TheBloodGuy

0

在這裏的sendMessage方法,視圖參數是被點擊的按鈕,EditText上是不是這種觀點的孩子,所以你需要從活動調用findViewById內容視圖,你打電話

EditText editText = (EditText) findViewById(R.id.edit_message); 
相關問題