Android setText after ActivityResult强制以NullPointerException关闭

Android setText after ActivityResult强制以NullPointerException关闭,android,sdk,Android,Sdk,我创建了一个(非常简单的)类,它从一个intent中获取字符串值。如果我制作了一个带有结果的祝酒词,就可以了。但是,当我试图设置TextView的文本时,程序会以NullPointerException停止。Eclipse在以下方面发出警告: TextView debugView = (TextView) findViewById(R.id.debugView); 从不读取局部变量debugView 我希望有人有主意 public class Main extends Activity {

我创建了一个(非常简单的)类,它从一个intent中获取字符串值。如果我制作了一个带有结果的祝酒词,就可以了。但是,当我试图设置TextView的文本时,程序会以
NullPointerException
停止。Eclipse在以下方面发出警告:

TextView debugView = (TextView) findViewById(R.id.debugView);
从不读取局部变量debugView

我希望有人有主意

public class Main extends Activity {

TextView debugView;
Button chooseOne;

Intent myIntent;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView debugView = (TextView) findViewById(R.id.debugView);
    //debugView.setText("Hola supermercado!");

    Button ChooseOne = (Button) findViewById(R.id.chooseOne);

    ChooseOne.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            myIntent = new Intent(Main.this, ResultFromSelection.class);
            myIntent.putExtra("sampleData", "This is Sample Data");
            startActivityForResult(myIntent, 1);

        }
    });

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == 1) {

        String msg = data.getStringExtra("returnedData");

        //Toast.makeText(Main.this, ""+msg, Toast.LENGTH_LONG).show();
        debugView.setText(""+msg);

    }
}

}
我的结果来自:

public class ResultFromSelection extends Activity {

 Intent intent;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selection);

    Intent intent= getIntent();
      intent.putExtra("returnedData", "A Value");
      setResult(RESULT_OK, intent);
    finish();
}

 }
来自Main的XML:


Logcat根本没有给我任何信息,只是空指针。我现在无法发布logcat,因为模拟器给了我更多的麻烦(在这次briljant更新之后>:()。将在需要时重试。

您正在oncreate内部创建debugView,这使它成为一个只能在那里使用的变量。更改

TextView debugView = (TextView) findViewById(R.id.debugView);


onActivityResult中没有debugView。类中是否还有名为debugView的字段?因为您正在onCreate中声明名为debugView的局部变量。

您在一个方法中声明了debugView,然后尝试在另一个方法中引用它。您需要将变量声明移到onCreate()之外方法。

难以置信,但这就是答案。我使用这种声明方式从未遇到任何问题。非常感谢。所有人。
this.debugView = (TextView) findViewById(R.id.debugView);