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

这是我的settingsactivity班

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

}
}


我的settingsactivity 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切换到很长时间了,但是我认为这可以工作。

让我知道您是否仍然面临一些问题。
谢谢

10-06 04:55