1

ListFragment だけでなく、ListView を Fragment に追加しようとしています。Fragment a ListViewよりも上にタブが必要です。コードは正常にコンパイルされますが、起動するとクラッシュします。

これは、ListViewも追加しようとしているフラグメントです。

public static class MainFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public MainFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        View V = inflater.inflate(R.layout.main, container, false);

        ListView LV = (ListView) this.getActivity().findViewById(R.id.mainList);
        String List[] = new String[2];
        List[0] = "tst1";
        List[1] ="tst2";
        ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_2, List);

        LV.setAdapter(aa);

        return V;
    }
}

ログの猫は、LV.setAdapter(aa); でクラッシュしていると言っています。

そして、これがフラグメントを呼び出す完全なコードです

public class MainCalActivity extends FragmentActivity implement ActionBar.TabListener {

SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_cal);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main_cal, menu);
    return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment;                  
        Bundle args = new Bundle();
        if(position == 0)
        {

            fragment = new MainFragment();
            args.putInt(MainFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return  fragment;
        } else
        {

            fragment = new CalFragment();
            args.putInt(MainFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.main);
            case 1:
                return getString(R.string.cal);

        }
        return null;
    }
}

/**
 * The Main fragment argument representing the main menu
 */
public static class MainFragment extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public MainFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        View V = inflater.inflate(R.layout.main, container, false);

        ListView LV = (ListView) this.getActivity().findViewById(R.id.mainList);
        String List[] = new String[2];
        List[0] = "tst1";
        List[1] ="tst2";
        ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_2, List);

        LV.setAdapter(aa);

        return V;
    }
}

/**
* The Calendar fragment argument representing the Calendar
*/

public static class CalFragment extends Fragment {
    /**
     * The Calendar fragment argument representing the Calendar
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public CalFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View V = inflater.inflate(R.layout.cal, container, false);

        return V;
    }
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
    vandroid:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/addS"
    android:textSize="30sp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="246dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/stuff" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:gravity="right"
    android:text="@string/add" />

<ListView
    android:id="@+id/mainList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

と私の丸太猫

03-21 13:15:13.845: E/ArrayAdapter(837): You must supply a resource ID for a TextView
03-21 13:15:14.015: E/AndroidRuntime(837): FATAL EXCEPTION: main
03-21 13:15:14.015: E/AndroidRuntime(837): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.ListView.onMeasure(ListView.java:1158)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15513)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15513)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15513)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1381)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15513)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15513)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15513)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-21 13:15:14.015: E/AndroidRuntime(837):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.View.measure(View.java:15513)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.Choreographer.doFrame(Choreographer.java:532)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.os.Handler.handleCallback(Handler.java:725)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:137)
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-21 13:15:14.015: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
03-21 13:15:14.015: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:511)
03-21 13:15:14.015: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-21 13:15:14.015: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-21 13:15:14.015: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)
03-21 13:15:14.015: E/AndroidRuntime(837): Caused by: java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
03-21 13:15:14.015: E/AndroidRuntime(837):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
03-21 13:15:14.015: E/AndroidRuntime(837):  ... 43 more
03-21 13:15:39.406: E/Trace(915): error opening trace file: No such file or directory (2)
03-21 13:15:40.465: E/ArrayAdapter(915): You must supply a resource ID for a TextView
03-21 13:15:40.495: E/AndroidRuntime(915): FATAL EXCEPTION: main
03-21 13:15:40.495: E/AndroidRuntime(915): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.ListView.onMeasure(ListView.java:1158)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.View.measure(View.java:15513)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.View.measure(View.java:15513)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.View.measure(View.java:15513)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1381)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.View.measure(View.java:15513)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.View.measure(View.java:15513)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.View.measure(View.java:15513)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-21 13:15:40.495: E/AndroidRuntime(915):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.View.measure(View.java:15513)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.Choreographer.doFrame(Choreographer.java:532)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.os.Handler.handleCallback(Handler.java:725)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.os.Looper.loop(Looper.java:137)
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-21 13:15:40.495: E/AndroidRuntime(915):  at java.lang.reflect.Method.invokeNative(Native Method)
03-21 13:15:40.495: E/AndroidRuntime(915):  at java.lang.reflect.Method.invoke(Method.java:511)
03-21 13:15:40.495: E/AndroidRuntime(915):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-21 13:15:40.495: E/AndroidRuntime(915):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-21 13:15:40.495: E/AndroidRuntime(915):  at dalvik.system.NativeStart.main(Native Method)
03-21 13:15:40.495: E/AndroidRuntime(915): Caused by: java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
03-21 13:15:40.495: E/AndroidRuntime(915):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
03-21 13:15:40.495: E/AndroidRuntime(915):  ... 43 more
4

1 に答える 1

1

スタックトレースは、コードの問題点を示しています: java.lang.IllegalStateException: ArrayAdapter はリソース ID を TextView にする必要があります

使用している

new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_2, List);

このコンストラクタは次のとおりです。

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

ドキュメントには、textViewResourceId について次のように記載されています。コンテキスト、int) )

使用しているレイアウトはより複雑なレイアウト (TwoLineListItem) であるため、使用する TextView をアダプターに伝える必要があります。

new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_2, android.R.id.text1, List);

または、代わりに単純なレイアウト android.R.layout.simple_list_item_1 を使用することもできます。

new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, List);

とにかく、1行に1つのテキストしかないように見えるため、2行のレイアウトを使用しても意味がありません。

于 2013-03-21T13:58:25.077 に答える