0

For some reason, my app crashes on the tablet I'm testing on, but not on the phone. I'm not sure what is causing this. It happens in different parts of the app, but it's always when moving from one screen to another.

Here is the code that seems to crash the app:

public void About(View view) 
    {
                Intent i = new Intent();
                i.setClassName("com.example","com.example.timestableseasy.About");
                startActivity(i);

    }

about.java

public class About extends Activity
{
TextView home;
@Override
public void onBackPressed() {
}
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);
    home = (TextView) findViewById(R.id.home);
    home.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v)
        {
            Log.d("Exercise", "button clicked");
            Intent i = new Intent();
            i.setClassName("com.example","com.example.timestableseasy.Menu2");
            startActivity(i);       
        }


    });
}
}

and here is the logcat:

06-16 16:20:59.465: D/AndroidRuntime(6281): Shutting down VM
06-16 16:20:59.465: W/dalvikvm(6281): threadid=1: thread exiting with uncaught exception (group=0x412f2930)
06-16 16:20:59.473: E/AndroidRuntime(6281): FATAL EXCEPTION: main
06-16 16:20:59.473: E/AndroidRuntime(6281): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.timestableseasy.About}: java.lang.NullPointerException
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.ActivityThread.access$700(ActivityThread.java:150)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.os.Looper.loop(Looper.java:175)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.ActivityThread.main(ActivityThread.java:5279)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at java.lang.reflect.Method.invoke(Method.java:511)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at dalvik.system.NativeStart.main(Native Method)
06-16 16:20:59.473: E/AndroidRuntime(6281): Caused by: java.lang.NullPointerException
06-16 16:20:59.473: E/AndroidRuntime(6281):     at com.example.timestableseasy.About.onCreate(About.java:25)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.Activity.performCreate(Activity.java:5283)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-16 16:20:59.473: E/AndroidRuntime(6281):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-16 16:20:59.473: E/AndroidRuntime(6281):     ... 11 more

This worked before, but stopped working in the last week for some reason.

EDIT: I have eliminated the possibility of there being an error in the large layout file - forcing the application to load the normal version by deleting the large file, it still shows the same error when pressing the button to go to the about screen.

neminem
  • 2,658
  • 5
  • 27
  • 36
Darren Murtagh
  • 591
  • 2
  • 6
  • 28
  • you got a NullPointerEx, can you post more of your About activity class? – PedroCactus Jun 16 '14 at 15:32
  • yeah sure but its just a button and a background – Darren Murtagh Jun 16 '14 at 15:33
  • @PedroCactus i added in the java class. there is nothing too special about it – Darren Murtagh Jun 16 '14 at 15:35
  • Maybe you have something different in the /res folder for higher resolutions? – Richard Lindhout Jun 16 '14 at 15:36
  • @RichardLindhout the layout is exactly the same for both resolution with bigger text in one. infact i copied the layout from the higher resolution and shrunk it to phone size which is works on – Darren Murtagh Jun 16 '14 at 15:43
  • Why aren't you using the Intent constructor new Intent(this, About.class);? – EpicPandaForce Jun 16 '14 at 16:28
  • @Zhuinden it was just the way i was shown how to do it. is one way bettter than the other? – Darren Murtagh Jun 16 '14 at 16:46
  • I think providing the .class is less likely to cause errors, for example I solved a case with the reflection of the Class object once on SO (using Class.forName) where the guy wanted to dynamically assign the class name to the Intent. – EpicPandaForce Jun 16 '14 at 16:51
  • ok i may start using that from now on. though i did figure out my problem. kinda the tablet has a problem with the onClickListener where the phone doesnt. it happens somewhere else in the app where the tablet doesn't like that there is a setText() statement. – Darren Murtagh Jun 16 '14 at 16:59
  • That's interesting. The question I was talking about is the following (not really relevant but the reason why I prefer the 'class': http://stackoverflow.com/questions/23993726/start-a-new-intent-by-setclassname-with-same-packege-in-android/23994149#23994149 ) – EpicPandaForce Jun 16 '14 at 17:15

1 Answers1

0

The only place when your NPE can occur is on this line:

home.setOnClickListener(new View.OnClickListener() ...

Which means that this line:

home = (TextView) findViewById(R.id.home);

returns null. Make sure you actually have id="@+id/home" on one of the TextViews in your activity_about.xml file.

Note that you may have separate layout files for phones and tablets. Usually, for phones, the layouts are taken from res/layout folder, for tablets - from res/layout-sw600dp - and only if that one is missing will the one from the res/layout be used. There really is no other place where this error can occur.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • but i do and it works. on my phone the problem is when the app runs on the tablet i'm testing on – Darren Murtagh Jun 16 '14 at 15:39
  • 1
    @DarrenMurtagh Are you sure you don't have separate layout files for phones and tablets? Usually, for phones, the layouts are taken from `res/layout` folder, for tablets - from `res/layout-sw600dp` - and only if that one is missing will the one from the `res/layout` be used. There really is no other place where this error can occur. – Aleks G Jun 16 '14 at 15:41
  • i do have separate layouts and they work in every other part of the app. including the menu which the button to get to about.java is its only this page that crashes the app. and as i said this is a recent problem that only just appeared after workign many times before without changing the code at all – Darren Murtagh Jun 16 '14 at 15:46
  • @DarrenMurtagh The issue is clearly _only_ with `res/layout/activity_about.xml` file - so do double-check it. – Aleks G Jun 16 '14 at 15:48
  • ok so i deleted the large layout to force the load of the normal size layout and it still crashes with the same errors – Darren Murtagh Jun 16 '14 at 16:18
  • i know the normal one works because it will come up and work on the phone i am using to test it for – Darren Murtagh Jun 16 '14 at 16:19