Jason Cheladyn
Jason Cheladyn

Reputation: 595

Why is onClickListener crashing my app when I start it?

i'm a new programmer trying to load a new view when I push a button on my splash screen. My splash is a viewPager with 4 views. I have a button on those views which when pushed I want a listview view to load. But When I added onClickListener it crashes as soon as the app starts. I've been trying to figure this out for a few days now and now i'm really desperate! Thanks in advance.

I tried to find out what fatal exception MAIN means, but it seems be a different problem for every program.

public class Splash extends Activity {

ImageButton listButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash);

    SplashPager adapter = new SplashPager();
    ViewPager myPager = (ViewPager) findViewById(R.id.splashPager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);

    listButtonListener();
}

public void listButtonListener() {
    listButton = (ImageButton) findViewById(R.id.splashB);
    listButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            startActivity(new Intent("com.example.survtest1.Main"));
        }
    });
}
}

//Crash Log

01-14 22:12:32.069: W/dalvikvm(14733): threadid=1: thread exiting with uncaught exception (group=0x40207560)
01-14 22:12:32.079: E/AndroidRuntime(14733): FATAL EXCEPTION: main
01-14 22:12:32.079: E/AndroidRuntime(14733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest1/com.example.survtest1.Splash}: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.os.Looper.loop(Looper.java:130)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.main(ActivityThread.java:3733)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at java.lang.reflect.Method.invoke(Method.java:507)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at dalvik.system.NativeStart.main(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733): Caused by: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.example.survtest1.Splash.listButtonListener(Splash.java:40)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.example.survtest1.Splash.onCreate(Splash.java:31)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-14 22:12:32.079: E/AndroidRuntime(14733):    ... 11 more
01-14 22:12:32.079: E/AndroidRuntime(14733): [Blue Error Handler] Make Debugging Report file for main
01-14 22:12:32.079: E/AndroidRuntime(14733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.survtest1/com.example.survtest1.Splash}: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.os.Looper.loop(Looper.java:130)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.main(ActivityThread.java:3733)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at java.lang.reflect.Method.invoke(Method.java:507)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:650)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at dalvik.system.NativeStart.main(Native Method)
01-14 22:12:32.079: E/AndroidRuntime(14733): Caused by: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.example.survtest1.Splash.listButtonListener(Splash.java:40)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.example.survtest1.Splash.onCreate(Splash.java:31)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-14 22:12:32.079: E/AndroidRuntime(14733):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
01-14 22:12:32.079: E/AndroidRuntime(14733):    ... 11 more

//manifest

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.survtest1.Splash"
        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="com.example.survtest1.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

//xml layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="@+id/splash" 
android:background="#000000" >

<ImageView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/intro__1" />

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="aaaaaaaaaaaaaaaaaa" 
       android:layout_gravity="bottom" 
       android:layout_marginLeft="75dp"
       android:id="@+id/splashB" 
       android:onClick="startActivity" />

</FrameLayout>

I just tried to add the android:onClick and it's still crashing

Upvotes: 0

Views: 252

Answers (3)

Franci Penov
Franci Penov

Reputation: 75981

Your problem is this:

01-14 22:12:32.079: E/AndroidRuntime(14733): Caused by: java.lang.NullPointerException
01-14 22:12:32.079: E/AndroidRuntime(14733):    at com.example.survtest1.Splash.listButtonListener(Splash.java:40)

If I were to guess (as you have not pasted the full source for your activity, I am not sure which line is line 40), I'd say your bug is on this line:

listButton = (ImageButton) findViewById(R.id.splashB);

Specifically, your layout most likely is missing <ImageButton android:id="@+id/splashB" ...> element. Thus, your listButton is null, which results in the NullPointerException in the next line.

Update: Your app crashes because, while it does have a <Button> with an id of splashB, that button is not an <ImageButton>, which is what your code expects.

You have to either change your layout to use <ImageButton> or change this line:

listButton = (ImageButton) findViewById(R.id.splashB);

to:

listButton = (Button) findViewById(R.id.splashB);

Either should work.

Upvotes: 1

Vsw10
Vsw10

Reputation: 143

listButton = (ImageButton) findViewById(R.id.splashB); here it's not reffering the correct id .According to my guess either splashB reffering to something else other than image button or its not created. So check inside xml file or paste your xml such that i can conclude the main reasons behind the error.

Upvotes: 0

BBdev
BBdev

Reputation: 4942

I might be guessing but i think your actual problem lies in this piece of code

public void onClick(View arg0) {
     startActivity(new Intent("com.example.survtest1.Main"));
}

Here you are starting a new Activity with an action, But you are not giving a valid Action. So if you want to start a new activity there You should pass the Context and your valid class name. Or if you are starting an Activity with an Action please make sure you are starting a valid action.

Please look at Starting Another Activity, and this Start Activity Using Custom Action.

Upvotes: 0

Related Questions