I have an Activity and an inner class with name InnerClass
in it which its updateAfterDelay()
method must update a TextView
. I want to avoid memory leak. So I changed InnerClass
to static and try to use WeakReference
like this:
public class MainActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.main );
textView = (TextView) findViewById ( R.id.textView );
}
private static class InnerClass {
private final WeakReference<TextView> targetTextView;
public InnerClass(TextView targetTextView) {
this.targetTextView = new WeakReference<TextView> ( targetTextView );
}
void updateAfterDelay() {
targetTextView.setText ( "text" );
}
}
}
But on targetTextView.setText ( "text" );
I get this compile error:
cannot resolve method 'setText(java.lang.String)'
How I can solve above problem?
It must be targetTextView.get().setText("")
.