0

I am making a pretty simple app, and I am testing the first part of it before adding all of the other parts to make sure it works. When I launch the app in the emulator, the screen rotates (I'm forcing the app into landscape mode) and it crashes. Here is my code: 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);
        }
    });
}
}

My XML code:

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

Here is my 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

My new 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 Answers1

0

You havent initialised the ImageButton hydrogen in your onCreate()

That is why you are getting a NullPointerException

So kindly add

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

before

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

So, It should be like this..

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);
            }
        });
Lal
  • 14,505
  • 4
  • 39
  • 63