我有一个正常工作的PreferenceActivity。然后,我决定添加一个导航抽屉,并根据我的其他活动布局编辑布局,这些布局也有工作导航抽屉。但是,在这之后,我的PreferenceActivity停止打开并开始抛出NPE。我甚至不能再开那个活动了。问题显然在onCreate中,也在logcat中所述的onClick方法中。然而,在这个关于创建的活动中有两个onClick,我没有看到这些是如何导致NPE的,因为它们在我的其他工作活动中完全相同。根据默认的DrawerLayout实现,我的布局xmls分成3 xmls。没有进一步的到期,这里是代码和xmls:
PreferenceActivity:
public class FirstYearCoursesActivity extends PreferenceActivity
implements NavigationView.OnNavigationItemSelectedListener {
private AppCompatDelegate mDelegate;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_year_courses);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar((toolbar));
Window window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
addPreferencesFromResource(R.xml.prefs);
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_view2);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
ImageButton logoutButton = (ImageButton) headerView.findViewById(R.id.logoutButton);
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstYearCoursesActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
@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
protected void onPause() {
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final Handler handler = new Handler();
if (drawer.isDrawerOpen(GravityCompat.START)) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
drawer.closeDrawer(GravityCompat.START);
}
}, 200);
super.onPause();
} else {
super.onPause();
}
}
private void setSupportActionBar(@Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
String key = preference.getKey();
switch (key) {
case "math119":
Intent intent = new Intent(FirstYearCoursesActivity.this, Math119Activity.class);
startActivity(intent);
break;
default:
Toast.makeText(this, "Other Click", Toast.LENGTH_SHORT).show();
break;
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// to be filled
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
return true;
}
}
activity.xml:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include
layout="@layout/app_bar_first_year_courses"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view2"
android:layout_width="250dp"
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:background="#ffffffff"
app:itemTextColor="#000000"
app:itemTextAppearance="@style/TextAppearance.AppCompat.Body1"/>
</android.support.v4.widget.DrawerLayout>
app_bar.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=".activities.FirstYearCoursesActivity">
<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>
<include layout="@layout/content_first_year_courses"/>
</android.support.design.widget.CoordinatorLayout>
content.xml中:
<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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.FirstYearCoursesActivity"
tools:showIn="@layout/app_bar_first_year_courses"
android:orientation="vertical">
</LinearLayout>
nav_header.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="@drawable/profilebanner"
android:gravity="bottom"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/header">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logoutButton"
android:src="@drawable/ic_power_settings_new_white_18dp"
android:background="#00ffffff"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/headerClick"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:paddingTop="20dp">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/imageView"
android:background="@drawable/onur"
android:layout_centerVertical="true"
android:layout_alignParentStart="false"
android:layout_alignBottom="@+id/textView3"
android:layout_marginBottom="-20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Onur Çevik"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:id="@+id/textView2"
android:textSize="13sp"
android:layout_toRightOf="@+id/imageView"
android:layout_marginLeft="25dp"
android:layout_marginTop="45dp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Environmental Engineering"
android:textSize="11sp"
android:layout_toRightOf="@+id/imageView"
android:layout_marginLeft="25dp"
android:layout_below="@+id/textView2"
android:layout_alignParentStart="false"/>
</RelativeLayout>
</RelativeLayout>
的prefs.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="firstyearprefs">
<Preference
android:title="Math-119"
android:key="math119"/>
<Preference
android:title="Math-120"
android:key="math120"/>
<Preference
android:title="Phys-105"
android:key="phys105"/>
<Preference
android:title="Phys-106"
android:key="phys106"/>
<Preference
android:title="Chem-107"
android:key="chem107"/>
<Preference
android:title="Eng-101"
android:key="eng101"/>
<Preference
android:title="Eng-102"
android:key="eng102"/>
<Preference
android:title="Ce-101"
android:key="ce101"/>
</PreferenceScreen>
logcat的:
02-08 02:10:35.415 30815-30815/com.theoc.proto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.theoc.proto, PID: 30815
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference
at android.preference.PreferenceScreen.bind(PreferenceScreen.java:143)
at android.preference.PreferenceActivity.bindPreferences(PreferenceActivity.java:1419)
at android.preference.PreferenceActivity.access$000(PreferenceActivity.java:123)
at android.preference.PreferenceActivity$1.handleMessage(PreferenceActivity.java:230)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
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:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
修改
值得一提的是,我对活动及其适配器进行了三重检查,以便在列表项目上单击此PreferenceActivity。我颠倒了所有的Drawer Layout实现,我能够通过父活动打开这个PreferenceActivity。导航抽屉代码或布局是弄乱它的。我正在获取listview.onclick(adapter)NPE因为我的列表项单击父活动转到空引用,在这种情况下它是PreferenceActivity本身。另外调试没有在PreferenceActivity的onCreate中显示任何null,除了savedInstanceState。
答案 0 :(得分:1)
我能够使用您的代码重现相同的NPE,并使用片段替换 if ( (num-months> months-in-year) & (R=0))
Add additional-value to balance-month
multiply month-counter by months-in-year
add interest-month to balance-month rounded
perform 400-process-interest
perform 310-print-values
display blank-line
end-perform
调用(请注意它已被弃用)来修复它,如以下话题中所述:
Android: Preference Fragment with a Navigation Drawer's Fragment
What to use instead of "addPreferencesFromResource" in a PreferenceActivity?
虽然在我的版本中,首选项视图与工具栏重叠,但我不确定应该采用哪种活动布局来解决这个问题,也许在您的版本中就可以了。