Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

android - ListFragment does not accept my layout

According to this link: ListFragment android developers

I want to set my custom layout for list, but it makes exceptions.

Here is the code:

public class ListFragmentActivity extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    FragmentManager fm = getSupportFragmentManager();
    if(fm.findFragmentById(android.R.id.content) == null)
    {
        myListFragments list = new myListFragments();
        fm.beginTransaction().add(android.R.id.content, list).commit();
    }
}

public static class myListFragments extends ListFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.topoffers, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        List<OfferModel> offers = Global.getInstance().getOffers();
        List<OfferModel> topOffers = new ArrayList<OfferModel>(4);
        for (int i = 0; i < 4; i++) {
            if (offers.get(i).getOfferImage() == null)
                offers.get(i).setOfferImage(
                        downloadFile(offers.get(i).getOfferImageUrl()));
            topOffers.add(offers.get(i));
        }
        LazyAdapter adapter = new LazyAdapter(getActivity());
        adapter.setData(topOffers);
        setListAdapter(adapter);
        setListShown(true);
    }

    public Bitmap downloadFile(String fileUrl) {
        URL myFileUrl = null;
        try {
            myFileUrl = new URL(fileUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            return BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}}

and my custom layout is R.layout.topoffers

Logcat:

 05-15 21:43:56.975: W/dalvikvm(218): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
 05-15 21:43:56.975: E/AndroidRuntime(218): Uncaught handler: thread main exiting due to uncaught exception
 05-15 21:43:56.995: E/AndroidRuntime(218): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.anddev.android.ikiwi/org.anddev.android.ikiwi.ListFragmentActivity}:
    java.lang.IllegalStateException: Can't be used with a custom content view 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.os.Handler.dispatchMessage(Handler.java:99) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.os.Looper.loop(Looper.java:123) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.main(ActivityThread.java:4363) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at java.lang.reflect.Method.invokeNative(Native Method) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at java.lang.reflect.Method.invoke(Method.java:521) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at dalvik.system.NativeStart.main(Native Method) 
 05-15 21:43:56.995: E/AndroidRuntime(218): Caused by: java.lang.IllegalStateException:
    Can't be used with a custom content view 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.ListFragment.setListShown(ListFragment.java:282)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.ListFragment.setListShown(ListFragment.java:258)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at org.anddev.android.ikiwi.ListFragmentActivity$myListFragments.onActivityCreated(ListFragmentActivity.java:57)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.Activity.performStart(Activity.java:3723) 
 05-15 21:43:56.995: E/AndroidRuntime(218):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
 05-15 21:43:56.995: E/AndroidRuntime(218):     ... 11 more 
 05-15 21:43:57.025: I/dalvikvm(218): threadid=7: reacting to signal 3 
 05-15 21:43:57.025: E/dalvikvm(218): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create the layout file list_content.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <LinearLayout android:id="@+id/progressContainer"
            android:orientation="vertical"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:visibility="gone"
            android:gravity="center">
        
        <ProgressBar style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text=""
                android:paddingTop="4dip"
                android:singleLine="true" />
            
    </LinearLayout>
        
    <FrameLayout android:id="@+id/listContainer"
            android:layout_width="match_parent" 
            android:layout_height="match_parent">
            
        <ListView android:id="@android:id/list"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />
        <TextView android:id="@+id/internalEmpty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    </FrameLayout>
        
</FrameLayout>

Put this inside your ListFragment class:

public ListView mList;
boolean mListShown;
View mProgressContainer;
View mListContainer;

public void setListShown(boolean shown, boolean animate){
    if (mListShown == shown) {
        return;
    }
    mListShown = shown;
    if (shown) {
        if (animate) {
            mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_out));
            mListContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_in));
        }
        mProgressContainer.setVisibility(View.GONE);
        mListContainer.setVisibility(View.VISIBLE);
    } else {
        if (animate) {
            mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_in));
            mListContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_out));
        }
        mProgressContainer.setVisibility(View.VISIBLE);
        mListContainer.setVisibility(View.INVISIBLE);
    }
}
public void setListShown(boolean shown){
    setListShown(shown, true);
}
public void setListShownNoAnimation(boolean shown) {
    setListShown(shown, false);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    int INTERNAL_EMPTY_ID = 0x00ff0001;
    View root = inflater.inflate(R.layout.list_content, container, false);
    (root.findViewById(R.id.internalEmpty)).setId(INTERNAL_EMPTY_ID);
    mList = (ListView) root.findViewById(android.R.id.list);
    mListContainer =  root.findViewById(R.id.listContainer);
    mProgressContainer = root.findViewById(R.id.progressContainer);
    mListShown = true;
    return root;
}

Now you can use normally:

setListShown(boolean shown);
setListShown(boolean shown, boolean animate);
setListShownNoAnimation(boolean shown);

Source:

http://source-android.frandroid.com/frameworks/base/core/java/android/app/ListFragment.java

http://source-android.frandroid.com/frameworks/base/core/res/res/layout/list_content.xml


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...