3

私はこのエラーに直面しています。私は多くの時間を費やしましたが、悲しいことです!私はこのエラーで立ち往生しています。私は私のアプリでアクションバーを追加したいが、それは私にこのエラーを表示し続けているgetSupportActionBar()、誰か私を案内してください。nullオブジェクトリファレンスで仮想メソッド 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)'を呼び出そうとしました

FATAL EXCEPTION: main 
Process: com.technerdshub.vusocial, PID: 20633 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.technerdshub.vusocial/com.technerdshub.vusocial.Activities.StudentDashboardActivity}: 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:2693) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:5942) 
    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:1389) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
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.technerdshub.vusocial.Activities.StudentDashboardActivity.onCreate(StudentDashboardActivity.java:50) 
      

私のjavaファイルには、次のとおりです。

package com.technerdshub.vusocial.Activities; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.Toolbar; 
import android.support.v7.app.AppCompatActivity; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.technerdshub.vusocial.Fragments.TaskFragment; 
import com.technerdshub.vusocial.Fragments.dummy.DummyContent; 
import com.technerdshub.vusocial.R; 

import java.util.ArrayList; 
import java.util.List; 

public class StudentDashboardActivity extends AppCompatActivity { 

    //private Toolbar toolbar; 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     displayLoginActivity(); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_student_dashboard); 

//  Parse.initialize(new Parse.Configuration.Builder(this) 
//      .applicationId("J5CIV2z6xeSCXDqdOfhE0kPSikvRFPyDyOJxqJNx") 
//      .clientKey("bEWm4nWtWiBrMczGfOvA7s4Ulr2bAU3W3TtVSLDf") 
//      .build() 
//  ); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setElevation(2); 


     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 

//  ParseObject testObject = new ParseObject("Task"); 
//  testObject.put("marks", 99); 
//  testObject.saveInBackground(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFragment(new TaskFragment(), "Quiz"); 
     adapter.addFragment(new TaskFragment(), "Assignment"); 
     adapter.addFragment(new TaskFragment(), "GDP"); 
     viewPager.setAdapter(adapter); 
    } 



    private void displayLoginActivity() { 

     Intent i = new Intent(this, Login.class); 
     startActivity(i); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "StudentDashboard Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.technerdshub.vusocial.Activities/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "StudentDashboard Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.technerdshub.vusocial.Activities/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<TaskFragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

     @Override 
     public TaskFragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFragment(TaskFragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 


    } 

} 

答えて

2

getSupportActionBar()を使用する前にActionBarを設定する必要があります。

、そう、あなたのレイアウトにツールバーを追加、 Toolbar t = (Toolbar) findViewById(R.id.toolbar) でそれを見つけて、それが動作するはず方法にsetSupportActionBar(t);

を呼び出します。

+0

このメソッドは実際に動作しています。 –

3

あなたはおそらくあなたのstyles.xmlでNoActionBarテーマがこの活動に適用されてきました。

これにあなたのstyles.xmlであなたのAppThemeを変更

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light"> 
    <!-- Customize your theme here. --> 
</style> 

をそして、あなたの活動はこのテーマを継承することを確認してください。

関連する問題

 関連する問題