【问题标题】:Expandable ListView in Fragment, (from AndroidHive)Fragment 中的可扩展 ListView,(来自 AndroidHive)
【发布时间】:2014-06-07 06:19:08
【问题描述】:

我正在尝试在我的项目中使用此示例 http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/。 主要问题是: 我有 ListFragment 而不是常规的 Activity。 我用getActivity().findViewByID代替findViewByID,用getActivity().getApplicationContext()代替this

应用程序在

处崩溃
  // setting list adapter
    expListView.setAdapter(listAdapter);

我只是 Android 开发的新手。有人可以解释如何为 ListFragment 调整示例。

【问题讨论】:

  • 您的 expListView 为空。

标签: android android-fragments expandablelistview


【解决方案1】:

expListView 在您的代码中为空,您需要在 onCreateView 上返回的视图来查找可扩展视图而不是 getActivity()

使用如下:

  expListView = YourView.findViewById(yourId);

如果这对您没有帮助,请发布 logcat 错误和代码片段

示例代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View test= inflater.inflate(R.layout.testLayout,
                container, false);
        expListView = test.findViewById(R.id.expanableId);


        return test;
 }

【讨论】:

  • 它可以工作,但现在在“return MyView”应用程序崩溃之后。说 06-07 06:31:41.700 1222-1222/com.example.moneybag.moneybag E/AndroidRuntime: 致命例外: main java.lang.RuntimeException: 你的内容必须有一个 id 属性为 'android.R.id' 的 ListView .list'
  • 当您使用ListFragment 时,如果您不想将ListFragment 更改为Fragment,则您的xml 布局中必须有带有android.R.id.list 的listView
【解决方案2】:

尝试关注

在 xml 中更改您的 ExpandableListView id。

应该是android:id="@+id/lvList"

在您的 java 文件中将其设为 ExpandableListView expListView = Your_View.findViewById(R.id.lvList);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2015-10-02
    • 2019-05-21
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多