1

ダウンローダーアプリを作成していますが、onCreateViewの戻りセクションの後に子供の親のエラーが発生しました。私はたくさんのことを試しましたが、何の助けにもなりません。私は試しました: ((ViewGroup)dlistView.getParent()).removeView(dlistView); その後、コンパイラは次のように言います:

01-12 12:05:15.558: E/AndroidRuntime(10740): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

またはコンテナを削除します/私はnpeを取得しました/またはVを削除しますが、V.getParent()はnullです。

親はどちらのビューで、子はどちらのビューですか?

コード:

public static class DownloadingFragment extends ListFragment {
public static final String ARG_SECTION_NUMBER = "section_number";
        public DownloadingFragment() {
        }

        @Override
        public View onCreateView (LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View V = inflater.inflate (R.layout.list, null);    
            ListView dlistView = (ListView) V.findViewById(R.id.dlist);
            List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
            downloadInfo.add(new DownloadInfo("File", 1000));
            DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
                    getActivity().getApplication(),R.layout.list, downloadInfo);
            dlistView.setAdapter(diaa);
        return dlistView; // throw exception 
        }
    }
//...
}

よろしくお願いします。

編集:新しいコード:

public static class DownloadingFragment extends Fragment {
        public static final String ARG_SECTION_NUMBER = "section_number";
        public DownloadingFragment() {
        }
        @Override
        public View onCreateView (LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View V = inflater.inflate (R.layout.list, null);    
        return V;
        }

        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            ListView dlistView = (ListView) getView().findViewById(R.id.dlist);
            List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
            downloadInfo.add(new DownloadInfo("File", 1000));
            DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
                    getActivity().getApplication(),R.layout.list, downloadInfo);
            dlistView.setAdapter(diaa);
        }
    }
//...
}
4

2 に答える 2

1

これは、親(Vのレイアウトツリーのどこかにある)を持つリストビューを返すためです。

どのタイプのフラグメントでも、onCreateViewは親を持たないビューを返す必要があります。

in this case , since it's a listFragment , create the listview (either using xml or programmatically ) , and then return it . do not allow it to have any parents since it needs to have a parent as a fragment .

read here for more information :

The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

于 2013-01-12T11:11:31.157 に答える
1

Note that the third argument of inflate is false. so replace this line

View V = inflater.inflate (R.layout.list, null);    

with this

View V = inflater.inflate (R.layout.list, null,false); 

and you are done.hope this helps.

于 2013-01-12T11:17:25.180 に答える