应用程序崩溃方向变化

时间:2015-06-12 15:02:30

标签: android

我的应用在从纵向旋转到横向时不断崩溃。由于它是一个小应用程序,我一直在评论代码块,看看它发生了什么。我的java文件如下:

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的

&#13;
&#13;
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)
&#13;
&#13;
&#13;

如果你看上面你会发现我认为问题所在。我在Android Studio中没有收到任何错误,但是当我旋转屏幕时它会崩溃。按下我没有的按钮不会使它崩溃。有什么帮助吗?

2 个答案:

答案 0 :(得分:3)

在方向更改时,系统将在layout-land或layout-port中查找布局,并使用该布局重新创建活动,以确保所有布局中的所有视图都可用,这样您就不会面对NPE

答案 1 :(得分:1)

显然你的横向布局(寻找文件夹值 - 土地)不包含带 id 的按钮&#39;计算&#39;。

因此findViewById(R.id.calculate)返回nullcalculate.setOnClickListener(...)会抛出NullPointerException。

要解决你可以

  • 使用 id 添加视图&#39;计算&#39;在景观布局中
  • 仅对两个方向使用引用的布局文件(将布局保留在values文件夹中,并删除values-landvalues-port中具有相同名称的布局

希望这有帮助