2014-12-19 29 views
0

我遇到了我的代碼問題,我不知道它背後的原因是什麼。我試圖從第二項活動中更新主要活動中的文本視圖。我試圖讓我的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" /> 
.... 

最後一個音符。我不確定使用意圖是否改變文本視圖的正確方法,因爲我確實有多個活動可以從主要活動中進行。

+0

你的實現方式真的很idk,只是奇怪&&也「我試圖讓我的textview公共靜態」不,而不是字符串.. – Elltz 2014-12-19 01:51:07

+0

當用startActivityForResult調用一個活動時,請確保你不要完成。 – 2014-12-19 04:22:55

回答

-1

在你的意圖中,你沒有指定應該去哪個類。

在你Add_Income方法:

Intent intent = new Intent(this, <Put your destination class>.class); 
intent.putExtra("X", Double.toString(IAmount)); 
setResult(RESULT_OK, intent); 
startActivity(intent); 
finish(); 
+0

我試圖指定它,但不幸的是仍然無法工作。謝謝你的嘗試和時間。 – 2014-12-19 02:15:20

+0

你的第二個活動是否顯示任何佈局? 我的意思是它擴展了活動嗎? 如果沒有那麼你我想我可以幫助 – Ookami 2014-12-19 02:17:46

+1

太糟糕了,因爲我的第二個活動包含不同的元素,是的它顯示佈局。感謝您的幫助再次感謝您。 – 2014-12-19 02:21:51

0

首先:

Intent intent = new Intent(); 
    intent.putExtra("X", Double.toString(IAmount)); 
    setResult(RESULT_OK, intent); 

定義意圖:

Intent intent = new Intent(getApplicationContext(),toActivity.class); 

然後

startActivityForResult(intent); 

此後,當您從活動中返回時,您的onActivityResult()代碼將運行。