设置长按事件侦听器时出现异常

时间:2014-06-06 14:04:17

标签: android android-layout android-fragments android-listview

我最近开始使用android,但我遇到了一个无法找到导致问题的问题。我正在使用主/从流的标准模板。

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".TasksListActivity" >

<fragment
    android:id="@+id/tasks_list"
    android:name="bg.pandasoft.task_2_do.TasksListFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"

    tools:layout="@android:layout/list_content" />

<FrameLayout
    android:id="@+id/tasks_detail_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3" />

</LinearLayout>

tasks_list片段正在保存我的类别,我尝试在其片段处理程序中执行此操作:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  sql = new SQLCat(getActivity());
  BuildList();
  getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            Categories item = _CatAdapter.getItem(position);
            CAT_KEY = item.GetKey();
            if (item.GetDeletable() == 0) {
                Toast.makeText(getActivity(), "Category '" + item.GetName()
                        + "' cannot be edited or deleted.", GLOBAL.ToastDuration);
            } else {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        getActivity());
                alertDialog.setTitle("Actions");
                alertDialog.setSingleChoiceItems(array, -1,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if (which == 0) {
                                    deleteEntry();
                                    BuildList();
                                } else if (which == 1) {
                                    Categories item = _CatAdapter
                                            .getItemById(CAT_KEY);
                                    AccessEditScreen(item.GetKey(), false);
                                }
                                dialog.cancel();
                            }
                        });
                alertDialog.show();
            }
            return true;
        }
    });

问题是,当调试器点击这一行时,它会因为Inflate Exception而爆炸。我正在尝试做什么?或者我在某个地方转错了?

------- ------- EDIT 错误如下:

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity     ComponentInfo{bg.pandasoft.task_2_do/bg.pandasoft.task_2_do.TasksListActivity}:   android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:1930)
at bg.pandasoft.task_2_do.TasksListActivity.onCreate(TasksListActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
... 11 more
 Caused by: java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at bg.pandasoft.task_2_do.TasksListFragment.onCreate(TasksListFragment.java:62)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
... 21 more

2 个答案:

答案 0 :(得分:2)

我遇到了这个问题,因为我在样式XML中定义了字体系列。如果你也这样做,请验证并删除它。

<item name="android:fontFamily">sans-serif</item> - 删除此

答案 1 :(得分:0)

问题是getListView()在创建片段视图之前无法返回列表。请参阅片段生命周期:

Fragment life-circle

正如您可以看到在onCreate()之前调用onCreateView()回调,这意味着片段视图(在您的情况下是它的列表)尚未膨胀。 因此,然后活动开始并尝试创建片段 - 由于在getListView()内检查列表就绪状态,它会获得异常。 您需要在getListView()中调用onCreateView()并修改代码。阅读有关片段on d.android.com的更多信息。