gpt4 book ai didi

Android:Fragment 布局中的 ListView

转载 作者:行者123 更新时间:2023-11-30 03:42:15 33 4
gpt4 key购买 nike

我正在尝试将 ListView 添加到 Fragment 上,而不仅仅是 ListFragment。我想要顶部的选项卡而不是 Fragment 中的 ListView。我的代码编译正常,但在启动时崩溃。

这是我正在尝试添加 ListView 的 fragment 。

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) 上崩溃了;

这里是调用 fragment 的完整代码

公共(public)类 MainCalActivity 扩展 FragmentActivity 实现 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

最佳答案

堆栈跟踪告诉您代码有什么问题: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 说:包含在实例化 View 时使用的 TextView 的布局文件的资源 ID(请参阅 http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, 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);

无论如何,使用两行布局在您的情况下没有任何意义,因为您似乎每行只有一个文本。

关于Android:Fragment 布局中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15547997/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com