0

我检查了 alert.class 以找到错误,因为 logcat 文件在启动 alert.class 活动时显示错误,但我找不到任何错误。
这是代码:

public class alert extends Activity 
{
    Button button1;
    Button button2;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);

    button1.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v1) 
        { 
        setContentView(R.layout.set);
        Intent myIntent = new Intent(getApplicationContext(), set.class);
        startActivity(myIntent);

        }
    });
    button2.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v1) 
        {
            setContentView(R.layout.set);
            Intent myIntent = new Intent(getApplicationContext(), set.class);
            startActivity(myIntent);
        }
    });
}

}

这是日志猫:

03-23 08:28:28.239: E/AndroidRuntime(463): FATAL EXCEPTION: main
03-23 08:28:28.239: E/AndroidRuntime(463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.indianic.demo.calendark/com.indianic.demo.calendark.alert}: java.lang.NullPointerException
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.os.Looper.loop(Looper.java:123)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-23 08:28:28.239: E/AndroidRuntime(463):  at java.lang.reflect.Method.invokeNative(Native Method)
03-23 08:28:28.239: E/AndroidRuntime(463):  at java.lang.reflect.Method.invoke(Method.java:507)
03-23 08:28:28.239: E/AndroidRuntime(463):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-23 08:28:28.239: E/AndroidRuntime(463):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-23 08:28:28.239: E/AndroidRuntime(463):  at dalvik.system.NativeStart.main(Native Method)
03-23 08:28:28.239: E/AndroidRuntime(463): Caused by: java.lang.NullPointerException
03-23 08:28:28.239: E/AndroidRuntime(463):  at com.indianic.demo.calendark.alert.onCreate(alert.java:37)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 08:28:28.239: E/AndroidRuntime(463):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-23 08:28:28.239: E/AndroidRuntime(463):  ... 11 more
4

1 回答 1

2
    button1.setOnClickListener(new OnClickListener()
{
    public void onClick(View v1) 
    { 
       // dont set layout here
    Intent myIntent = new Intent(getApplicationContext(), set.class);
    startActivity(myIntent); // after starting activity set.java it will create layout it self

    }
});
button2.setOnClickListener(new OnClickListener()
{
    public void onClick(View v1) 
    {

        Intent myIntent = new Intent(getApplicationContext(), set.class);
        startActivity(myIntent);
    }
});
于 2013-03-23T04:54:22.233 回答