我有一個應用程序的片段。其中一個片段要求用戶在2個文本字段中輸入隊名。我想將名字保存在onSaveInstanceState中。然而,當我移動到另一個片段並旋轉屏幕時,這種方式可行,我在下面的錯誤點出現在我試圖保存的edittext字段中。有沒有人知道我在這裏做錯了什麼。我得到一個NullPointerException但我不知道爲什麼。謝謝!片段onSaveInstanceState崩潰屏幕旋轉的應用程序
顯示java.lang.NullPointerException:嘗試上的空對象引用調用虛擬方法android.view.View android.view.View.findViewById(int)的'
錯誤點,
的EditText teamAname =(EditText)mView.findViewById(R.id.teamAname);
//Save data
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
setUserVisibleHint(false);
savedInstanceState.putInt("TeamATry", TeamATry);
savedInstanceState.putInt("TeamBTry", TeamBTry);
EditText teamAname = (EditText) mView.findViewById(R.id.teamAname);
teamAname.setInputType(InputType.TYPE_CLASS_TEXT);
String teamAnametext = teamAname.getText().toString();
EditText teamBname = (EditText) mView.findViewById(R.id.teamBname);
teamBname.setInputType(InputType.TYPE_CLASS_TEXT);
String teamBnametext = teamBname.getText().toString();
savedInstanceState.putInt("TeamAConvert", TeamAConvert);
savedInstanceState.putInt("TeamBConvert", TeamBConvert);
savedInstanceState.putInt("TeamAPenalty", TeamAPenalty);
savedInstanceState.putInt("TeamBPenalty", TeamBPenalty);
savedInstanceState.putInt("TeamAScore", TeamAscore);
savedInstanceState.putInt("TeamBScore", TeamBscore);
savedInstanceState.putString("TeamAname", teamAnametext);
savedInstanceState.putString("TeamBname", teamBnametext);
super.onSaveInstanceState(savedInstanceState);
Log.i("4", teamAnametext + "Saved");
Log.i("4", "Match Frag Scores saved");
super.onSaveInstanceState(savedInstanceState);
}
OnCreateView代碼
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//Inflate rugby layout
View rootview = inflater.inflate(R.layout.rugbycounter, container, false);
this.mView = rootview;
//This will show the menu
setHasOptionsMenu(true);
//Load saved state
if(savedInstanceState!=null)
{
teamAnametext = savedInstanceState.getString("TeamAname");
teamBnametext = savedInstanceState.getString("TeamBname");
TeamATry = savedInstanceState.getInt("TeamATry");
TeamBTry = savedInstanceState.getInt("TeamBTry");
TeamAConvert = savedInstanceState.getInt("TeamAConvert");
TeamBConvert = savedInstanceState.getInt("TeamBConvert");
TeamAPenalty = savedInstanceState.getInt("TeamAPenalty");
TeamBPenalty = savedInstanceState.getInt("TeamBPenalty");
TeamAscore = savedInstanceState.getInt("TeamAScore");
TeamBscore = savedInstanceState.getInt("TeamBScore");
Log.i("Assign4", "Score values restored");
}
return rootview;
}
Rugbycounter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--Scrollview to handle horizontal orientation-->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--Linear Layout for top section of buttons-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!--Table layout with two columns-->
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1"
android:layout_marginTop="10dp"
android:divider="@color/actionbar_background"
android:showDividers="middle"
android:dividerPadding="5dp">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/teamAname"
android:text="@+id/teamAname"
android:hint="@string/hintEnterTeamA"
android:layout_column="0"
android:gravity="center"
android:freezesText="true" >
</EditText>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/teamBname"
android:text="@+id/teamBname"
android:hint="@string/hintEnterTeamB"
android:layout_column="1"
android:gravity="center"
android:freezesText="true"
android:layout_marginStart="1dp">
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@+id/teamAscore"
android:id="@+id/teamAscore"
android:layout_column="0"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:singleLine="true"
android:freezesText="true" >
</TextView>
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@+id/teamBscore"
android:id="@+id/teamBscore"
android:layout_column="1"
android:gravity="center"
android:textStyle="bold"
android:textSize="50sp"
android:freezesText="true" >
</TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/penalty3"
android:id="@+id/aPenalty"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_column="0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/penalty3"
android:id="@+id/bPenalty"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_column="1" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/conversion2"
android:id="@+id/aConversion"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_column="0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/conversion2"
android:id="@+id/bConversion"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_column="1" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/try5"
android:id="@+id/aTry"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_column="0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/try5"
android:id="@+id/bTry"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_column="1" />
</TableRow>
</TableLayout>
<!--Linear layout to create separation between top and bottom layout-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp">
</LinearLayout>
<!--Linear Layout for bottom 3 buttons-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:gravity="center_horizontal">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset"
android:id="@+id/resetButton"
android:height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SUMMARY"
android:id="@+id/summaryButton"
android:layout_gravity="end"
android:height="50dp"
android:layout_marginEnd="5dp"
android:layout_marginStart="40dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
顯示你的onCreateView()代碼 –
我已經添加了我的onCreateView代碼。這看起來好嗎? – MarcusRey
嗨,丹尼爾,謝謝!你能告訴我一個使用成員變量作爲參考目的的例子嗎? – MarcusRey