我绝对是Android的初学者。现在我开始学习android。现在我正在使用工具栏创建自定义操作栏。我尝试在一篇在线文章中引用它。但这是在抛出错误。我的代码出了什么问题?
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
这是主题
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
这是我的action_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/my_action_bar_toolbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/orange"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@drawable/icon_1"
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
这是我的MainActivity.java类
公共类MainActivity扩展了ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar actionBarToolBar = (Toolbar)findViewById(R.id.my_action_bar_toolbar);
setSupportActionBar(actionBarToolBar);
actionBarToolBar.setNavigationIcon(R.drawable.icon_3);
actionBarToolBar.setNavigationContentDescription("This is navigation content description");
actionBarToolBar.setLogo(R.drawable.icon_2);
actionBarToolBar.setLogoDescription("This is logo description");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
当我运行它时,它只会一直抛出错误。
这是我在Logcat中得到的。
01-18 16:47:46.199 5584-5584/? D/dalvikvm: Late-enabling CheckJNI
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog D/AndroidRuntime: Shutting down VM
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa61c5908)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: FATAL EXCEPTION: main
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toolbardog.toolbardog/com.example.toolbardog.toolbardog.MainActivity}: java.lang.NullPointerException
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: Caused by: java.lang.NullPointerException
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:84)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at com.example.toolbardog.toolbardog.MainActivity.onCreate(MainActivity.java:17)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5104)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-18 16:47:46.359 5584-5584/com.example.toolbardog.toolbardog E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你应该扩展AppCompactivity
。当您为活动设置不同的布局时,工具栏位于action_toolbar.xml
。在您的情况下,自然工具栏为空。您需要在activity_main.xml
。
只需在您的activity_main.xml
中添加<include layout="@layout/action_toolbar />
即可