I created a (very simple) class wich gets a string value from an intent. If i create a Toast with the result, it’s oké. But when i try to set the text of the TextView the program stops with a NullPointerException. Eclipse gives a warning on:
TextView debugView = (TextView) findViewById(R.id.debugView);
The local variable debugView is never read
I hope someone has an idea.
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);
}
}
}
Where i get the result from:
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();
}
}
XML From Main:
Logcat doesn’t give me any info at all, just the nullPointer. I can’t post a logcat right now because the emulator gives me some more trouble (after this briljant update >:(). Will try again when needed.
You are creating the debugView inside your oncreate which makes it a variable that only can be used there. Change
to