简单的Android应用程序总是崩溃

时间:2014-03-02 20:26:28

标签: android

我在开发Android应用程序的新手,但即使是最简单的应用程序总是崩溃当我尝试添加点击监听器我试图谷歌我的问题,但没有成功。我还尝试在新项目屏幕上将API更改为不同版本。我能够运行Hello World应用程序但是当我尝试将监听器添加到我的应用程序时,我无法再运行它。这是我的错误日志。

03-02 21:13:49.153  19700-19700/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.app.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:4538)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
 at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
03-02 21:13:49.163    1598-1922/? E/EmbeddedLogger﹕ App crashed! Process: com.example.app
03-02 21:13:49.163    1598-1922/? E/EmbeddedLogger﹕ App crashed! Package: com.example.app v1 (1.0)
03-02 21:13:49.163    1598-1922/? E/EmbeddedLogger﹕ Application Label: My Application 7
03-02 21:13:51.566  19729-19729/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider
03-02 21:13:56.871  19959-19959/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider

这里只是我改变了一些代码(我在设计中添加了按钮)

public class MainActivity extends ActionBarActivity {

    Button btn = null;

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

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

        btn = (Button)findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn.setText("Hi");
            }
        });
    }

3 个答案:

答案 0 :(得分:0)

查看文件MainActivity.java的第32行。听起来你正试图在null对象上调用方法。

例如,您确定您的布局activity_main.xml包含ID为Button的{​​{1}}吗?

答案 1 :(得分:0)

问题是您的R.id.button布局中不存在activity_main.xml,或者此代码中存在null

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

分别尝试 并找出哪一个无效。从那里开始

顺便说一句,NullPointerException发生在第32行。

答案 2 :(得分:0)

根据您对最后一个答案的评论,您不会导入任何R文件。

您需要以这种方式导入: import com.<package_name>.<app_name>.R;

这将导入可用的ID生成文件到您的Java代码中,并且可以看到Button id button