0

我想讓我的HomeActivity擴展爲BaseActivity(建議here),因此我可以在每個需要的活動中使用相同的導航抽屜。但是我在HomeActivity的onCreate方法中得到了一個NullPointerException。這是BaseActivity的代碼:通過擴展BaseActivity在不同的活動中使用導航抽屜

import android.os.Bundle; 
import android.app.Activity; 
import android.content.res.Configuration; 
import android.support.v4.app.ActionBarDrawerToggle; 
import android.support.v4.widget.DrawerLayout; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class BaseActivity extends Activity 
{ 
    public DrawerLayout drawerLayout; 
    public ListView drawerList; 
    private String[] drawerListEntries; 
    private ActionBarDrawerToggle drawerToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 

     drawerListEntries = new String[] {"Bla bla", "Profile", "Home"}; 

     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_navigation_drawer, 0, 0) 
     { 
      public void onDrawerClosed(View view) 
      { 
       getActionBar().setTitle(R.string.app_name); 
      } 

      public void onDrawerOpened(View drawerView) 
      { 
       getActionBar().setTitle(R.string.hello_world); 
      } 
     }; 
     drawerLayout.setDrawerListener(drawerToggle); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     //layers = getResources().getStringArray(R.array.layers_array); 
     drawerList = (ListView) findViewById(R.id.drawer_list); 
//  View header = getLayoutInflater().inflate(R.layout.drawer_list_header, null); 
//  drawerList.addHeaderView(header, null, false); 
     drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerListEntries)); 
//  View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
//    R.layout.drawer_list_footer, null, false); 
//  drawerList.addFooterView(footerView); 

     drawerList.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { 
       //map.drawerClickEvent(pos); 
       String selectedValue = (String) drawerList.getAdapter().getItem(pos); 
       Toast.makeText(getBaseContext(), selectedValue, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     if (drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 

    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     drawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     drawerToggle.onConfigurationChanged(newConfig); 
    } 
} 

這是HomeActivity的延伸BaseActivity代碼:

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.support.v4.app.ActionBarDrawerToggle; 
import android.support.v4.widget.DrawerLayout; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import com.facebook.Session; 

public class HomeActivity extends BaseActivity { 

    TextView greeting; 
    Button orderButton, deliverButton, logout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_home); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.home, menu); 
     //return super.onCreateOptionsMenu(menu); 
     return true; 
    } 


} 

我得到一個NullPointerException:

super.onCreate(savedInstanceState); // HomeActivity 

,當我點擊那它去:

drawerLayout.setDrawerListener(drawerToggle); // BaseActivity 

有誰知道我該如何解決這個問題?

更新:

drawer_list.xml:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


    <ListView 
     android:id="@+id/drawer_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="5dp" 
     android:background="@android:color/transparent" 
     android:cacheColorHint="@android:color/transparent" 
     android:divider="#CCCCCC" 
     android:dividerHeight="1dp" 
     android:paddingLeft="2dp" /> 


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

drawer_list_item.xml:在幾個帖子

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/drawer_list_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="12dp" 
     android:textSize="24sp" 
     android:layout_gravity="left" 
     android:textColor="@color/belize_hole" 
     android:fontFamily="sans-serif-light"> 
    </TextView> 



</LinearLayout> 
+0

看起來您正在嘗試在設置內容視圖之前查找抽屜視圖。 – daentech

+0

@daentech但這個答案被接受,海報聲稱它的作品100%...你會建議做什麼? – Loolooii

+0

如果在'drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);'line之前將'setContentView(R.layout.activity_home);'從HomeActivity移動到BaseActivity中,它會起作用嗎? – daentech

回答

2

同樣的問題。我是android新手,但以這種方式解決它。很想聽聽是否有更好的「正確」方式來做到這一點。 (是的,沒有片段,但我知道我最終需要學習)。

問題出現,基類不知道佈局。不知道如何或爲什麼它爲他人工作,而不必這樣做,但我是Android的新手。

我得到了同樣的錯誤,並通過onCreate將我的佈局傳遞給基礎來解決。

所以在基地,我修改的onCreate是這樣的:

protected void onCreate(Bundle savedInstanceState, int resLayoutID) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(resLayoutID); 

然後,從我的新的活動,我有這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState, R.layout.my_activity); 

這段代碼在我的基地現在的作品,而不是留空:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
drawerList = (ListView) findViewById(R.id.left_drawer); 

順便說一句,這個結果是由朋友建議給我,它對我有用。

相關問題