我的应用程序在模拟器中崩溃,但项目没有错误

时间:2014-07-16 17:21:13

标签: android

我正在制作一个非常简单的应用程序,我正在测试它的第一部分,然后添加所有其他部分以确保它有效。当我在模拟器中启动应用程序时,屏幕旋转(我强迫应用程序进入横向模式)并且它崩溃了。这是我的代码: MainActivity.java:

package martinproductions.periodictable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

ImageButton hydrogen;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hydrogen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(getApplicationContext(),Hydrogen.class);
            startActivity(i);
        }
    });
}
}

我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >

<ImageButton
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:src="@drawable/hydrogenicon"
    android:background="@null"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_marginLeft="3dp"
    android:layout_marginTop="3dp"
    android:id="@+id/Hydrogen"
    android:contentDescription="@string/hydrogendesc" />

</RelativeLayout>

这是我的logcat:

07-16 13:17:08.300: D/dalvikvm(812): GC_FOR_ALLOC freed 48K, 9% free 3451K/3792K,       paused 173ms, total 177ms
07-16 13:17:08.560: D/AndroidRuntime(812): Shutting down VM
07-16 13:17:08.560: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0xb1a7aba8)
07-16 13:17:08.720: E/AndroidRuntime(812): FATAL EXCEPTION: main
07-16 13:17:08.720: E/AndroidRuntime(812): Process: martinproductions.periodictable,   PID: 812
07-16 13:17:08.720: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to start activity   ComponentInfo{martinproductions.periodictable/martinproductions.periodictable.MainActivity}: java.lang.NullPointerException
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.os.Looper.loop(Looper.java:136)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 13:17:08.720: E/AndroidRuntime(812):  at java.lang.reflect.Method.invokeNative(Native Method)
07-16 13:17:08.720: E/AndroidRuntime(812):  at java.lang.reflect.Method.invoke(Method.java:515)
07-16 13:17:08.720: E/AndroidRuntime(812):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 13:17:08.720: E/AndroidRuntime(812):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 13:17:08.720: E/AndroidRuntime(812):  at dalvik.system.NativeStart.main(Native Method)
07-16 13:17:08.720: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
07-16 13:17:08.720: E/AndroidRuntime(812):  at martinproductions.periodictable.MainActivity.onCreate(MainActivity.java:16)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.Activity.performCreate(Activity.java:5231)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-16 13:17:08.720: E/AndroidRuntime(812):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-16 13:17:08.720: E/AndroidRuntime(812):  ... 11 more
07-16 13:17:43.109: I/Process(812): Sending signal. PID: 812 SIG: 9

我的新logcat:

07-16 13:33:33.460: D/AndroidRuntime(977): Shutting down VM
07-16 13:33:33.460: W/dalvikvm(977): threadid=1: thread exiting with uncaught exception (group=0xb1a90ba8)
07-16 13:33:33.500: E/AndroidRuntime(977): FATAL EXCEPTION: main
07-16 13:33:33.500: E/AndroidRuntime(977): Process: martinproductions.periodictable, PID: 977
07-16 13:33:33.500: E/AndroidRuntime(977): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{martinproductions.periodictable/martinproductions.periodictable.MainActivity}: java.lang.NullPointerException
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.os.Looper.loop(Looper.java:136)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 13:33:33.500: E/AndroidRuntime(977):  at java.lang.reflect.Method.invokeNative(Native Method)
07-16 13:33:33.500: E/AndroidRuntime(977):  at java.lang.reflect.Method.invoke(Method.java:515)
07-16 13:33:33.500: E/AndroidRuntime(977):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 13:33:33.500: E/AndroidRuntime(977):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 13:33:33.500: E/AndroidRuntime(977):  at dalvik.system.NativeStart.main(Native Method)
07-16 13:33:33.500: E/AndroidRuntime(977): Caused by: java.lang.NullPointerException
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.Activity.findViewById(Activity.java:1884)
07-16 13:33:33.500: E/AndroidRuntime(977):  at martinproductions.periodictable.MainActivity.<init>(MainActivity.java:11)
07-16 13:33:33.500: E/AndroidRuntime(977):  at java.lang.Class.newInstanceImpl(Native Method)
07-16 13:33:33.500: E/AndroidRuntime(977):  at java.lang.Class.newInstance(Class.java:1208)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-16 13:33:33.500: E/AndroidRuntime(977):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-16 13:33:33.500: E/AndroidRuntime(977):  ... 11 more
07-16 13:33:41.060: I/Process(977): Sending signal. PID: 977 SIG: 9

1 个答案:

答案 0 :(得分:0)

您尚未初步化ImageButton

中的hydrogen onCreate()

这就是你得到NullPointerException

的原因

好好添加

ImageButton hydrogen=(ImageButton)findViewById(R.id.Hydrogen);

hydrogen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(getApplicationContext(),Hydrogen.class);
            startActivity(i);
        }
    });

所以,它应该是这样的..

ImageButton hydrogen=(ImageButton)findViewById(R.id.Hydrogen);
hydrogen.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getApplicationContext(),Hydrogen.class);
                startActivity(i);
            }
        });