我有一个主类,我想向其中添加两个片段。目前,我只编写了一个片段,所以我决定测试一下。但是,我在 log cat 中得到如下异常:
08-02 13:07:24.541: E/Trace(952): error opening trace file: No such file or directory (2)
08-02 13:07:27.641: E/AndroidRuntime(952): FATAL EXCEPTION: main
08-02 13:07:27.641: E/AndroidRuntime(952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tqafragments/com.example.tqafragments.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.os.Looper.loop(Looper.java:137)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-02 13:07:27.641: E/AndroidRuntime(952): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 13:07:27.641: E/AndroidRuntime(952): at java.lang.reflect.Method.invoke(Method.java:511)
08-02 13:07:27.641: E/AndroidRuntime(952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-02 13:07:27.641: E/AndroidRuntime(952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-02 13:07:27.641: E/AndroidRuntime(952): at dalvik.system.NativeStart.main(Native Method)
08-02 13:07:27.641: E/AndroidRuntime(952): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
08-02 13:07:27.641: E/AndroidRuntime(952): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-02 13:07:27.641: E/AndroidRuntime(952): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.Activity.setContentView(Activity.java:1881)
08-02 13:07:27.641: E/AndroidRuntime(952): at com.example.tqafragments.MainActivity.onCreate(MainActivity.java:13)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.Activity.performCreate(Activity.java:5104)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-02 13:07:27.641: E/AndroidRuntime(952): ... 11 more
08-02 13:07:27.641: E/AndroidRuntime(952): Caused by: android.app.SuperNotCalledException: Fragment FeedFragment{40d076d0 #0 id=0x7f080003} did not call through to super.onCreate()
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.Fragment.performCreate(Fragment.java:1675)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.app.Activity.onCreateView(Activity.java:4717)
08-02 13:07:27.641: E/AndroidRuntime(952): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
08-02 13:07:27.641: E/AndroidRuntime(952): ... 21 more
我的主要活动除了
setContentView
. 有一个指向
Here is the class 的标签:
main.xml
fragment
FeedFragment
FeedFragment
package com.example.tqafragments;
import java.util.Vector;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
/**
* FeedFragment
* @author
*
*/
public class FeedFragment extends Fragment{
Vector<Feed> feeds = null;
@Override
public void onCreate(Bundle savedInstanceState){
// nothing to initialize.
}
//------------------------------------------------------------------------------
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
return inflater.inflate(R.layout.feed_fragment, container, false); // inflate the fragment layout
}
//------------------------------------------------------------------------------
@Override
public void onStart(){
new AsyncFeedRetriever().execute(new FeedProcessor()); // get the RSS feeds
// Add itemClickListener for ExpandableListView
// in other fragment as the other fragment
// is not yet inflated. Will result in a NPE here.
}
//------------------------------------------------------------------------------
/**
* AsyncRetriever
* @author
*
*/
class AsyncFeedRetriever extends AsyncTask<FeedProcessor,Void,FeedProcessor>{
ProgressDialog pDialog = null;
//------------------------------------------------------------------------------
@Override
protected void onPreExecute(){
pDialog = new ProgressDialog(getActivity()); // display dialog box
pDialog.setMessage(getActivity().getResources().getText(R.string.loading));
pDialog.show();
}
//------------------------------------------------------------------------------
@Override
protected FeedProcessor doInBackground(FeedProcessor... arg0) {
FeedProcessor p = arg0[0];
p.processFeed(); // get the feeds, actually, from the URL.
return p;
}
//------------------------------------------------------------------------------
@Override
protected void onPostExecute(FeedProcessor p){
pDialog.dismiss();
ExpandableListView list = (ExpandableListView) getActivity().findViewById(R.id.expandingList);
if(p.errorOccured == false){
feeds = p.getFeeds(); // populate the list
ArrayAdapter<Feed> adapter = new ArrayAdapter<Feed>(
getActivity(),
R.layout.list_row,
R.id.row,
feeds);
list.setAdapter(adapter);
}else{
// nothing
}
}
//------------------------------------------------------------------------------
}
}
feed_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView android:id="@+id/expandingList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</ExpandableListView>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:background="#FF808080">
<Button android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/refresh_str"/>"
</LinearLayout>
</LinearLayout>
有人可以帮我解决这个问题吗?