首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我实现setSupportActionBar方法时,我的应用程序崩溃了

当我实现setSupportActionBar方法时,我的应用程序崩溃了
EN

Stack Overflow用户
提问于 2016-04-28 13:59:10
回答 1查看 329关注 0票数 0

问题是,如果我将setSupportActionBar (工具栏)从add_event活动中删除,它就会正常工作,add_event活动会打开并显示屏幕上的所有按钮和textEdits,但不会在工具栏上显示任何按钮,只显示工具栏上的任何按钮,而如果我将该setSupportActionBar方法放入其中,当我调用活动(add_event)时,它会崩溃。

有人能解释一下发生了什么吗?(预先谢谢:)

我在这里放了一些代码,但是你们可以在我的GitHub里下载整个项目

https://github.com/dennysfarias/RemindIt

代码语言:javascript
运行
复制
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //setting toolbar
    Toolbar toolbar = (Toolbar)findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);
}


public void callactAddEvt(){
    Intent callAddEvt = new Intent(MainActivity.this, add_event.class);
    MainActivity.this.startActivity(callAddEvt);
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}


//put the options inside the 3dot menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            // User chose the "Settings" item, show the app settings UI...
            Toast.makeText(getApplicationContext(), "Settings Option Pressed", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.action_addevent:
            //User chose the AddEvent action, whom calls the com.ddtec.remindit.add_event activity
            callactAddEvt();
            Toast.makeText(getApplicationContext(), "Add Event Option Pressed", Toast.LENGTH_SHORT).show();

            return true;
        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            Toast.makeText(getApplicationContext(), "No Option Pressed", Toast.LENGTH_SHORT).show();

            return super.onOptionsItemSelected(item);

    }
}
}

这是我的MainActivity --它工作得很好,我在我的手机上试了一下,它看起来很棒。

现在我要展示我的第二次活动

代码语言:javascript
运行
复制
public class add_event extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_event);

    Toolbar toolbar = (Toolbar)findViewById(R.id.my_toolbar2);
    setSupportActionBar(toolbar);
}


@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_add_event, menu);
    return true;
}




@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_addfinish:
            // User chose the Done action, saving the event...
            Toast.makeText(getApplicationContext(), "Finish/Save Option Pressed", Toast.LENGTH_SHORT).show();

            return true;

        case R.id.action_cancel:
            // User chose the Cancel action, leaving without saving the current event...
            Toast.makeText(getApplicationContext(), "Cancel Option Pressed", Toast.LENGTH_SHORT).show();

            return true;

        case R.id.action_settings:
            Toast.makeText(getApplicationContext(), "Settings Option Pressed", Toast.LENGTH_SHORT).show();

            return true;


        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            Toast.makeText(getApplicationContext(), "No Option Pressed", Toast.LENGTH_SHORT).show();

            return super.onOptionsItemSelected(item);

    }
}
}

我的add_event活动布局文件

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".add_event">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar2"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"/>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/my_toolbar2">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:hint="Event Name"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="37dp"
            android:layout_margin="10dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:hint="Event Location"
            android:layout_below="@+id/editText"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp" />

        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:ems="10"
            android:id="@+id/editText3"
            android:layout_alignParentStart="true"
            android:hint="Event Date"
            android:layout_below="@+id/editText2"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp" />

        <EditText
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:inputType="time"
            android:ems="10"
            android:id="@+id/editText4"
            android:hint="Event Time"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_alignTop="@+id/editText3"
            android:layout_alignEnd="@+id/editText2" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:inputType="textMultiLine"
            android:ems="10"
            android:id="@+id/editText5"
            android:hint="Event Description"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/editText4"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp" />
    </RelativeLayout>


</RelativeLayout>

在这里我的Style.xml

代码语言:javascript
运行
复制
<resources>


<!-- Base application theme. -->
<style name="AppThemeWithNoAB" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/my_primary</item>
    <item name="colorPrimaryDark">@color/my_primary_dark</item>
    <item name="colorAccent">@color/my_accent</item>
    <item name="android:textColorPrimary">@color/my_primary_text</item>
    <item name="android:textColorSecondary">@color/my_secondary_text</item>
    <item name="android:windowBackground">@color/window_background</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>


</style>

罗格猫

代码语言:javascript
运行
复制
04-27 14:13:30.197 241-519/? D/RouteManager: doReconsiderRouting: DONE
04-27 14:13:30.197 241-519/? D/EVENT_THREAD: void CEventThread::run() Do poll with timeout: -1
04-27 14:13:30.197 241-520/? E/TinyAlsaStream: getFramesAvailable: Unable to get available frames
04-27 14:13:30.197 241-520/? E/TinyAlsaStream: getFramesAvailable: Unable to get available frames
04-27 14:13:30.197 241-520/? E/TinyAlsaStream: getFramesAvailable: Unable to get available frames
04-27 14:13:30.197 241-520/? E/TinyAlsaStream: getFramesAvailable: Unable to get available frames
04-27 14:13:30.207 525-1054/system_process V/WindowManager: Adding window Window{30cae9cd u0 com.ddtec.remindit/com.ddtec.remindit.add_event} at 8 of 27 (after Window{368d2628 u0 com.ddtec.remindit/com.ddtec.remindit.MainActivity})
04-27 14:13:30.327 11735-11735/com.ddtec.remindit D/AndroidRuntime: Shutting down VM
04-27 14:13:30.327 11735-11735/com.ddtec.remindit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.ddtec.remindit, PID: 11735
                                                                    android.view.InflateException: Couldn't resolve menu item onClick handler ClearAll in class com.ddtec.remindit.add_event
                                                                        at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:242)
                                                                        at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:443)
                                                                        at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479)
                                                                        at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
                                                                        at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
                                                                        at com.ddtec.remindit.add_event.onCreateOptionsMenu(add_event.java:25)
                                                                        at android.app.Activity.onCreatePanelMenu(Activity.java:3632)
                                                                        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341)
                                                                        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                                                                        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                        at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                                                                        at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5310)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
                                                                     Caused by: java.lang.NoSuchMethodException: ClearAll [interface android.view.MenuItem]
                                                                        at java.lang.Class.getMethod(Class.java:664)
                                                                        at java.lang.Class.getMethod(Class.java:643)
                                                                        at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:240)
                                                                        at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:443) 
                                                                        at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479) 
                                                                        at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196) 
                                                                        at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118) 
                                                                        at com.ddtec.remindit.add_event.onCreateOptionsMenu(add_event.java:25) 
                                                                        at android.app.Activity.onCreatePanelMenu(Activity.java:3632) 
                                                                        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341) 
                                                                        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                                                                        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                                                                        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                                                                        at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454) 
                                                                        at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61) 
                                                                        at android.os.Handler.handleCallback(Handler.java:739) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                        at android.os.Looper.loop(Looper.java:135) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5310) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
                                                                        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114) 
04-27 14:13:30.327 525-1034/system_process W/ActivityManager:   Force finishing activity 1 com.ddtec.remindit/.add_event
04-27 14:13:30.337 525-1034/system_process W/ActivityManager:   Force finishing activity 2 com.ddtec.remindit/.MainActivity
04-27 14:13:30.357 186-186/? I/upi_ug31xx: [get_daemon_uevent_request]: uevent_request = 0 (0000)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-28 14:08:06

在您的menu_add_event.xml在线编号:13中,您添加了“android:onClick=”“ClearAll”。这就是问题的根源。要么删除该行,要么在活动中添加以下函数。

代码语言:javascript
运行
复制
public boolean ClearAll(MenuItem item) { 
// actions 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36916942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档