2014-04-02 41 views
0

我正在嘗試使用具有一些活動且沒有片段的導航抽屜。我讀我通過幾個答案在這裏和this結束了和我心中已經得到了相同的問題,因爲用戶Dediqated,這裏是我的代碼:具有多個活動的Android導航抽屜

BaseActivity.java:

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

public class BaseActivity extends Activity { 

    public DrawerLayout drawerLayout; 
    public ListView drawerList; 
    public String[] layers; 
    private ActionBarDrawerToggle drawerToggle; 

    protected void onCreate(Bundle savedInstanceState) { 
     try { 
      // R.id.drawer_layout should be in every activity with exactly the same 
      // id. 
      drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

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

       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.planets_array); 
      drawerList = (ListView) findViewById(R.id.left_drawer); 
      drawerList.setAdapter(new ArrayAdapter<String>(this, 
        R.layout.drawer_list_item, android.R.id.text1, layers)); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      e.printStackTrace(); 
      e.printStackTrace(); 
     } 

    } 

    @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); 
    } 
} 

TestActivity.java:

import android.os.Bundle; 

public class TestActivity extends BaseActivity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile); 
    } 
} 

activity_base.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"> 

    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
     <!-- Add content here --> 
    </FrameLayout> 

    <!-- The navigation drawer --> 
    <ListView android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"/> 
</android.support.v4.widget.DrawerLayout> 

activity_profile.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".ProfileActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

drawer_list_item.xml

<!-- 
    Copyright 2013 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

     http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
    --> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:gravity="center_vertical" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:textColor="#fff" 
    android:background="?android:attr/activatedBackgroundIndicator" 
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/> 

在BaseActivity.java符合drawerLayout.setDrawerListener(drawerToggle); Eclipse中拋出一個NullPointerException,我只是不知道爲什麼。

對於同樣的問題,我已經提出了一個額外的問題,但我是新的,不允許添加評論。

回答

2

你在做什麼不能工作,因爲你正在呼叫findViewById之前設置contentview。這意味着,您甚至在設置佈局之前嘗試訪問活動佈局中的元素,換句話說,在初始化視圖之前您正在調用View.findViewById
此外,據我所知,您必須在您的活動中撥打super.onCreate。你沒有這樣做。您在TestActivity中致電super.onCreate,但必須在BaseActivity之內致電super.onCreate
我尚未與AB工作了活動,但我會建議做到以下幾點:

BaseActivity

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

void initDrawer(){ 
    // put everything you have in your onCreate in here: 
    try { 
     // R.id.drawer_layout should be in every activity with exactly the same 
     // id. 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

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

      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.planets_array); 
     drawerList = (ListView) findViewById(R.id.left_drawer); 
     drawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, android.R.id.text1, layers)); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     e.printStackTrace(); 
     e.printStackTrace(); 
    } 

} 

測試活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile); 
    initDrawer(); 
} 
+0

你是完全正確的,這工作得很好。在這一點上,我只需要知道我可以在哪裏定義哪個activity在哪個listView-Element應該啓動 – user3488420

+0

好吧,你必須考慮一些邏輯來執行點擊並註冊一個'OnClickListener',因爲[在這裏](http: //developer.android.com/training/implementing-navigation/nav-drawer.html#Init)。最簡單的方法是創建一個靜態數組並使用索引。 – MalaKa

+0

您在基本活動中有抽屜佈局,並且正在設置其他活動的佈局。基本活動如何在此場景中獲取抽屜佈局視圖 –

0

用這種方法BaseActivity並不適合我。原因是Activity和Fragments的混合以及在eclipse中創建新項目時友好生成的NavigationDrawerFragment的使用。

我有活動A有一個菜單5項。 1項目將啓動另一個活動(因爲它是一個FragmentPagerAdaper),其他人將只切換活動A中的片段。

當我使用BaseActivity時,我只有一個onNavigationDrawerItemSelected(int)(click-event)。

在Activity B中也使用這個會導致一個問題,因爲我需要先切換回Activity A然後顯示相應的Fragment。這實質上是不同的點擊處理。

我最終解決了這兩個活動中的onNavigationDrawerItemSelected(int)。實現抽屜只有兩條線,所以這不是太多的雙重工作。如果我有更多的活動,那麼我會去參加BaseActivity,但會根據調用活動需要用於處理案例的switch語句。