-1

My app keeps crashing when its rotated from portrait to landscape. Since it is a small app I have been commenting blocks of code out to see where it is happening. My java file is as follows:

package com.th3ramr0d.poundforpound;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button calculate;
EditText inputname1;
EditText inputname2;
EditText inputbodyweight1;
EditText inputweightlifted1;
TextView outputname1;
TextView outputname2;
TextView outputratio1;
int bodyweight1 = 0;

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

    calculate = (Button)findViewById(R.id.calculate);

    inputname1 = (EditText)findViewById(R.id.inputname1);
    inputname2 = (EditText)findViewById(R.id.inputname2);

    inputweightlifted1 = (EditText)findViewById(R.id.inputweightlifted1);

    outputname1 = (TextView)findViewById(R.id.outputname1);
    outputname2 = (TextView)findViewById(R.id.outputname2);
    outputratio1 = (TextView)findViewById(R.id.outputratio1);



    calculate.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//I have determined these 2 lines of code are the culprit. When I comment them out it works fine but when I bring them back in like below it crashes when changing to landscape. 
                    String inputname1var = inputname1.getText().toString();
                    String inputname2var = inputname2.getText().toString();

                    /*inputbodyweight1 = (EditText)findViewById(R.id.inputbodyweight1);

                    int bodyweight1 = Integer.valueOf(inputbodyweight1.getText().toString());

                    outputname1.setText(inputname1var);
                    outputname2.setText(inputname2var);
                    outputratio1.setText(String.valueOf(bodyweight1));*/



                }
            }
    );


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<RelativeLayout

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/mainLayout">

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:focusableInTouchMode="true"
    android:id="@+id/gridlayout">

    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/textView"
        android:layout_row="0"
        android:layout_column="0" />

    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:text="Body Weight"
        android:id="@+id/textView2"
        android:layout_row="1"
        android:layout_column="0" />

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/inputbodyweight1"
        android:layout_row="1"
        android:layout_column="1" />

    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:text="Weight Lifted"
        android:id="@+id/textView3"
        android:layout_row="2"
        android:layout_column="0" />

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/inputweightlifted1"
        android:layout_row="2"
        android:layout_column="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:layout_row="3"
        android:layout_column="0" />

    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/textView4"
        android:layout_row="4"
        android:layout_column="0" />

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/inputname2"
        android:layout_row="4"
        android:layout_column="1" />

    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:text="Body Weight"
        android:id="@+id/textView5"
        android:layout_row="5"
        android:layout_column="0" />

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText5"
        android:layout_row="5"
        android:layout_column="1" />

    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:text="Weight Lifted"
        android:id="@+id/textView6"
        android:layout_row="6"
        android:layout_column="0" />

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/inputname1"
        android:layout_row="0"
        android:layout_column="1" />

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/inputname1"
        android:layout_row="0"
        android:layout_column="2" />

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText6"
        android:layout_row="6"
        android:layout_column="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:layout_row="7"
        android:layout_column="0" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Calculate"
        android:id="@+id/calculate"
        android:layout_row="8"
        android:layout_column="0"
        android:layout_columnSpan="2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="15dp"
        android:layout_row="9"
        android:layout_column="0" />

    <Button
        android:id="@+id/reset"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RESET"
        android:layout_row="10"
        android:layout_column="0"
        android:layout_columnSpan="2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/outputname1"
        android:layout_row="11"
        android:layout_column="0"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/outputname2"
        android:layout_row="11"
        android:layout_column="1"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ratio"
        android:id="@+id/outputratio1"
        android:layout_row="12"
        android:layout_column="0"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ratio"
        android:id="@+id/textView10"
        android:layout_row="12"
        android:layout_column="1"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView11"
        android:layout_row="13"
        android:layout_column="0"
        android:layout_columnSpan="2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name is stronger than name by xx%"
        android:id="@+id/textView12"
        android:layout_row="14"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="center_horizontal" />


</GridLayout>

LogCat

