0

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

Jason Cheladyn
  • 565
  • 1
  • 12
  • 24

3 Answers3

1

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.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • this is line 40 listButton.setOnClickListener(new OnClickListener() { – Jason Cheladyn Jan 15 '13 at 04:06
  • How do I fix it? Why is it null? findViewById is using the correct id. Thanks a lot ♥ – Jason Cheladyn Jan 15 '13 at 04:20
  • Can you post your xml layout in the question as well? – Franci Penov Jan 15 '13 at 04:21
  • Sorry I didn't tell you I changed the imagebutton to button after this crash. Its not the problem. ;( – Jason Cheladyn Jan 15 '13 at 06:14
  • Also, it might be useful to post in your question the exact sources that do repro the exception. The exception you have in the logcat output would be exactly would I would expect with the sources you have posted. But if you sources have changed after you hit that exception, all bets are off. Did you happen to change other stuff as well? – Franci Penov Jan 15 '13 at 06:23
  • No I only changed it from an imageButton to a Button, thank you for the advice I'm new to this site. I really appreciate it. – Jason Cheladyn Jan 15 '13 at 08:03
  • It's still saying listButton is null, I don't understand. – Jason Cheladyn Jan 15 '13 at 08:06
  • As the source code in the questions is right now, the code is expecting `ImageButton`, the layout file has a `Button`. You *cannot* cast `Button` to `ImageButton`, hence line 39 _will_ result on `listButton` being null. _This_ is the bug in the question source code. *If* you have a different source code that is still giving you the same error, the only way to get a different answer would be to post _that_ source code. – Franci Penov Jan 16 '13 at 02:52
0

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.

Community
  • 1
  • 1
BBdev
  • 4,898
  • 2
  • 31
  • 45
  • 1
    `Activity.startActivity` does not need `Context`, as the activity itself is a context. Also, the actions on intents are just string, and as long as there's any .apk which declares an activity with intent filter with the same string, it's "valid". – Franci Penov Jan 15 '13 at 03:46
  • he might not define his custom action in manifest file, or may be havent define the activity there. – BBdev Jan 15 '13 at 03:50
  • If there is no activity declared that listens to that action, startActivity will do nothing. It will not throw a NullPointerException. – Franci Penov Jan 15 '13 at 03:53
0

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.

Vsw10
  • 143
  • 3
  • 10
  • Thanks, I posted the xml, I changed it from an imageBtn to a regular btn. – Jason Cheladyn Jan 15 '13 at 04:31
  • now do the similar changes in your code. After changes clean and run the code – Vsw10 Jan 15 '13 at 04:38
  • and let me know about your logcat?? – Vsw10 Jan 15 '13 at 04:39
  • comment these lines and then once again clean and run ViewPager myPager = (ViewPager) findViewById(R.id.splashPager); myPager.setAdapter(adapter); myPager.setCurrentItem(0); – Vsw10 Jan 15 '13 at 05:16
  • still getting (Splash.java.40) crash :9 ugh I dont understand. I guess I'll just have to remake the entire project a different way. – Jason Cheladyn Jan 15 '13 at 06:17
  • same logs u getting this time also or some thing different..Do me a favour, plz paste the recent logs .. – Vsw10 Jan 15 '13 at 06:34
  • The same exact logs, it's something to do with the onClickListener, it's saying listButton is null. I don't understand why it's null. – Jason Cheladyn Jan 15 '13 at 08:04
  • have you changed the type of listButton from ImageButton to Button? – Vsw10 Jan 15 '13 at 09:02
  • just check once again your setContentView . Check that you referred the correct xml or not . I have examined your problem and come to the conclusion that the exception could be either due to setContentView or findViewById. Just pay your attention on these two things.. – Vsw10 Jan 15 '13 at 09:24
  • ViewPager myPager = (ViewPager) findViewById(R.id.splashPager); where you have mentioned this.Because in your provided xml it's not visible.And it should give exception but its not giving .So, it means that the layout provided by you inside setContentView is another one and not taking layout where you have defined the Button. – Vsw10 Jan 15 '13 at 09:31