-1

I have been following Android Development tutorials online but when I run the app on the emulator it closes due to errors.

I Googled the first error to no avail.But looking through the rest of the log cat I noticed it was throwing errors from the main activity.I think it may be a problem with my manifest file but I'm not sure what exactly.

My package name is com.example.androidtutorials.

Could someone enlighten me as to what I have done incorrectly in following the tutorial.My manifest file is as follows:

<?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

My onCreate method from the main class:

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);
    }
Brian Var
  • 6,029
  • 25
  • 114
  • 212

1 Answers1

1

Since this line is giving you the NPE

width.setOnClickListener(this)

then width must be null. This means that button1 is not an id of a Button in your layout file activity_main.xml.

Check your layout file that you have a Button like

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

If you feel you have this then try cleaning your project

Project --> Clean..

and please post that xml file if that doesn't fix your problem. But I'm sure that you will find you don't.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Sorted now.The problem was with the ids as you stated in your answer.The button name was uppercase Button. – Brian Var Sep 20 '13 at 18:19