我该如何修复NullPointerException?

时间:2015-07-28 07:07:48

标签: android

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;

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());
            }
        });

    }

}

3 个答案:

答案 0 :(得分:2)

您错过了设置内容视图。

FB.api('/{photo_id}/', function(res) { console.log(res) })

之后添加以下行
super.onCreate(savedInstanceState);

答案 1 :(得分: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);
            }
        });

    }

}

答案 2 :(得分:0)

您没有在活动中设置布局参考。这就是为什么您需要在onCreate方法中的setContentView(R.layout.yourXMLFile);之后添加super.onCreate(savedInstanceState);的原因。希望有所帮助