我有三项活动。我将一个对象从一个活动发送到第二个活动。我使用onActivityResult从第二个活动检索数据。然后我需要将我检索到的数据发送到第三个活动,但我有一个例外。
主要活动(问题是实施parcelable的类。您可以在其他问题First question中看到:
public class MainActivity extends Activity {
Tgestion Tges= new Tgestion();
Button buttonI,buttonM,buttonB,buttonD,buttonS;
static final int RECUPERAR_GESTOR = 1;
static final int RECUPERAR_MODIFICAR=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
buttonI = (Button) findViewById(R.id.buttonIntroducir);
buttonI.setEnabled(true);
buttonI.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, IntroducirPatron.class);
intent.putExtra("com.example.sistemacontrasena.gestion", Tges);
//startActivity(intent);
startActivityForResult(intent, RECUPERAR_GESTOR);
}
});
buttonM = (Button) findViewById(R.id.buttonModificar);
buttonM.setEnabled(false);
buttonM.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent2 = new Intent(context, ModificarPatron.class);
intent2.putExtra("com.example.sistemacontrasena.mod", Tges);
startActivityForResult(intent2, RECUPERAR_MODIFICAR);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == RECUPERAR_GESTOR) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
this.Tges=data.getParcelableExtra("com.example.sistemacontrasena.result_gestion");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
}
在第二个活动中我放了setResult:
Intent returnIntent = new Intent();
returnIntent.putExtra("com.example.sistemacontrasena.result_gestion", this.gestion);
setResult(RESULT_OK, returnIntent);
return maximo;
直到这里一切都是正确的 在第三个活动中我使用:
Tgestion gestion2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modificar_patron);
this.gestion2=getIntent().getParcelableExtra("com.example.sistemacontrasena.mod");
}
但是当我在第三个活动中调用question2的函数时,例如:
String[] prueba= new String[2];
prueba=this.gestion2.getSecret();
System.out.println(prueba[0])
我有一个例外:
06-15 13:37:44.852: E/AndroidRuntime(32262): FATAL EXCEPTION: main
06-15 13:37:44.852: E/AndroidRuntime(32262): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sistemacontrasena/com.example.sistemacontrasena.ModificarPatron}: java.lang.NullPointerException
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.os.Looper.loop(Looper.java:138)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.app.ActivityThread.main(ActivityThread.java:3701)
06-15 13:37:44.852: E/AndroidRuntime(32262): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 13:37:44.852: E/AndroidRuntime(32262): at java.lang.reflect.Method.invoke(Method.java:507)
06-15 13:37:44.852: E/AndroidRuntime(32262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
06-15 13:37:44.852: E/AndroidRuntime(32262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
06-15 13:37:44.852: E/AndroidRuntime(32262): at dalvik.system.NativeStart.main(Native Method)
06-15 13:37:44.852: E/AndroidRuntime(32262): Caused by: java.lang.NullPointerException
06-15 13:37:44.852: E/AndroidRuntime(32262): at com.example.sistemacontrasena.ModificarPatron.onCreate(ModificarPatron.java:35)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 13:37:44.852: E/AndroidRuntime(32262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
06-15 13:37:44.852: E/AndroidRuntime(32262): ... 11 more
我需要通过主要活动使用从第二个活动检索到第三个活动的信息。
答案 0 :(得分:0)
您在第3个活动中的gestion2引用上获得了nullpointerexception。 getParcelableExtra(" com.example.sistemacontrasena.mod" )中使用的密钥不包含任何值,因为您还没有为此密钥分配任何值。
只需确保您用于检索值的密钥已经在调用活动意图中分配了一些值。
如果您调用任何方法的任何引用/对象为null,请输入正确的日志以检查空值。