我在此链接Single Header中遵循 commonsware 提供的示例
当我运行它时,它给了我以下 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类如下:
编辑偏好:
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:
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);
}
}
首选项内容片段:
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>"));
}
}
片段演示:
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));
}
}