2017-04-06 56 views
0

まず最初に、この質問が以前に尋ねられたことを知っています。私は他の解決策を探して一時間を費やしました。ほとんどはgetActionBar() returns nullからです。上記を拡張getSupportActionBar()。setDisplayHomeAsUpEnabled(true); throws NullPointerException

public class NavigationDrawer extends AppCompatActivity { 

String[] mDrawerTitles; 
DrawerLayout mDrawerLayout; 
ListView mDrawerList; 
ActionBarDrawerToggle mDrawerToggle; 
String activityTitle; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.navigation_drawer_layout); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    mDrawerTitles = getResources().getStringArray(R.array.drawer_array); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer); 
    activityTitle = getTitle().toString(); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //here is where the error is thrown 
    getSupportActionBar().setHomeButtonEnabled(true); 

    addDrawerItems(); 
    navDrawerSetup(); 

} 

MainActiviy:他のクラスから継承

NavigationDrawerクラス

public class MainActivity extends NavigationDrawer { 

int[]images={R.drawable.user_manual,R.drawable.tips_tricks, R.drawable.troubleshooting}; 
Integer printer; 
ListView myListView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View contentView = inflater.inflate(R.layout.activity_main, null, false); 
    mDrawerLayout.addView(contentView, 0); 
} 

ナビゲーションドロワーが

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<ListView 
    android:id="@+id/left_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#999" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" /> 


</android.support.v4.widget.DrawerLayout> 

のstyles.xml

を.XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/content_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.shelleyd.myapplication.MainActivity" 
tools:showIn="@layout/activity_main"> 

<ListView 
    android:id="@+id/myList" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" /> 

</LinearLayout> 

答えて

1

活動NavigationDrawerでcontent_main

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.example.shelleyd.myapplication.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

activity_mainあなたは setContentView(R.layout.navigation_drawer_layout)を実行している、と navigation_drawer_layoutで例外につながる、何も任意の Toolbarはありません。

+0

ええ、ありがとうございました。アプリは今開きますが、ナビの引き出しは表示されません。 – Norno3

+0

あなたは継承を悪用しています。これらの活動を分離する。それはあなたのコードクリーナーとあなたの人生を簡単になります。 – azizbekian

+0

ああ、私はちょうどこれを続けていた - http://stackoverflow.com/a/25254794と私は同じそれをやったと思った。私は今、すべてのアクティビティでnav引き出しコードに戻ります。 – Norno3

1

R.id.toolbar IDを使用してR.layout.activity_mainにはビューがありませんので、あなたは、アクティビティツールバーがnullに設定されているNavigationDrawer.class

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //This is null 
setSupportActionBar(toolbar); 

あなたに次行います。したがって、上記のコードの後に​​ツールバーを設定しようとすると、NULLオブジェクトにプロパティを設定しようとしています。そのオブジェクトはNULLポインタ例外を生成します。

アクティビティの上の2行を削除するか、R.layout.activity_mainレイアウトファイルのR.id.toolbar IDを含むツールバービューを宣言します。そうすれば問題は解決します。

関連する問題