使用新意图调用相同的活动

时间:2013-09-04 17:13:28

标签: android android-intent android-activity dialog android-dialog

我是Android的新手,我在使用Intent时遇到了问题。我希望你能帮助我。

我想在活动中打开一个带按钮的对话框。当您按下按钮时,它会再次加载相同的活动但重置一些参数。 它就像第1轮,然后第2轮。

我有一个结束对话框,因为它调用另一个活动 第二个对话框工作正常,我在另一个类中使用show(在类partida的方法内),但问题是当下一轮的弹出窗口打开时我按下OK。我不知道我可以放在那里。 我也声明了一些静态变量和普通变量。

这是我的代码:

        partida partida;
    public String animal, oculta, vidasS, puntosS, rondaS;
    public TextView palabra, vidasrest, puntostot, rondanum;
    ImageView vida;
    Button []btn=new Button[27];
    static int vidas=5;
    static int puntos=0;
    static int ronda=1;

    static AlertDialog.Builder finalpartida, rondanueva;

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            partida=new partida();
            setContentView(R.layout.activity_partida);

            finalpartida = new AlertDialog.Builder(this);  
            finalpartida.setTitle("Final de la partida");  
            finalpartida.setMessage("Click OK para finalizar partida");            
            finalpartida.setCancelable(false);  
            finalpartida.setNegativeButton("Ok", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface finalpartida, int id) {  
                    //Creamos el Intent
                    Intent intent = new Intent(PartidaActivity.this, MainActivity.class);
                    vidas=5;
                    ronda=1;
                    puntos=0;
                    finish();
                    //Iniciamos la nueva actividad
                    startActivity(intent);
                }  
            });

            animal=partida.animal;
            oculta=partida.inicializa(animal);
            palabra = (TextView)findViewById(R.id.wordtxt);
            palabra.setText(oculta);
            vidasrest = (TextView)findViewById(R.id.txtvidas);
            vidasS = Integer.toString(vidas);
            vidasrest.setText("Vidas: "+vidasS);
            vida = (ImageView)findViewById(R.id.imgvidas);
            puntostot = (TextView)findViewById(R.id.txtPuntos);
            puntosS = Integer.toString(puntos);
            puntostot.setText("Puntos: "+puntosS);
            rondanum = (TextView)findViewById(R.id.txtronda);
            rondaS = Integer.toString(ronda);
            rondanum.setText("Ronda: "+rondaS);

            rondanueva = new AlertDialog.Builder(this);  
            rondanueva.setTitle("Fin de ronda "+ronda);  
            rondanueva.setMessage("Click OK para siguiente ronda");            
            rondanueva.setCancelable(false);  
            rondanueva.setNegativeButton("Ok", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface rondanueva, int id) {  
                    vidas=5;
                    ronda++;
//THIS DOESN'T WORK
                    Intent intent = new Intent(PartidaActivity.this, RondaActivity.class);
                    finish();
                    startActivity(intent);
                    //NEITHER THIS: ((Activity) rondanueva).setTitle("Fin de ronda "+ronda);


                    animal=partida.animal;//nombre del animal
                    oculta=partida.inicializa(animal);//estructuro palabra vacía
                    palabra.setText(oculta);
                    vidasS = Integer.toString(vidas);
                    vidasrest.setText("Vidas: "+vidasS);
                    vida.setImageResource(R.drawable.vidas5);
                    rondaS = Integer.toString(ronda);
                    rondanum.setText("Ronda: "+rondaS);

                }  
            });

        }

错误日志当我使用Intent i = getIntent(); startActivity(i);在onClick里面

