Java 为什么这个静态字段为空?
在android上。 我的活动是这样的Java 为什么这个静态字段为空?,java,android,nullpointerexception,broadcastreceiver,Java,Android,Nullpointerexception,Broadcastreceiver,在android上。 我的活动是这样的 public class MainActivity extends Activity { static TextView textview; static int aaa = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().setSoft
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);
//其他方法,如startservice()等
还有一个广播接收器,用于接收来自服务的标志
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);
像这样,在onReceiver中,textview为null。
作为测试,int aaa不是空的
为什么textview为空
编辑
texttiewxml是
<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" />
使用相同textview.settext的活动中的和方法不为null。
谢谢
编辑2
我知道我应该保存这个变量。
那么,保存视图变量的最佳方法是什么?
SharedReferences似乎无法保存视图变量。这可能是因为您没有引用正确的textview元素。仔细检查布局文件中的ID。这可能是因为您没有引用正确的textview元素。仔细检查布局文件中的ID。此线程表示存在静态字段可能是个问题。 一般来说,android会杀死你的应用程序。。返回时可能不会设置静态变量
此线程表示存在静态字段可能是个问题。 一般来说,android会杀死你的应用程序。。返回时可能不会设置静态变量
您的布局XML是什么样子的?您的布局XML是什么样子的?哦,,,我忘了我在使用webview和VIEW.Goe。哦,,,我忘了我在使用webview和VIEW.Goe。