1

我一直在关注 Android 教程并创建了MyFirstApp(请参阅http://developer.android.com/training/basics/firstapp/index.html),我可以在模拟器上启动该应用程序,但是在输入消息和点击“发送”我在下面的代码中的这一行得到一个由NullPointerException引起的IllegalStateException :

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

调试后,很明显此时 editText,但我看不出原因,或者我在教程中遗漏的任何内容。

我的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
4

2 回答 2

5

更改此行

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

EditText editText = (EditText)findViewById(R.id.edit_message);
于 2012-12-21T09:13:00.737 回答
0

这里在 sendMessage 方法中,view 参数是被点击的 Button,EditText 不是这个 view 的子视图,所以需要从 activity 内容视图中调用 findViewById,调用方式是

EditText editText = (EditText) findViewById(R.id.edit_message);
于 2012-12-21T09:15:31.147 回答