2015-01-02 43 views
0

我做了一個簡單的應用程序,因爲我剛剛與Android開始。我做了一個按鈕來改變佈局,但經過測試後,它每次都會崩潰應用程序。 這裏是我的代碼XML改變佈局按鈕崩潰的應用程序

package nathanschmidt.nathan; 


    import android.support.v7.app.ActionBarActivity; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.widget.NumberPicker; 
    import android.widget.TextView; 
    import android.graphics.Color; 
    import android.content.Intent; 
    import android.widget.Button; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
public class MainActivity extends ActionBarActivity { 
TextView numberView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    numberView = (TextView) findViewById(R.id.numberview); 
    NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker); 
    numberPicker.setMaxValue(10); 
    numberPicker.setMinValue(0); 
    numberPicker.setWrapSelectorWheel(true); 
    numberPicker.setOnValueChangedListener(
      new NumberPicker.OnValueChangeListener() { 
       @Override 
       public void onValueChange(NumberPicker picker, int oldVal, int newVal) { 

        int color; 
        if (newVal < 1) { 
         color = Color.parseColor("#000000"); 
        } else if (newVal < 2) { 
         color = Color.parseColor("#01093B"); 
        } else if (newVal < 3) { 
         color = Color.parseColor("#000C57"); 
        } else if (newVal < 4) { 
         color = Color.parseColor("#000F73"); 
        } else if (newVal < 5) { 
         color = Color.parseColor("#00128a"); 
        } else if (newVal < 6) { 
         color = Color.parseColor("#00159E"); 
        } else if (newVal < 7) { 
         color = Color.parseColor("#0017B0"); 
        } else if (newVal < 8) { 
         color = Color.parseColor("#001AC4"); 
        } else if (newVal < 9) { 
         color = Color.parseColor("#001FE8"); 
        } else { 
         color = Color.parseColor("#0022FF"); 
        } 

        numberView.setTextColor(color); 

       } 
      }); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.about); 

    // Watch for button clicks. 
    Button button = (Button)findViewById(R.id.change); 
    button.setOnClickListener(submitListener); 
} 

private OnClickListener submitListener = new OnClickListener() { 
    public void onClick(View v) { 

    } 
}; 

XML

<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: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:background="@drawable/os2"> 


<TextClock 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/numberview" 
    android:textSize="100dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" /> 

<NumberPicker 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/numberPicker" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentEnd="true"/> 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="change" 
    android:id="@+id/change" 
    android:layout_below="@+id/numberPicker" 
    android:layout_centerHorizontal="true" /> 

+1

後崩潰的堆棧跟蹤 –

+0

爲什麼你要爲你的內容視圖兩次?另外:你爲什麼要將它從一個佈局更改爲下一個? – DigCamara

+0

我認爲,如果我能做到這一點,我可以簡單地改變設計。除非有一種方法可以通過按下按鈕來更改xml。 – NathanAD

回答

0

什麼實際崩潰是這些代碼兩行

super.onCreate(savedInstanceState); 
setContentView(R.layout.about); 

您在這裏呼籲ActivityonCreate()方法,其中兩次這不是一個好的做法所有和你在setContentView()方法改變佈局,然後當你使用你從activity_main佈局初始化numberView而你目前的佈局是佈局。我不知道你會得到什麼異常是NullPointerExceptionIllegalStateException

+0

工作!謝謝!現在我想知道如何改變佈局。那可能嗎? – NathanAD

+0

這是可能的,但是當您調用'setContentView()'時,您正在爲活動充氣新佈局,因此您需要重新初始化您的視圖(Buttons,TextViews ...),並且不要使用從你現在沒有設置佈局。 –

相關問題