从一个活动转到另一个活动时出错

时间:2017-11-04 15:36:41

标签: android android-intent onclick onclicklistener calculator

当我尝试从一个活动转到另一个活动时,应用程序崩溃了。 这是代码。我以前曾经使用过意图从一个活动转到另一个活动所以我知道代码是正确的。我的应用程序是一个简单的计算器,但当用户键入一个秘密密码应用程序从计算器到另一个活动

equal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            val2 = Integer.parseInt(temp);
            sp = getSharedPreferences("passinfo", Context.MODE_PRIVATE);
            if(temp.equals(sp.getString("pass",""))){
                Intent i = new Intent(MainActivity.this,Privatefolder.class);
                startActivity(i);

            }

            else if(val1 == null || val2 == null){
                Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
            }else{
                if(operator.equals("+") ){
                    result = val1 + val2;
                }
                if(operator.equals("-")){
                    result = val1 - val2;
                }
                if(operator.equals("*")){
                    result = val1 * val2;
                }
                if(operator.equals("/")){
                    result = val1/val2;
                }
                temp1 = temp1 +" = " +result.toString();
                t1.setText(temp1);
                temp="";temp1="";
            }
        }
    });

我还在清单文件中注册了活动

<activity android:name=".Privatefolder"></activity>

我也在使用像这样的onclick方法。这是否必须与应用程序崩溃做一些事情?当用户点击任何名为0到9的按钮时,此方法将运行。

  protected void onClicknumbers(View v){
    Button b = (Button) v;
    if(temp1.equals("")) {
        temp += b.getText();
        t2.setText(temp);
    }
    else{
        temp += b.getText().toString();
        temp1 += b.getText();
        t1.setText(temp1);
    }

}

这是我得到的错误

11-04 20:44:22.744 16989-16989/com.dreamfighter.vault E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.dreamfighter.vault, PID: 16989
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamfighter.vault/com.dreamfighter.vault.Privatefolder}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                        at android.os.Looper.loop(Looper.java:207)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                        at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:204)
                                                                        at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
                                                                        at com.dreamfighter.vault.Privatefolder.onCreate(Privatefolder.java:18)
                                                                        at android.app.Activity.performCreate(Activity.java:6309)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
                                                                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                        at android.os.Looper.loop(Looper.java:207) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5728) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

1 个答案:

答案 0 :(得分:0)

如果是这种情况,您似乎在Privatefolder活动中使用自己的自定义工具栏,那么您必须为您的应用声明一个不说明操作栏的主题

所以转到 res / values / styles.xml 并打开此文件夹,您将在顶部看到类似的内容

<style name="AppTheme" parent="something here">

(这里的东西)是你会找到的默认主题。

将行更改为此

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
相关问题