-4

My application was working normally but then when I added the splash screen an error has been appeared and I don't know what to do or how t solve it

the error is

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

my MainActivity

public class MainActivity extends AppCompatActivity {

private final int SPLASH_DISPLAY_LENGTH = 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_splash);


    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent splashIntent = new Intent(MainActivity.this , SplashActivity.class);

            MainActivity.this.startActivity(splashIntent);
            MainActivity.this.finish();
        }
    },SPLASH_DISPLAY_LENGTH);




    // Find the View that shows the numbers category
    TextView numbers = (TextView) findViewById(R.id.numbers);

    // Set a click listener on that View
    numbers.setOnClickListener(new OnClickListener() {
        // The code in this method will be executed when the numbers category is clicked on.
        @Override
        public void onClick(View view) {
            // Create a new intent to open the {@link NumbersActivity}
            Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

            // Start the new activity
            startActivity(numbersIntent);
        }
    });

and the activity_splash

  <?xml version="1.0" encoding="utf-8"?>
  <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"
android:background="@color/category_phrases"
tools:context="com.example.android.miwok.SplashActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/nemo"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="HELLO NEMO"
    android:textColor="#FFF"
    android:textSize="40sp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="40dp"
    android:fontFamily="casual"
    android:textStyle="bold|italic"

    />

SplashActivity 

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
}
}

anything to solve it?

  • 2
    `android:id="@+id/numbers"` add this to your textView in the splash activity !! – Santanu Sur Mar 07 '18 at 09:31
  • you are getting wrong textview id that's why it crashing, give id to textview and then find in your activity. – Hemant Parmar Mar 07 '18 at 09:33
  • Are you sure `activity_splash` is the right layout for `MainActivity`? – Mike M. Mar 07 '18 at 09:35
  • this is not a duplicate - the `SplashActivity` is finishing the MainActivity, making it null... – Jim Mar 07 '18 at 09:36
  • the numbers id is in another XML activity and when add an id to the text view in activity_splash it still show an error :/ @SantanuSur – Tabarek Ghassan Mar 07 '18 at 09:38
  • the activity_splash will appear for 4 seconds and then the activity_main will appear @MikeM. – Tabarek Ghassan Mar 07 '18 at 09:41
  • 1
    @Jim This is a duplicate. The `TextView` with ID `numbers` is not in the layout they're setting on `MainActivity`. The `postDelayed()` on the `Handler` does not pause execution there. It proceeds immediately, and crashes when the `findViewById()` call returns null, and the `setOnClickListener()` call throws the NPE. The `finish()` has nothing to do with it. – Mike M. Mar 07 '18 at 09:45
  • Tabarek Ghassan, you're calling `setContentView()` in `MainActivity` with `R.layout.activity_splash`, not `R.layout.activity_main`. Also, if you want the splash to appear first, don't you want the `Handler` code in `SplashActivity` instead? And also `SplashActivity` as your launcher `Activity`? As you have it now, `MainActivity` is starting the `SplashActivity` after your set duration. – Mike M. Mar 07 '18 at 09:46
  • @MikeM. - the OP states "application was working normally" until the change... clearly many things changed, but the "duplicate" references are neither relevant to the changes nor obvious as "duplicate" issues (although the "findViewById" link is helpful, and relevant in the sense that it explain the NPE, the OP appears to be unaware of how that change created the issue... and "fixing" it still doesn't make the app work again..) – Jim Mar 07 '18 at 09:56
  • I changed the `R.layout.activity_splash` to `R.layout.activity_main ` but it shows the `activity_main` first for 4 sec and then the `activity_splash` appears ..... `public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); } }` this is my SplashActivity. @MikeM. – Tabarek Ghassan Mar 07 '18 at 10:00
  • @Jim The second linked duplicate is exactly relevant. It explains why `findViewById()` is returning null. – Mike M. Mar 07 '18 at 10:00
  • Tabarek Ghassan, yes, because you have your logic backwards. The `Handler` code should be in `SplashActivity`, with an `Intent` for `MainActivity`, and the `` you currently have on `MainActivity`'s `` element in the manifest should be moved to `SplashActivity`'s ``. – Mike M. Mar 07 '18 at 10:05
  • @MikeM.- yes, and see the OP's comment prior to yours. If that answer fixed the issue, this would be resolved... it seems the question is poorly worded, but the problem was poorly understood by the OP. Closing the question does not allow for responses that are more complex that could help others that might make similar mistakes (I was constructing a response as the question was closed... it would explain the multiple issues raised in the question) – Jim Mar 07 '18 at 10:05
  • the 4 second delay is the postDelay in the SplashActivity - Main will display for 4 seconds while it waits.. but the then the MainActivity is "finished" so as @MikeM. points out. The SplayActivity should finish after 4 seconds.. – Jim Mar 07 '18 at 10:12
  • @Jim We already have hundreds of posts here that demonstrate a splash `Activity`. If you feel you have new insights to this, then you should post your answer on one of the "canonicals", so that future searches, as well as questions marked as duplicates of that one, point to the sum collected knowledge. (I've edited the duplicates list with the main one.) Those looking for advice on implementing a splash screen in the future aren't going to be searching for a `NullPointerException` on a `TextView`. – Mike M. Mar 07 '18 at 10:16

1 Answers1

1

You forgot to set the id in the textview

<TextView
    android:id="@+id/numbers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="HELLO NEMO"
    android:textColor="#FFF"
    android:textSize="40sp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="40dp"
    android:fontFamily="casual"
    android:textStyle="bold|italic"

/>
Alessandro.Vegna
  • 1,242
  • 10
  • 19