关于将操作栏填充到子活动的RuntimeException

时间:2016-07-04 12:13:30

标签: java android

我刚接触到android / coding / Java,如果出现错误,请轻松告诉我

Iam尝试为我的主要活动( LoginActivity )的子活动重现相同的操作栏  这是我的LoginActivity

public class LoginActivity extends AppCompatActivity {

    ActionBar actionBar;

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

        actionBar = getActionBar();// ActionBar Customisations
                actionBar.setTitle("Klok Innovations");
                actionBar.setLogo(R.drawable.collection_report);
                actionBar.setDisplayUseLogoEnabled(true);
                actionBar.setDisplayShowHomeEnabled(true);

当我打开它时应用程序崩溃...这是 logcat

07-04 17:24:15.005 7636-7636/online.klok.mobpos E/AndroidRuntime: FATAL EXCEPTION: main
  Process: online.klok.mobpos, PID: 7636
  Theme: themes:{}
  java.lang.RuntimeException: Unable to start activity ComponentInfo{online.klok.mobpos/online.klok.mobpos.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
      at android.app.ActivityThread.-wrap11(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5461)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
      at online.klok.mobpos.LoginActivity.onCreate(LoginActivity.java:28)
      at android.app.Activity.performCreate(Activity.java:6251)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5461) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

另外,如何在屏幕底部创建栏并显示在我的所有子活动中?

2 个答案:

答案 0 :(得分:0)

使用

getSupportActionBar();

而不是

getActionBar();

答案 1 :(得分:0)

这就是为什么因为你的动作栏获得空指针的例外:

试试这个

assert actionbar != null;
if(actionbar != null){
   **write your code here**
}

现在它永远不会是空的..

  1. 您正在使用appCompateActivity,因此请使用getSupportActionBar。在onCreate方法的每个subActivity中添加这一行。

         assert getSupportActionBar() != null;
       // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setTitle("Your Title Here");
    
  2. 注释行用于左上角的后退按钮..如果需要,则取消注释。并在清单中写一些行来做一些后退按钮操作。

    <activity android:name=".yourSubActivityName">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".whichActivityYouWantToGo" />
    </activity>
    
    1. 如果您在mainActivity中并希望使用对话框退出应用程序..这是代码。

      @Override
      public void onBackPressed() {
          AlertDialog.Builder builder = new AlertDialog.Builder(context);
          builder.setMessage("Are you sure you want to exit?")
                  .setPositiveButton("Yes",
                          new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int id) {
                                  dialog.dismiss();
                                  finish();
                              }
                          })
                  .setNegativeButton("No", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int id) {
                          dialog.dismiss();
                      }
                  });
          // Create the AlertDialog object and return it
          builder.create();
          builder.show();
      }