【发布时间】: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