运行应用程序时主要活动出错

时间:2013-09-20 17:41:30

标签: java android android-activity android-logcat

我一直在线关注Android开发教程,但是当我在模拟器上运行应用程序时,它会因错误而关闭。

我用Google搜索了第一个错误无效。但是看一下log cat的其余部分,我注意到它是从主要活动中抛出错误。我认为这可能是我的清单文件有问题,但我不是确定究竟是什么。

我的包名是com.example.androidtutorials。

有人可以告诉我在教程之后我做错了什么。我的清单文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidtutorials"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidtutorials.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Numbers"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.NUMBERS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".InternalStorage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.INTERNALSTORAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Reading"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.READING" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MENU" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

         <activity
            android:name=".SaveToSD"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.SAVETOSD" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>  

    </application>

</manifest>

Log cat:

 09-20 17:22:58.802: E/Trace(799): error opening trace file: No such file or directory (2)
    09-20 17:23:02.662: D/AndroidRuntime(799): Shutting down VM
    09-20 17:23:02.662: W/dalvikvm(799): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    09-20 17:23:02.707: E/AndroidRuntime(799): FATAL EXCEPTION: main
    09-20 17:23:02.707: E/AndroidRuntime(799): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtutorials/com.example.androidtutorials.MainActivity}: java.lang.NullPointerException
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.os.Looper.loop(Looper.java:137)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.ActivityThread.main(ActivityThread.java:5041)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at java.lang.reflect.Method.invoke(Method.java:511)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at dalvik.system.NativeStart.main(Native Method)
    09-20 17:23:02.707: E/AndroidRuntime(799): Caused by: java.lang.NullPointerException
    09-20 17:23:02.707: E/AndroidRuntime(799):  at com.example.androidtutorials.MainActivity.onCreate(MainActivity.java:27)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.Activity.performCreate(Activity.java:5104)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    09-20 17:23:02.707: E/AndroidRuntime(799):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    09-20 17:23:02.707: E/AndroidRuntime(799):  ... 11 more
    09-20 17:27:43.197: I/Process(799): Sending signal. PID: 799 SIG: 9
    09-20 17:27:45.187: D/AndroidRuntime(858): Shutting down VM
    09-20 17:27:45.187: W/dalvikvm(858): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    09-20 17:27:45.207: E/AndroidRuntime(858): FATAL EXCEPTION: main
    09-20 17:27:45.207: E/AndroidRuntime(858): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtutorials/com.example.androidtutorials.MainActivity}: java.lang.NullPointerException
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.os.Looper.loop(Looper.java:137)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.ActivityThread.main(ActivityThread.java:5041)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at java.lang.reflect.Method.invoke(Method.java:511)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at dalvik.system.NativeStart.main(Native Method)
    09-20 17:27:45.207: E/AndroidRuntime(858): Caused by: java.lang.NullPointerException
    09-20 17:27:45.207: E/AndroidRuntime(858):  at com.example.androidtutorials.MainActivity.onCreate(MainActivity.java:27)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.Activity.performCreate(Activity.java:5104)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    09-20 17:27:45.207: E/AndroidRuntime(858):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    09-20 17:27:45.207: E/AndroidRuntime(858):  ... 11 more
    09-20 17:27:48.847: I/Process(858): Sending signal. PID: 858 SIG: 9

来自主类的onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        width = (Button)findViewById(R.id.button1);
        height = (Button)findViewById(R.id.button2);
        calc = (Button)findViewById(R.id.button3);
        area = (TextView)findViewById(R.id.textView1);

        width.setOnClickListener(this);
        height.setOnClickListener(this);
        calc.setOnClickListener(this);
    }

1 个答案:

答案 0 :(得分:1)

由于此行为您提供了NPE

width.setOnClickListener(this)

然后width必须是null。这意味着button1不是id文件Buttonlayout的{​​{1}}。

检查您的activity_main.xml文件中是否有layout

Button

如果您觉得有这个,请尝试清理项目

  

项目 - &gt;清洁..

如果不能解决您的问题,请发布该xml文件。但我相信你会发现你没有。