gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:59:36 35 4
gpt4 key购买 nike

好吧,问题是,如果我从 add_event Activity 中删除 setSupportActionBar(toolbar) ,它就会起作用,add_event Activity 将打开并在屏幕上显示所有按钮和文本编辑,但不在工具栏上显示任何按钮,只是工具栏,干净,如果我放置 setSupportActionBar 方法,当我调用 Activity (add_event) 时它会崩溃。

有人可以解释一下发生了什么吗?预先感谢:)

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

https://github.com/dennysfarias/RemindIt

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,它工作正常,我在手机上尝试了这个,看起来很棒。

现在我要展示我的第二个 Activity

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 Activity 布局文件

<?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

<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>

日志猫

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)

最佳答案

在第 13 行的 menu_add_event.xml 中,您添加了“android:onClick="ClearAll"”。这就是导致问题的原因。删除该行或在您的 Activity 中添加以下函数。

public boolean ClearAll(MenuItem item) { 
// actions
}

关于java - 当我实现 setSupportActionBar 方法时,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36916942/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com