各位大家好,并提前感谢您的帮助。
我有两项活动。我在第一个活动中调用了一个对话框。 问题是,如果按下正按钮,我希望对话框重新启动活动一,但我看不出该怎么做。
这是我的代码......
活动一:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
Funciones Funciones1 = new Funciones();
Funciones1.MuestraDialogo(CluefichaActivity.this);
return true;
}
return super.onOptionsItemSelected(item);
}
活动二:
public class Funciones extends Activity
{ private static final int DIALOGO = 1;
private AlertDialog.Builder ventana;
Activity miActividad;
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOGO:
ventana = new AlertDialog.Builder(miActividad);
ventana.setIcon(miActividad.getResources().getIdentifier("my_icon", "drawable", miActividad.getPackageName()));
ventana.setTitle("title");
ventana.setMessage("message");
ventana.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int boton)
{
Intent intent = miActividad.getIntent();
miActividad.finish();
startActivity(intent);
}
});
}
ventana.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int boton)
{
}
});
return ventana.create();
}
return null;
}
... ...
public void MuestraDialogo (Activity actividad)
{
miActividad = actividad;
showDialog(DIALOGO);
}
运行时出现此错误...
Process: com.myproject.projectname, PID: 1841
java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3511)
at android.app.Activity.startActivityForResult(Activity.java:3472)
at android.app.Activity.startActivity(Activity.java:3714)
at android.app.Activity.startActivity(Activity.java:3682)
at com.myproject.projectname.Funciones$1.onClick(Funciones.java:130)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
其中“Funciones.java:130”是这行代码:“startActivity(intent);”
有人可以帮帮忙吗?谢谢!
答案 0 :(得分:2)
您需要从子活动对话框向您的主要活动发送广播(LocalBroadcast)并处理该广播以促使终止:
在您的主Activity
中,说MainActividad
static final String BROADCAST_ACTIVITY_CLOSE = "com.example.ACTIVITY_CLOSE_BROADCAST";
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(BROADCAST_ACTIVITY_CLOSE)) {
terminateCleanly();
finish();
}
}
};
然后在onCreate
:
LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BROADCAST_ACTIVITY_CLOSE);
broadcastManager.registerReceiver(broadcastReceiver, intentFilter);
保存更新清单,我认为更清洁。
您的对话框通过以下方式调用:
Intent RTReturn = new Intent(MainActividad.BROADCAST_ACTIVITY_CLOSE);
LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);
答案 1 :(得分:2)
您遇到的问题是,一旦您调用完成后,旧活动的意图为null。虽然John的答案是一种更简洁的终止方式,但您遇到的问题是再次开始活动。
尝试替换
Intent intent = miActividad.getIntent();
startActivity(intent);
与
Intent newIntent = new Intent(Funciones.this, CluefichaActivity.class);
startActivity(newIntent);