2012-04-24 30 views
0

Android用のプログラミングが新しく、コード内のTextViewを更新する際に問題があります。私はそれを設定することができ、それはonResume()から適切に更新されます。 (私はこれをデバッグするためのSystem.outを使用しました)Android:TextViewがinvalidate()またはpostInvalidate()で更新されない

onActivityResult(int requestCode, int resultCode, Intent returnIntent){ 
    if (resultCode == Activity.RESULT_OK){ 
     String myString= returnIntent.getString("mystr","empty"); 
     myClass=myClass.interperetResults(myString); //this method returns correctly 
     leftText.setText(""+myClass.toString()); 
     leftText.invalidate() //neither invalidate() or postInvalidate() changes result 
    } 
}; 

、それがTextViewのクラス内に更新表示されます:

その後、私は、この方法でのsetTextを使用

startActivityForIntent(my.android.activity,0) 

呼び出しますただし、画面上では更新されません。

私が知る限り、TextView.invalidate()またはTextView.postInvalidate()はどちらも有効ではありません。私は行方不明の何か、または何ですか?私は他の投稿からのさまざまな提案の多くを試しましたが、ここでは役に立たない。

私はrightTextオブジェクトと、返された文字列がnullの場合のif文も持っていますが、これは私が問題があるセクションです。皆さんが私のコードの他の部分が必要な場合は、私に知らせてください。私は喜んでお手伝いします。入力いただきありがとうございます。

編集:リクエストに応じて、ここに私のXMLレイアウトです。問題は私がその見方を見ることができないことではない。 onResume()でsetTextを呼び出すと、正しく表示され、変更されます。とにかく、ここに私のXMLコードです。私は変数名を一般的なものに変更するような気がしないので、これは実際にはそうです。私はプロジェクトのパートナーと一緒に仕事をしています。彼はXMLとレイアウトを作成した人なので、ここのすべてが何であるかは完全にはわかりません。私は皆さんがそれを理解できることを願っています。

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

} 

そしてonResume:

protected void onResume() { 
    super.onResume(); 
    // The activity has become visible (it is now "resumed"). 
    setContentView(R.layout.play); 

    myEquation = new Equation(getResources()); 
    leftText = (TextView) this.findViewById(R.id.playLeftEquation); 
    rightText = (TextView) this.findViewById(R.id.playRightEquation); 

    //leftText.setText("No data"); 
    //rightText.setText("No data"); //The textviews update fine here. 
       //Currently commented out because I thought that maybe this was 
       //overwriting the other setText, but still no change. 

} 



<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/atomic_bckgrd" 
    android:columnCount="11" > 
    <ImageButton  
     android:id="@+id/playTrashCanButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_columnSpan="2" 
     android:layout_gravity="left" 
     android:layout_row="0" 
     android:contentDescription="@string/trash" 
     android:onClick="clickHandler" 
     android:src="@drawable/trash" /> 

    <Button 
     android:id="@+id/playEditButton" 
     android:layout_column="2" 
     android:layout_row="0" 
     android:onClick="clickHandler" 
     android:text="@string/edit" /> 

    <ImageButton 
     android:id="@+id/playRulesButton" 
     android:layout_column="4" 
     android:layout_columnSpan="5" 
     android:layout_row="0" 
     android:contentDescription="@string/rules" 
     android:onClick="clickHandler" 
     android:src="@drawable/rules1" /> 

    <TextView   //One of the non-updating fields 
     android:id="@+id/playLeftEquation" 
     android:layout_column="1" 
     android:layout_gravity="bottom" 
     android:layout_row="1" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <ImageButton 
     android:id="@+id/playEnterButton" 
     android:layout_column="2" 
     android:layout_gravity="bottom" 
     android:layout_row="1" 
     android:contentDescription="@string/enter" 
     android:onClick="clickHandler" 
     android:src="@drawable/enter1" /> 

    <TextView    //The other non-updating field 
     android:id="@+id/playRightEquation" 
     android:layout_column="3" 
     android:layout_columnSpan="6" 
     android:layout_gravity="bottom" 
     android:layout_row="1" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <Space 
     android:layout_width="52dp" 
     android:layout_height="1dp" 
     android:layout_column="0" 
     android:layout_row="0" /> 

    <Space 
     android:layout_width="148dp" 
     android:layout_height="1dp" 
     android:layout_column="1" 
     android:layout_row="0" /> 

    <Space 
     android:layout_width="161dp" 
     android:layout_height="1dp" 
     android:layout_column="2" 
     android:layout_row="0" /> 

    <Space 
     android:layout_width="46dp" 
     android:layout_height="1dp" 
     android:layout_column="3" 
     android:layout_row="0" /> 

    <Space 
     android:layout_width="36dp" 
     android:layout_height="1dp" 
     android:layout_column="4" 
     android:layout_gravity="fill_horizontal" 
     android:layout_row="0" /> 

    <Space 
     android:layout_width="1dp" 
     android:layout_height="160dp" 
     android:layout_column="0" 
     android:layout_gravity="fill_horizontal" 
     android:layout_row="0" /> 

</GridLayout> 
+1

xmlレイアウトとこのアクティビティのonCreateメソッドを投稿できますか? – gutiory

+0

@ powerisallあなたはtextViewを無効にする必要はありません。ビューにはおそらくテキストが表示されていますが、何らかの理由でそれを見ることはできません。あなたがxmlレイアウトで見つけられるかもしれないと示唆しています。 – Joe

+0

申し訳ありませんが、私は関連するXMLレイアウトを含むように主要な質問を更新しました。 – powerisall

答えて

1

FIXED

はここのonCreateだ 私はこの問題を考え出しました。 myEquationからtoStringメソッドを削除して使用するアドレスを確認した後、私はonResume()でmyEquationを初期化していることに気付きました。 (私は実際にこの点でonStart()に移動しましたが、同じ効果がありました)

そうです。 onCreate()ですべての変数を初期化し、大規模な頭痛を避けます。

+1

1つしかできない場合。 ;) –

関連する問題