我无法在MainActivity中使用工具栏,因为它扩展了FragmentActivity,因此我尝试将其放在Fragment类中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(br.com.app.coffeehour.coffee_hour.R.layout.fragment1_menu, container, false);
mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
}
mToolbar.setTitle(null);
return view;
}
我也尝试了ActionBarActivity,但也没有成功。
以下是异常消息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.app.coffeehour.coffeehour.app/br.com.app.coffeehour.coffee_hour.navigation.ActivityOne}: java.lang.ClassCastException: br.com.app.coffeehour.coffee_hour.navigation.ActivityOne cannot be cast to android.support.v7.app.AppCompatActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassCastException: br.com.app.coffeehour.coffee_hour.navigation.ActivityOne cannot be cast to android.support.v7.app.AppCompatActivity
at br.com.app.coffeehour.coffee_hour.navigation.top_menu.fragment1_menu.onCreateView(fragment1_menu.java:33)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2442)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at android.app.Activity.performStart(Activity.java:7029)
答案 0 :(得分:0)
首先,如前所述,如果您只有一个活动,则应该扩展AppCompatActivity或Activity(最好是AppCompatActivity,因为您会具有更大的灵活性)。
如果要使用片段,则应创建一个单独的类 将此类命名为MyFragment 现在让MyFragment扩展Fragment
-如果您使用的是Android Studio,请右键单击com.whateverYourAppIsCalled文件夹,然后选择新的片段>>>片段(空白)
-然后,Android Studio的向导将完成您的大部分繁重工作,并为您提供一个准备好进行编码的新Fragment!
-Fragments总是附加到父Activity上,如果我正确理解,您应该能够进入Fragments父Activity的onCreate()方法并动态设置您的栏。
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.yourView);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.setTitle(null);
}
如果在此之后仍未显示STILL栏,则说明您的工具栏XML中有错误。
--------------或------------------
-您的工具栏需要充气(通常,如果编写工具栏而不是使用常规操作栏,则需要手动充气)。在片段内部膨胀...
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.toolbar, menu);
super.onCreateOptionsMenu(menu,inflater);
}
--------------或------------------
即使失败,也请尝试检查您的清单文件,以确保特定活动的主题支持操作栏...
例如:
<activity
android:name=".MyActivityThatNeedsAToolbar"
android:label="@string/my_Activity_That_Needs_A_Toolbar"
android:theme="Theme.AppCompat.NoActionBar"
</activity>
If your theme attribute says some variation of No_Action_Bar then change it to a theme that supports action bars!
希望这会有所帮助!
答案 1 :(得分:0)
谢谢大家,我已经找到了问题。我试图将工具栏添加到片段中,但是现在我移至MainActivity,并且可以正常工作。我不知道AppCompatActivity扩展了FragmentActivity