09-09 17:07:09.294:W / System.err(329):java.lang.NullPointerException:asset 09-09 17:07:09.294:W / System.err(329):at android.content.res.AssetManager.readAsset(Native Method) 09-09 17:07:09.354:W / System.err(329):在android.content.res.AssetManager.access $ 700(AssetManager.java:36) 09-09 17:07:09.354:W / System.err(329):在android.content.res.AssetManager $ AssetInputStream.read(AssetManager.java:572) 09-09 17:07:09.364:W / System.err(329):at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:516) 09-09 17:07:09.364:W / System.err(329):at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479) 09-09 17:07:09.364:W / System.err(329):at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318) 09-09 17:07:09.374:W / System.err(329):at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275) 09-09 17:07:09.374:W / System.err(329):at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217) 09-09 17:07:09.374:W / System.err(329):at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:253) 09-09 17:07:09.374:W / System.err(329):at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1091) 09-09 17:07:09.374:W / System.err(329):at org.myproject.ahorcadoanimales.Xml。(Xml.java:25) 09-09 17:07:09.374:W / System.err(329):at org.myproject.ahorcadoanimales.partida。(partida.java:13) 09-09 17:07:09.394:W / System.err(329):at org.myproject.ahorcadoanimales.PartidaActivity.onCreate(PartidaActivity.java:36) 09-09 17:07:09.394:W / System.err(329):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-09 17:07:09.394:W / System.err(329):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 09-09 17:07:09.394:W / System.err(329):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 09-09 17:07:09.394:W / System.err(329):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 09-09 17:07:09.394:W / System.err(329):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 09-09 17:07:09.394:W / System.err(329):在android.os.Handler.dispatchMessage(Handler.java:99) 09-09 17:07:09.394:W / System.err(329):在android.os.Looper.loop(Looper.java:123) 09-09 17:07:09.394:W / System.err(329):在android.app.ActivityThread.main(ActivityThread.java:3683) 09-09 17:07:09.394:W / System.err(329):at java.lang.reflect.Method.invokeNative(Native Method) 09-09 17:07:09.424:W / System.err(329):at java.lang.reflect.Method.invoke(Method.java:507) 09-09 17:07:09.424:W / System.err(329):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 09-09 17:07:09.444:W / System.err(329):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-09 17:07:09.444:W / System.err(329):at dalvik.system.NativeStart.main(Native Method) 09-09 17:07:09.564:D / AndroidRuntime(329):关闭VM 09-09 17:07:09.564:W / dalvikvm(329):threadid = 1:线程退出未捕获异常(组= 0x40015560) 09-09 17:07:09.604:E / AndroidRuntime(329):致命异常:主要 09-09 17:07:09.604:E / AndroidRuntime(329):java.lang.RuntimeException:无法启动活动ComponentInfo {org.myproject.ahorcadoanimales / org.myproject.ahorcadoanimales.PartidaActivity}:java.lang.NullPointerException 09-09 17:07:09.604:E / AndroidRuntime(329):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.os.Handler.dispatchMessage(Handler.java:99) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.os.Looper.loop(Looper.java:123) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.app.ActivityThread.main(ActivityThread.java:3683) 09-09 17:07:09.604:E / AndroidRuntime(329):at java.lang.reflect.Method.invokeNative(Native Method) 09-09 17:07:09.604:E / AndroidRuntime(329):at java.lang.reflect.Method.invoke(Method.java:507) 09-09 17:07:09.604:E / AndroidRuntime(329):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 09-09 17:07:09.604:E / AndroidRuntime(329):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-09 17:07:09.604:E / AndroidRuntime(329):at dalvik.system.NativeStart.main(Native Method) 09-09 17:07:09.604:E / AndroidRuntime(329):引起:java.lang.NullPointerException 09-09 17:07:09.604:E / AndroidRuntime(329):at org.myproject.ahorcadoanimales.partida.inicializa(partida.java:19) 09-09 17:07:09.604:E / AndroidRuntime(329):at org.myproject.ahorcadoanimales.PartidaActivity.onCreate(PartidaActivity.java:66) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-09 17:07:09.604:E / AndroidRuntime(329):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 09-09 17:07:09.604:E / AndroidRuntime(329):... 11更多

2 个答案:

答案 0 :(得分:0)

您是否看不到弹出对话框的问题?如果没有,你能解释究竟是什么问题吗?

答案 1 :(得分:0)

如果每次尝试启动活动时应用程序崩溃,很可能是您尝试启动的活动未在AndroidManifest.xml文件中正确声明。

https://developer.android.com/reference/android/app/Activity.html