2

on android. my activity is like this.

public class MainActivity extends Activity {

   static TextView textview;
   static int aaa = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    setContentView(R.layout.activity_main);

             textview = (TextView)findViewById(R.id.textView6);

//other method,startservice()and so on.

and there is BroadcastReceiver to receive a flag from service.

     public static class Receiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intents) {
        intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        textview.setText("set")//throw null point
                    Log.d("aaa","aaa is" + aaa);

like this, in onReceiver, textview is null. as test, int aaa is'nt null.

why textview is null?

EDIT Texttiew xml is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adview"
        android:layout_alignParentLeft="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

And method in Activity using same textview.settext isnt null. Thanks.

EDIT2 I can know i should save this variable. So, whats the best way to save VIEW variable? SharedPreferences seem not able to save VIEW variable.

user2027811
  • 4,691
  • 2
  • 14
  • 10

2 Answers2

1

This thread indicates that having static fields might be a problem. In general, android kills your app.. and on return static variables may not be set.

static variable null when returning to the app

Community
  • 1
  • 1
smk
  • 5,340
  • 5
  • 27
  • 41
0

This is likely that you are not referencing the correct textview element. Double check your IDs in your layout file.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45