gpt4 book ai didi

java - android.view.InflateException : Binary XML file line #2:

转载 作者:行者123 更新时间:2023-12-02 10:23:55 25 4
gpt4 key购买 nike

我对编码还很陌生。在我的项目中,我创建了一个设置菜单,该菜单应显示用户应该能够搜索不同新闻报道的设置。我遇到的问题是单击 settingsActivity 后,我的应用程序崩溃了。

这是我的设置 Activity 类

package com.example.android.newsapp;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import android.support.v7.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
}

public static class NewsPreferenceFragment extends PreferenceFragment
implements Preference.OnPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_main);

Preference topicSearch =
findPreference(getString(R.string.settings_topic_search_key));

bindPreferenceSummaryToValue(topicSearch);
}

@Override
public boolean onPreferenceChange(Preference preference, Object
value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference)
preference;
int prefIndex =
listPreference.findIndexOfValue(stringValue);
if (prefIndex>= 0) {
CharSequence[] lables = listPreference.getEntries();
preference.setSummary(lables[prefIndex]);
}
}
else {
preference.setSummary(stringValue);
}
return true;
}

private void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(this);
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(preference.getContext());
String preferenceString =
preferences.getString(preference.getKey(), "");
onPreferenceChange(preference, preferenceString);
}

}
}

我的设置 Activity xml

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.android.newsapp.SettingsActivity$NewsPreferenceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment">

</fragment>

和错误

01-09 22:34:16.602 1899-1899/com.example.android.newsapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.newsapp/com.example.android.newsapp.SettingsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.android.newsapp.SettingsActivity.onCreate(SettingsActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at com.example.android.newsapp.SettingsActivity$NewsPreferenceFragment.bindPreferenceSummaryToValue(SettingsActivity.java:51)
at com.example.android.newsapp.SettingsActivity$NewsPreferenceFragment.onCreate(SettingsActivity.java:30)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
at android.app.Activity.onCreateView(Activity.java:4664)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
at com.example.android.newsapp.SettingsActivity.onCreate(SettingsActivity.java:17) 
at android.app.Activity.performCreate(Activity.java:5008) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 

最佳答案

将此行更改为:

android:name="com.example.android.newsapp.SettingsActivity$NewsPreferenceFragment"

android:name="com.example.android.newsapp.SettingsActivity.NewsPreferenceFragment"

我从 Android 切换到 Android 可能已经很长时间了,但我认为这会起作用。

如果您仍然遇到一些问题,请告诉我。谢谢

关于java - android.view.InflateException : Binary XML file line #2:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54121733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com