不幸的是App已停止在Android Emulator中

时间:2014-09-01 06:30:04

标签: java android

我在我的应用程序的一个java文件中收到错误 错误是:java.lang.NullPointerException

我在下面一行,

String getName = name.getText().toString();

这是我的代码,我在detailSave方法中遇到错误

public class Contact_Details extends ActionBarActivity {

    Button save, show;
    EditText name, email, phnno;
    DataHandler handler;
    long id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact__details);

        save = (Button) findViewById(R.id.c_save);
        show = (Button) findViewById(R.id.c_show);
        name = (EditText) findViewById(R.id.c_name);
        email = (EditText) findViewById(R.id.c_email);
        phnno = (EditText) findViewById(R.id.c_phnno);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    /*
     * public void detailSave(View v) { String getName =
     * name.getText().toString(); String getEmail = email.getText().toString();
     * String getPhnno = phnno.getText().toString(); handler = new
     * DataHandler(getBaseContext()); handler.open(); id =
     * handler.insertData(getName, getEmail, getPhnno);
     * Toast.makeText(getBaseContext(), "Data inserted",
     * Toast.LENGTH_LONG).show(); handler.close(); }
     */

    public void detailSave(View v) {
        String getName = name.getText().toString();
        String getEmail = email.getText().toString();
        String getPhnno = phnno.getText().toString();

        if (getName != null && getEmail != null && getPhnno != null) {
            handler = new DataHandler(getBaseContext());
            handler.open();
            long id = handler.insertData(getName, getEmail, getPhnno);
            Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show();
            handler.close();
        }
    }

    public void detailShow(View v) {
        String getName, getEmail, getPhnno;
        getName = "";
        getEmail = "";
        getPhnno = "";
        handler = new DataHandler(getBaseContext());
        handler.open();
        Cursor C = handler.returnData();
        if (C.moveToFirst()) {
            do {
                getName = C.getString(0);
                getEmail = C.getString(1);
                getPhnno = C.getString(2);

            } while (C.moveToNext());
        }
        handler.close();
        Toast.makeText(getBaseContext(), "Name :" + getName + "\n Email :" + getEmail + "\n Contact No :" + getPhnno,
                Toast.LENGTH_LONG).show();
    }
}

Logcat:

  

09-01 02:53:44.068:D / dalvikvm(1162):GC_FOR_ALLOC释放了142K,7%免费   3231K / 3444K,暂停61ms,总计73ms 09-01 02:53:49.408:   D / AndroidRuntime(1162):关闭VM 09-01 02:53:49.408:   W / dalvikvm(1162):threadid = 1:线程退出未捕获的异常   (group = 0xb1a68ba8)09-01 02:53:49.458:E / AndroidRuntime(1162):致命   EXCEPTION:main 09-01 02:53:49.458:E / AndroidRuntime(1162):流程:   com.example.wedding_app,PID:1162 09-01 02:53:49.458:   E / AndroidRuntime(1162):java.lang.IllegalStateException:不能   执行活动的方法09-01 02:53:49.458:   E / AndroidRuntime(1162):at   android.view.View $ 1.onClick(View.java:3823)09-01 02:53:49.458:   E / AndroidRuntime(1162):at   android.view.View.performClick(View.java:4438)09-01 02:53:49.458:   E / AndroidRuntime(1162):at   android.view.View $ PerformClick.run(View.java:18422)09-01   02:53:49.458:E / AndroidRuntime(1162):at   android.os.Handler.handleCallback(Handler.java:733)09-01   02:53:49.458:E / AndroidRuntime(1162):at   android.os.Handler.dispatchMessage(Handler.java:95)09-01   02:53:49.458:E / AndroidRuntime(1162):at   android.os.Looper.loop(Looper.java:136)09-01 02:53:49.458:   E / AndroidRuntime(1162):at   android.app.ActivityThread.main(ActivityThread.java:5017)09-01   02:53:49.458:E / AndroidRuntime(1162):at   java.lang.reflect.Method.invokeNative(Native Method)09-01   02:53:49.458:E / AndroidRuntime(1162):at   java.lang.reflect.Method.invoke(Method.java:515)09-01 02:53:49.458:   E / AndroidRuntime(1162):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)   09-01 02:53:49.458:E / AndroidRuntime(1162):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)09-01   02:53:49.458:E / AndroidRuntime(1162):at   dalvik.system.NativeStart.main(Native Method)09-01 02:53:49.458:   E / AndroidRuntime(1162):引起:   java.lang.reflect.InvocationTargetException 09-01 02:53:49.458:   E / AndroidRuntime(1162):at   java.lang.reflect.Method.invokeNative(Native Method)09-01   02:53:49.458:E / AndroidRuntime(1162):at   java.lang.reflect.Method.invoke(Method.java:515)09-01 02:53:49.458:   E / AndroidRuntime(1162):at   android.view.View $ 1.onClick(View.java:3818)09-01 02:53:49.458:   E / AndroidRuntime(1162):... 11更多09-01 02:53:49.458:   E / AndroidRuntime(1162):引起:java.lang.NullPointerException   09-01 02:53:49.458:E / AndroidRuntime(1162):at   com.example.wedding_app.Contact_Details.detailSave(Contact_Details.java:56)   09-01 02:53:49.458:E / AndroidRuntime(1162):... 14更多09-01   02:53:52.018:I / Process(1162):发送信号。 PID:1162 SIG:9 09-01   02:53:55.038:D / gralloc_goldfish(1192):没有GPU仿真的仿真器   检测

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

getText()不会返回null。所以它必须是name,这里为空。仔细检查您如何初始化name = (EditText) findViewById(R.id.c_name);。可能应该责怪R.id.c_name