我是Android新手,正在按照以下教程学习Material Design Toolbar:
http://www.android4devs.com/2014/12/how-to-make-material-design-app.html
但是在代码实现后,logcat中显示了以下错误:
04-01 19:16:10.214 2246-2253/com.example.bhaskar.ddit_results E/art﹕ Failed sending reply to debugger: Broken pipe
04-01 19:16:11.985 2246-2246/com.example.bhaskar.ddit_results E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.bhaskar.ddit_results, PID: 2246
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bhaskar.ddit_results/com.example.bhaskar.ddit_results.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95)
at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88)
at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:84)
at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175)
at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
at com.example.bhaskar.ddit_results.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
这是由NullPointerException引起的问题。我正在使用Android Studio 1.0.1。
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#ff32cd32</item>
<item name="colorPrimaryDark">#ff04cd2c</item>
<item name="colorAccent">#ff4081</item>
</style>
v21/styles.xml:
<resources>
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
在 toolbar.xml 中,我有以下内容:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
同时,我已经将工具栏布局包含在activity_main.xml文件中。 在MainActivity.java中:
public class MainActivity extends ActionBarActivity {
....
private Toolbar toolbar;
public static final int RECORD_ENTRY_REQUEST_CODE=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set our view from the "main" layout resource
toolbar = (Toolbar)findViewById(R.id.toolbar);
//Toolbar will now take on default actionbar characteristics
setContentView(R.layout.activity_main);
setSupportActionBar(toolbar);
setTitle("DDIT_Results");
....
}
我搜索了这个问题,但没有找到任何解决方案。希望有人能识别出这个错误并快速回答,否则如果需要我可以提供更多信息。感谢任何帮助。
setContentView
。 - nikis