2013-05-04 101 views
0

我遵循commonsware在這個環節中提供的樣品Single HeaderPreferenceFragment了java.lang.RuntimeException

,當我運行它,它給了我強制關閉下面的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類如下:

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:

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:

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

回答

0

您最好讓Eclipse運行通過異常點並查看LogCat。

話雖這麼說,你的問題是:

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

如果移動應用到你自己的包,你需要調整其他文件相匹配,特別是res/xml/preference_headers.xml

+0

先生IM保持你的例子是同樣的功能套餐名稱:com.commonsware.android.pref1header – 2013-05-04 22:36:19

+0

logcat的指示:在com.commonsware.android.pref1header.FragmentsDemo.onCreate(FragmentsDemo.java:28)錯誤 – 2013-05-04 22:39:17

+0

@AndroidStack :在您的環境中出現問題,然後,因爲應用程序肯定有效。我現在就在這裏跑。 – CommonsWare 2013-05-04 22:39:44

相關問題