2015-11-20 52 views
0

這是我的第一個問題。我發誓我研究並嘗試了很多,但沒有結果,我有點沮喪,沒有結果和錯誤。 :)與其他活動共享導航抽屜(高級)

我在Android Studio創建了一個導航抽屜項目,現在我試圖與其他活動共享菜單(MainActivity)。

我的MainActivity:

package br.com.pmarx.ihamster; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(menuItemListener); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
private OnNavigationItemSelectedListener menuItemListener = new OnNavigationItemSelectedListener() { 

    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.my_larder_general_activity) { 
      Intent myLarderViewActivity = new Intent(getApplicationContext(), MyLarderGeneralActivity.class); 
      startActivity(myLarderViewActivity); 
     } else if (id == R.id.my_plan_activity) { 
      Intent myPlanActivity = new Intent(getApplicationContext(), MyPlanActivity.class); 
      startActivity(myPlanActivity); 
     } else if (id == R.id.ins_item_larder) { 
      Intent insItemLarderActivity = new Intent(getApplicationContext(), InsItemLarderActivity.class); 
      startActivity(insItemLarderActivity); 
     } else if (id == R.id.ins_item_plan) { 
      Intent insItemPlanActivity = new Intent(getApplicationContext(), InsItemPlanActivity.class); 
      startActivity(insItemPlanActivity); 
     } else if (id == R.id.my_larder_kept_activity) { 
      Intent myLarderKeptActivity = new Intent(getApplicationContext(), MyLarderKeptActivity.class); 
      startActivity(myLarderKeptActivity); 
     } else if (id == R.id.my_larder_use_activity) { 
      Intent myLarderUseActivity = new Intent(getApplicationContext(), MyLarderUseActivity.class); 
      startActivity(myLarderUseActivity); 
     } else if (id == R.id.my_shopping_list_activity) { 
      Intent myShoppingListActivity = new Intent(getApplicationContext(), MyShoppingListActivity.class); 
      startActivity(myShoppingListActivity); 
     //Populando a Database Daqui 
     } else if (id == R.id.database_populate) { 
      DatabaseDAO crud = new DatabaseDAO(getBaseContext()); 
      crud.popularDatabase(); 
     //Populando a Database Até Aqui 
     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 

    } 

}; 
} 

我的新的活動,以擴展在MainActivity(和菜單):

package br.com.pmarx.ihamster; 

import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class MyShoppingListActivity extends MainActivity { 

private ListView listItem; 
private CreateDatabase database; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

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

    listItem = (ListView)findViewById(R.id.listView); 
    listItem.setOnItemClickListener(listItemListener); 

}; 

@Override 
protected void onResume() { 

    super.onResume(); 

    DatabaseDAO crud = new DatabaseDAO(getBaseContext()); 
    Cursor cursor = crud.loadShoppingList(); 

    String[] fieldNames = new String[] {database.PROD_NOME, "volume3"}; 
    int[] idViews = new int[] {R.id.tvProd, R.id.tvVolum}; 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.sel_item_plan, cursor, fieldNames, idViews, 0); 

    listItem = (ListView)findViewById(R.id.listView); 
    listItem.setAdapter(adapter); 

}; 

private OnItemClickListener listItemListener = new OnItemClickListener(){ 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 

     //Passando parametro para a outra tela 
     Intent insertItemActivity = new Intent(getBaseContext(), InsItemLarderActivity.class); 
     insertItemActivity.putExtra("PROD_ID", String.valueOf(id)); 
     startActivity(insertItemActivity); 


    } 

}; 

} 

首先,我得到的錯誤:「這項活動已經由提供的操作欄窗口裝飾「。所以,我改變了這樣的風格:

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

我activity_main.xml中

<?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" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<include layout="@layout/app_bar_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

我app_bar_main.xml

<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=".MainActivity"> 
    <android.support.design.widget.AppBarLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     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> 

<include layout="@layout/content_main" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 
</android.support.design.widget.CoordinatorLayout> 

我my_plan

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical" 
android:background="#fdb46d" 
android:weightSum="1"> 

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:rowCount="1" 
    android:columnCount="4" 
    android:layout_gravity="center_horizontal"> 

</GridLayout> 

<include 
    android:id="@+id/toolbar" 
    layout="@layout/activity_main" /> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/listView" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:choiceMode="singleChoice" 
    android:clickable="false" 
    android:contextClickable="false" /> 

</LinearLayout> 

但是,我開始得到錯誤「顯示java.lang.NullPointerException:在嘗試調用虛擬方法 '布爾android.support.v4.widget.DrawerLayout.isDrawerOpen(INT)'空對象引用「。

經過一些改變,現在我得到了一個空對象引用的錯誤「java.lang.NullPointerException:嘗試調用虛方法'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' 「

讓我們一起做,一步一步來。我真的堅持這一點。

感謝先進!

+0

我從MainActivity中刪除了「public void onBackPressed()」mehod,並且NullPointer已停止,但該菜單未顯示在新的活動中。對不起,我的英語不好。 –

+0

好的1)我發現這更容易使用創建導航抽屜:https://github.com/mikepenz/MaterialDrawer 2)你最可能得到的錯誤,因爲MyShoppingListActivity重寫'setContentView()'意味着沒有DrawerLayout ,因此它確實是空的。看起來像你應該嘗試和使用片段,因爲這是一個更自然的適合導航抽屜(1活動與抽屜編排一組片段) – cYrixmorten

+0

我添加更多的細節來幫助你。謝謝! –

回答

0

MyShoppingListActivity開始

首先,onCreate方法被調用並執行這條線。

super.onCreate(savedInstanceState); 

它調用父類的onCreate方法,它是MainActivity的onCreate方法。

所以setContentView(R.layout.activity_main);在超級調用中執行。

然後setContentView(R.layout.my_plan)被執行,其設置佈局是R.layout.my_plan

所以發現工具條線路將返回null,如果你沒有在你的R.layout.my_plan XML文件中定義ID R.id.toolbar工具欄。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

該解決方案將創建一個沒有佈局,沒有調用setContentView(...)方法的BaseActivity。將所有設置視圖代碼放入if(findViewById(id)!= null){...}塊。

然後在您的子類Activities'layout.xml文件中使用相同的Id定義Widgets。

+0

現在我得到錯誤java.lang.RuntimeException:無法啓動活動ComponentInfo {br.com.pmarx.ihamster /br.com.pmarx.ihamster.MyShoppingListActivity}:java.lang.NullPointerException:嘗試調用虛擬方法'java .lang.CharSequence android.support.v7.widget.Toolbar.getTitle()'空對象引用 –

+0

我添加更多的細節來幫助你。謝謝! –