简体   繁体   English

PreferenceFragment java.lang.RuntimeException

[英]PreferenceFragment java.lang.RuntimeException

I follow the sample provided by commonsware in this link Single Header 我在此链接中遵循了commonsware提供的示例Single Header

and when i run it it gave me force close with below logcat , 当我运行它时,它使我用logcat下面的力关闭,

any advice will be appreciated , thanks 任何建议将不胜感激,谢谢

logcat : logcat:

FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo   
 {com.commonsware.android.pref1header/com.commonsware.android.pref1header.
 FragmentsDemo}: android.view.InflateException: Binary XML file line #1: Error  
 inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #1: Error 
         inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView
     (PhoneWindow.java:216)
at android.app.Activity.setContentView(Activity.java:1660)
at com.commonsware.android.pref1header.FragmentsDemo.onCreate 
     (FragmentsDemo.java:28)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
   Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to  
    instantiate fragment 
   com.commonsware.android.pref1header.PreferenceContentsFragment: make sure class 
     name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
... 19 more
  Caused by: java.lang.ClassNotFoundException:  
  com.commonsware.android.pref1header.PreferenceContentsFragment in loader 
  dalvik.system.PathClassLoader[/data/app/com.commonsware.android.pref1header-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
... 22 more

JAVA class as follow : JAVA类如下:

EditPreferences: EditPreferences:

 public class EditPreferences extends PreferenceActivity {
   private boolean needResource=false;

   @SuppressWarnings("deprecation")
  @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  if (needResource
      || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    addPreferencesFromResource(R.xml.preferences);
         }
       }

  @Override
 public void onBuildHeaders(List<Header> target) {
   if (onIsHidingHeaders() || !onIsMultiPane()) {
    needResource=true;
         }
  else {
     loadHeadersFromResource(R.xml.preference_headers, target);
        }
      }
    }

StockPreferenceFragment: StockPreferenceFragment:

  public class StockPreferenceFragment extends PreferenceFragment {
 @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

 int res=

     getActivity().getResources().getIdentifier(getArguments().getString
         ("resource"), "xml",getActivity().getPackageName());

       addPreferencesFromResource(res);
             }
        }

PreferenceContentsFragment: PreferenceContentsFragment:

  public class PreferenceContentsFragment extends Fragment {
      private TextView checkbox=null;
      private TextView ringtone=null;
      private TextView checkbox2=null;
      private TextView text=null;
      private TextView list=null;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
     View result=inflater.inflate(R.layout.content, parent, false);

  checkbox=(TextView)result.findViewById(R.id.checkbox);
  ringtone=(TextView)result.findViewById(R.id.ringtone);
  checkbox2=(TextView)result.findViewById(R.id.checkbox2);
  text=(TextView)result.findViewById(R.id.text);
  list=(TextView)result.findViewById(R.id.list);

    return(result);
      }

  @Override
    public void onResume() {
     super.onResume();

   SharedPreferences prefs=
      PreferenceManager.getDefaultSharedPreferences(getActivity());

  checkbox.setText(new Boolean(prefs.getBoolean("checkbox", false)).toString());
  ringtone.setText(prefs.getString("ringtone", "<unset>"));
  checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2", false)).toString());
  text.setText(prefs.getString("text", "<unset>"));
  list.setText(prefs.getString("list", "<unset>"));
        }
    }

FragmentsDemo: 片段演示:

  public class FragmentsDemo extends FragmentActivity {
       @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
         }

  @Override
   public boolean onCreateOptionsMenu(Menu menu) {
   new MenuInflater(this).inflate(R.menu.actions, menu);

     return(super.onCreateOptionsMenu(menu));
             }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
     case R.id.settings:
       startActivity(new Intent(this, EditPreferences.class));

        return(true);
               }

    return(super.onOptionsItemSelected(item));
      }
     }

You are better served letting Eclipse run past the point of the exception and look at LogCat. 让Eclipse运行到异常点之外并查看LogCat更好。

That being said, your problem is: 话虽如此,您的问题是:

Caused by: java.lang.ClassNotFoundException: com.commonsware.android.pref1header.PreferenceContentsFragment

If you moved the app into your own package, you need to adjust the other files to match, notably res/xml/preference_headers.xml . 如果将应用程序移动到自己的程序包中,则需要调整其他文件以使其匹配,尤其是res/xml/preference_headers.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM