-1

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

我用谷歌搜索了第一个错误无济于事。但是查看日志猫的其余部分,我注意到它从主要活动中抛出了错误。我认为这可能是我的清单文件有问题,但我不确定到底是什么。

我的包名称是 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>

日志猫:

 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);
    }
4

1 回答 1

1

因为这条线给你NPE

width.setOnClickListener(this)

那么width一定是null。这意味着这button1不是您文件id中的 a 。Buttonlayoutactivity_main.xml

检查你的layout文件,你有一个Button喜欢

<Button
    android:id="@+id/button1"
    .../>

如果你觉得你有这个然后尝试清理你的项目

项目->清理..

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

于 2013-09-20T18:03:01.590 回答