2015-06-12 152 views
-1

我的應用程序在從縱向轉爲橫向時不斷崩潰。由於它是一個小應用程序,我一直在評論代碼塊以查看它發生的位置。我的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的

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)

如果你看上面你會發現我認爲問題在哪裏。我在Android Studio中沒有收到任何錯誤,但在旋轉屏幕時它會崩潰。按下我的按鈕不會使它崩潰。任何幫助?

+0

哪條線是47號線? –

+0

第47行:calculate.setOnClickListener( – dtrodriguez

+0

你可以發佈activity_main.xml嗎? –

回答

1

顯然您的橫向佈局(查找文件夾值land)不包含按鈕id'calculate'。

因此findViewById(R.id.calculate)返回nullcalculate.setOnClickListener(...)將引發NullPointerException。

爲了解決你可以

  • 添加視圖與景觀佈局
  • 僅使用了兩個取向引用佈局文件ID「計算」(讓你的佈局文件夾values在並刪除那些與values-land相同的名稱和values-port

希望這有助於

3

與方向的變化,系統將查找在佈局土地或佈局端口的佈局,並重新創建與佈局的活動,以便確保所有的意見在所有的佈局都可以讓你不用面對NPE

+0

所以我是否只需要在main_activity(land)中更改我的view id以反映main_activity中的那些? – dtrodriguez

+0

yes @dtrodriguez,我想知道爲什麼我的答案沒有設置爲答案,儘管在一小時之前給了您解決方案。 –

相關問題