بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
سوال درباره Navigation Drawer with multiply Activity
با سلام خیلی سعی کردم مشکلم حل کنم و حتی توی سوال و جواب های که دیگران توی انجمن گفتند گشتم و حتی تو گوگل هم گشتم ، اولش که حتی برنامه ام درست کار نمیکرد ولی خداراشکر الان برنامه ام اومده بالا ، اما دو تا مشکل دارم :
اولین مشکل اینکه Activity که به Activity Enhanced وصل کردم و context کردم به (Standard Activity) در صفحه اصلی نمایش میده اما کار نمیکنه
و بزارید مشکل دوم ام هم بگم بعدش کدهام میفرستم ؛ مشکل دوم اینه که وقتی Sliding Menu باز میکنم روی یکی از آیتم ها کلیک میکنم crash میکنه .
اکتیویتی ها رو هم فقط extends کردم از Enhanced Activity که احتمال میدم اینجای کارم مشکل داشته باشه .
این Activity Enhanced :
package com.ahEngineer.programming.framework.activity;
/**
* This activity will add Navigation Drawer for our application and all the code related to navigation drawer.
* We are going to extend all our other activites from this BaseActivity so that every activity will have Navigation Drawer in it.
* This activity layout contain one frame layout in which we will add our child activity layout.
*/
public class EnhancedActivity extends HAppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
/**
* Static variable for selected item position. Which can be used in child activity to know which item is selected from the list.
*/
protected static int position;
/**
* Drawer listner class for drawer open, close etc.
*/
private static ActionBarDrawerToggle actionBarDrawerToggle;
private static DrawerLayout drawer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Founder(this)
.noActionBar()
.fullscreen()
.setContentView(R.layout.activity_enhanced)
.build();
drawer = findViewById(R.id.drawer_layout);
}
@Override
protected void onResume() {
super.onResume();
Toolbar hToolbar = findViewById(R.id.toolbar);
setSupportActionBar(hToolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, hToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
position = item.getItemId();
openActivity(position);
drawer.closeDrawer(GravityCompat.START);
return true;
}
/**
* Launching activity when any list item is clicked.
*/
protected void openActivity(int position) {
/**
* We can set title & itemChecked here but as this BaseActivity is parent for other activity,
* So whenever any activity is going to launch this BaseActivity is also going to be called and
* it will reset this value because of initialization in onCreate method.
* So that we are setting this in child activity.
*/
switch (position) {
case R.id.standard:
startActivity(new Intent(HBase.getCurrentActivity(), StandardActivity.class));
break;
case R.id.scientific:
startActivity(new Intent(HBase.getCurrentActivity(), ScientificActivity.class));
break;
case R.id.programmer:
startActivity(new Intent(HBase.getCurrentActivity(), ProgrammerActivity.class));
break;
case R.id.dateCalculation:
startActivity(new Intent(HBase.getCurrentActivity(), DateCalculationActivity.class));
break;
case R.id.currency:
startActivity(new Intent(HBase.getCurrentActivity(), CurrencyActivity.class));
break;
case R.id.volume:
startActivity(new Intent(HBase.getCurrentActivity(), VolumeActivity.class));
break;
case R.id.length:
startActivity(new Intent(HBase.getCurrentActivity(), LengthActivity.class));
break;
case R.id.weight:
startActivity(new Intent(HBase.getCurrentActivity(), WeightAndMassActivity.class));
break;
case R.id.temperature:
startActivity(new Intent(HBase.getCurrentActivity(), TemperatureActivity.class));
break;
case R.id.energy:
startActivity(new Intent(HBase.getCurrentActivity(), EnergyActivity.class));
break;
case R.id.area:
startActivity(new Intent(HBase.getCurrentActivity(), AreaActivity.class));
break;
case R.id.speed:
startActivity(new Intent(HBase.getCurrentActivity(), SpeedActivity.class));
break;
case R.id.time:
startActivity(new Intent(HBase.getCurrentActivity(), TimeActivity.class));
break;
case R.id.power:
startActivity(new Intent(HBase.getCurrentActivity(), PowerActivity.class));
break;
case R.id.data:
startActivity(new Intent(HBase.getCurrentActivity(), DataActivity.class));
break;
case R.id.pressure:
startActivity(new Intent(HBase.getCurrentActivity(), PressureActivity.class));
break;
case R.id.angle:
startActivity(new Intent(HBase.getCurrentActivity(), AngleActivity.class));
break;
case R.id.about:
startActivity(new Intent(HBase.getCurrentActivity(), AboutActivity.class));
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nav_menu, 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.
// ActionBarDrawerToggle will take care of this.
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/* We can override onBackPressed method to toggle navigation drawer*/
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
onBackPressed();
}
}
}
این activity_enhanced
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.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"
app:menu="@menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
این toolbar
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".StandardActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.AppCompat.NoActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#009688"
app:popupTheme="@style/Theme.AppCompat" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/activity_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
اینم errors
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ahEngineer.programming.calculator10, PID: 1905
java.lang.RuntimeException: Unable to resume activity {com.ahEngineer.programming.calculator10/com.ahEngineer.programming.calculator10.ScientificActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.navigation.NavigationView.setNavigationItemSelectedListener(com.google.android.material.navigation.NavigationView$OnNavigationItemSelectedListener)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.navigation.NavigationView.setNavigationItemSelectedListener(com.google.android.material.navigation.NavigationView$OnNavigationItemSelectedListener)' on a null object reference
///////////////////at com.ahEngineer.programming.framework.activity.EnhancedActivity.onResume(EnhancedActivity.java:85)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
at android.app.Activity.performResume(Activity.java:6076)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .