gpt4 book ai didi

android - setSupportActionBar() 抛出空指针异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:43 26 4
gpt4 key购买 nike

我是 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。

样式.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");
....

}

我搜索了这个问题,但没有找到任何解决方案。希望有人能认识到这个错误并快速回答 - 否则我可以根据要求提供更多信息。我们将不胜感激。

最佳答案

在添加任何 View(例如 Toolbar)之前,您需要调用 setContentView

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // <-- call this first

toolbar = (Toolbar) findViewById(R.id.toolbar); // <-- setup views after

setSupportActionBar(toolbar);
setTitle("DDIT_Results");
}

请在此处查看官方 Android 开发者博客:http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

操作栏

To use Toolbar as an Action Bar, first disable the decor-provided Action Bar. The easiest way is to have your theme extend from Theme.AppCompat.NoActionBar (or its light variant).

Second, create a Toolbar instance, usually via your layout XML:

<android.support.v7.widget.Toolbar
android:id=”@+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/colorPrimary” />

The height, width, background, and so on are totally up to you; these are just good examples. As Toolbar is just a ViewGroup, you can style and position it however you want.

Then in your Activity or Fragment, set the Toolbar to act as your Action Bar:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);

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

From this point on, all menu items are displayed in your Toolbar, populated via the standard options menu callbacks.

关于android - setSupportActionBar() 抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29393169/

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