【问题标题】:I'm getting the following runtime error. Please help me figure out why我收到以下运行时错误。请帮我找出原因
【发布时间】:2015-10-03 01:00:35
【问题描述】:

我正在使用 Material Design 开发一个应用程序。

运行以下代码并点击菜单中的“关于”选项后,应用程序崩溃了,我遇到了以下异常:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.abc.xyz/com.abc.xyz.AboutActivity}:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)”`

这是我的 AboutActivity.java 文件的代码:

public class AboutActivity extends AppCompatActivity {

    public Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        SpannableString s = new SpannableString("About");
        s.setSpan(new TypefaceSpan(this, "Pacifico.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(s);
    }

    @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_about, 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);
    }
}

由于我是材料设计的新手,我真的不知道在这里做什么。

【问题讨论】:

标签: java android nullpointerexception runtimeexception


【解决方案1】:

您是否在“activity_about.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” />

您还有一个工具栏工具栏;变量是多余的。如果您不使用它,请将其删除。

尝试: 移除对 getsupportactionbar 的调用。

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setHomeButtonEnabled(true);

【讨论】:

  • 谢谢,你的回答给了我一个提示。我现在想通了!
【解决方案2】:

它为空,因为您在布局中使用了工具栏(我在您的代码中看到了一个 Toobar 对象)。这意味着,您的主题可能是.NoActionBar 主题。因此,当您使用工具栏时,您必须将该工具栏设置为ActionBar。然后你可以使用ActionBar 并应用你想要调用的任何方法。您可以使用以下代码 sn-p 初始化 Toolbar。

private void initToolbar() {
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final ActionBar actionBar = getSupportActionBar();

    if (actionBar != null) {
        actionBar.setTitle("Your title");
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

【讨论】:

    【解决方案3】:

    通常,空指针异常仅表明您正在尝试访问尚未定义或初始化的项目。试试这个

    ActionBar actionBar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    

    您应该在此处粘贴您的布局文件以提供更多详细信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2022-06-13
      • 2014-09-23
      • 1970-01-01
      • 2020-03-26
      • 2022-10-06
      • 1970-01-01
      相关资源
      最近更新 更多