我是Android的初学者,试图运行我的第一个应用程序。 但是日志文件中有错误,并且应用程序被此消息“不幸的是,应用程序已停止”终止!

这是我的Java文件:

public class MainActivity extends ActionBarActivity {

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

    Button mClick=(Button)findViewById(R.id.button1);

    mClick.setOnClickListener(new View.OnClickListener() {


    //@Override
    @SuppressLint("ShowToast")
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast t = Toast.makeText(MainActivity.this, R.string.hi, 3000).show();          

    }
            });
}
}

并且日志有以下错误:

04-19 18:50:02.200: E/AndroidRuntime(792): FATAL EXCEPTION: main
04-19 18:50:02.200: E/AndroidRuntime(792): Process: com.example.test, PID: 792
04-19 18:50:02.200: E/AndroidRuntime(792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.os.Looper.loop(Looper.java:136)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.ActivityThread.main(ActivityThread.java:5017)
04-19 18:50:02.200: E/AndroidRuntime(792):  at java.lang.reflect.Method.invokeNative(Native Method)
04-19 18:50:02.200: E/AndroidRuntime(792):  at java.lang.reflect.Method.invoke(Method.java:515)
04-19 18:50:02.200: E/AndroidRuntime(792):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-19 18:50:02.200: E/AndroidRuntime(792):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-19 18:50:02.200: E/AndroidRuntime(792):  at dalvik.system.NativeStart.main(Native Method)
04-19 18:50:02.200: E/AndroidRuntime(792): Caused by: java.lang.NullPointerException
04-19 18:50:02.200: E/AndroidRuntime(792):  at com.example.test.MainActivity.onCreate(MainActivity.java:29)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.Activity.performCreate(Activity.java:5231)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-19 18:50:02.200: E/AndroidRuntime(792):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-19 18:50:02.200: E/AndroidRuntime(792):  ... 11 more

你能发现问题吗?

===============>>#1 票数:0 已采纳

您的mClick按钮未创建,可能是因为findViewById(R.id.button1)失败。

然后,由于按钮为空,因此当您在其上调用函数时,会得到一个NPE。

===============>>#2 票数:0

mClick可能为空。 检查在setContentView上设置的布局中是否存在R.id.button1。

  ask by user3552862 translate from so

未解决问题?本站智能推荐: