我遇到了我的代碼問題,我不知道它背後的原因是什麼。我試圖從第二項活動中更新主要活動中的文本視圖。我試圖讓我的textview公共靜態,但無法更新它。另外我試圖使用onActivityResult方法的意圖,但沒有結果。這是我的主要活動的Java代碼Android:onActivityResult不起作用
.....
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String income = data.getStringExtra("X");
TextView tvView = (TextView) findViewById(R.id.MainIncome);
tvView.setText(income);
Toast.makeText(getBaseContext(), "Income Added", Toast.LENGTH_SHORT).show();
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getBaseContext(), "Cant Add Income, Something Went Wrong!", Toast.LENGTH_SHORT).show();
}
}
}
....
這是XML文件
....
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Income: " />
<TextView
android:id="@+id/MainIncome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
....
,這裏是我的第二個活動的java代碼
....
public void Add_Income(View v) {
EditText IncomePayee = (EditText) findViewById(R.id.IncomePayee);
EditText IncomeAmount = (EditText) findViewById(R.id.IncomeAmount);
IPayee = IncomePayee.getText().toString();
IAmount = Double.parseDouble(IncomeAmount.getText().toString());
IAmount += GlobalVariables.TotIncome;
Intent intent = new Intent();
intent.putExtra("X", Double.toString(IAmount));
setResult(RESULT_OK, intent);
finish();
}
....
這裏的XML文件
....
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="Income Amount"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/IncomePayee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="175dp"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/AddIncomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:layout_centerHorizontal="true"
android:onClick="Add_Income"
android:text="Add Income" />
....
最後一個音符。我不確定使用意圖是否改變文本視圖的正確方法,因爲我確實有多個活動可以從主要活動中進行。
你的實現方式真的很idk,只是奇怪&&也「我試圖讓我的textview公共靜態」不,而不是字符串.. – Elltz 2014-12-19 01:51:07
當用startActivityForResult調用一個活動時,請確保你不要完成。 – 2014-12-19 04:22:55