06-12 18:01:52.384  20125-20125/? D/dalvikvm﹕ Late-enabling CheckJNI
06-12 18:01:52.579  20125-20125/com.th3ramr0d.poundforpound I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
06-12 18:01:52.579  20125-20125/com.th3ramr0d.poundforpound W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
06-12 18:01:52.579  20125-20125/com.th3ramr0d.poundforpound D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-12 18:01:52.580  20125-20125/com.th3ramr0d.poundforpound I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
06-12 18:01:52.580  20125-20125/com.th3ramr0d.poundforpound W/dalvikvm﹕ VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
06-12 18:01:52.580  20125-20125/com.th3ramr0d.poundforpound D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-12 18:01:52.625  20125-20125/com.th3ramr0d.poundforpound I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
06-12 18:01:52.625  20125-20125/com.th3ramr0d.poundforpound W/dalvikvm﹕ VFY: unable to resolve virtual method 371: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
06-12 18:01:52.625  20125-20125/com.th3ramr0d.poundforpound D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-12 18:01:52.625  20125-20125/com.th3ramr0d.poundforpound I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
06-12 18:01:52.625  20125-20125/com.th3ramr0d.poundforpound W/dalvikvm﹕ VFY: unable to resolve virtual method 373: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
06-12 18:01:52.625  20125-20125/com.th3ramr0d.poundforpound D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-12 18:01:52.721  20125-20125/com.th3ramr0d.poundforpound D/android.widget.GridLayout﹕ horizontal constraints: x2-x0>=492, x1-x0>=263, x3-x2>=225, x3-x0<=492, x2-x1<=225 are inconsistent; permanently removing: x3-x0<=492, x2-x1<=225.
06-12 18:01:52.748  20125-20125/com.th3ramr0d.poundforpound W/Adreno-GSL﹕ <get_panel_settings:3805>: Android framework reported version 2. So, don't force ES30
06-12 18:01:52.786  20125-20125/com.th3ramr0d.poundforpound I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build:  (CL3869936)
    OpenGL ES Shader Compiler Version: 17.01.11.SPL
    Build Date: 02/28/14 Fri
    Local Branch:
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
06-12 18:01:52.865  20125-20125/com.th3ramr0d.poundforpound D/OpenGLRenderer﹕ Enabling debug mode 0
06-12 18:05:40.563  20125-20125/com.th3ramr0d.poundforpound D/AndroidRuntime﹕ Shutting down VM
06-12 18:05:40.564  20125-20125/com.th3ramr0d.poundforpound W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4183dd40)
06-12 18:05:40.577  20125-20125/com.th3ramr0d.poundforpound E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.th3ramr0d.poundforpound, PID: 20125
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.th3ramr0d.poundforpound/com.th3ramr0d.poundforpound.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
            at android.app.ActivityThread.access$900(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.th3ramr0d.poundforpound.MainActivity.onCreate(MainActivity.java:47)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
            at android.app.ActivityThread.access$900(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

If you look above you will find where I think the problem is. I am not getting any errors in Android Studio but it crashes when I rotate the screen. Pressing the button I have doesn't make it crash. Any help?

2 Answers2

3

On orientation change the system will look for layouts in layout-land or layout-port and recreates the activity with that layout so make sure all your views are available in all layouts so you do not face NPE

N Jay
  • 1,774
  • 1
  • 17
  • 36
  • so would I just change my view ids in main_activity(land) to reflect those in the main_activity? – dtrodriguez Jun 12 '15 at 15:30
  • yes @dtrodriguez, i wonder why my answer was not set as answer although gave you the solution one hour before. – N Jay Jun 13 '15 at 09:24
1

Apparently your layout for landscape (look for folder values-land) doesn't contain a Button with id 'calculate'.

Therefore findViewById(R.id.calculate) returns null and calculate.setOnClickListener(...) throws a NullPointerException.

To solve you can either

  • Add a view with the id 'calculate' in the landscape layout
  • Use only the referenced layout file for both orientations (keep your layout in the folder values and remove the ones with the same name in values-land and values-port)

Hope this helps

Nivaldo Bondança
  • 853
  • 2
  • 10
  • 20