我没有按下向上按钮(实际上activity_order活动无法启动)。当我单击MainActivity应用栏中的图标(+)时,它会崩溃
我的直觉是
有问题getSupportActionBar().setDisplayHomeAsUpEnabled(true);
这是第二项活动
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class activity_order extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
这是主要/家长活动
import android.content.Intent;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar= findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(R.id.action_create_order==item.getItemId()) {
Intent intent = new Intent(this, activity_order.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
错误日志
2019-06-18 22:51:49.374 4127-4147/com.example.orderpizza I/Adreno: QUALCOMM build : f47ef91, I09d312ff84
Build Date : 07/24/17
OpenGL ES Shader Compiler Version: XE031.09.00.04
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.UM.5.5.R1.07.01.02.269.046
Remote Branch : NONE
Reconstruct Branch : NOTHING
2019-06-18 22:51:49.383 4127-4147/com.example.orderpizza I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-18 22:51:49.383 4127-4147/com.example.orderpizza D/OpenGLRenderer: Swap behavior 1
2019-06-18 22:51:49.418 4127-4127/com.example.orderpizza W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
2019-06-18 22:51:52.150 4127-4127/com.example.orderpizza V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@7d39c7c
2019-06-18 22:51:52.269 4127-4127/com.example.orderpizza I/Timeline: Timeline: Activity_launch_request time:28140110 intent:Intent { cmp=com.example.orderpizza/.activity_order }
2019-06-18 22:51:52.317 4127-4127/com.example.orderpizza D/AndroidRuntime: Shutting down VM
2019-06-18 22:51:52.318 4127-4127/com.example.orderpizza E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.orderpizza, PID: 4127
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.orderpizza/com.example.orderpizza.activity_order}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2818)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6393)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.orderpizza.activity_order.onCreate(activity_order.java:16)
at android.app.Activity.performCreate(Activity.java:6858)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2710)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2818)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6393)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
2019-06-18 22:51:52.337 4127-4127/com.example.orderpizza I/Process: Sending signal. PID: 4127 SIG: 9
请提供帮助,因为我没有得到为什么它会显示nullPointerException。
答案 0 :(得分:1)
我认为您的 activity_order 布局没有ID为 toolbar_main 的任何工具栏 xml 请重新检查... 您至少应该有一个appbarlayout和一个工具栏
<android.support.design.widget.AppBarLayout
android:id="@+id/my_appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_margin="0dp"
app:layout_scrollFlags="snap"
app:navigationIcon="@drawable/ic_arrow_back_black_24dp"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:theme="@style/ThemeToolbar.default" />
</android.support.design.widget.AppBarLayout>
答案 1 :(得分:0)
您可能未使用支持库,如果要导入android.app.ActionBar
,则应使用getActionBar()代替。或仅导入android.support.v7.app.ActionBar
。
为了您的安全,请务必检查getSupportActionBar()!=是否也为null