java - ListView 未初始化

标签 java android butterknife

这让我现在很抓狂。我有两个带有 ListView 的 fragment ,它们都使用 ButterKnife。其中一个工作正常,但另一个则不行。

这是工作 fragment :

//IRRELEVANT CODE ABOVE
@BindView(R.id.exerciseListView1)
ListView exerciseListView;

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public ExerciseFragment() {
}

@SuppressWarnings("unused")
public static ExerciseFragment newInstance(int columnCount) {
    ExerciseFragment fragment = new ExerciseFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_exercise_list, container, false);
    unbinder = ButterKnife.bind(this, view);

    ListItem[] exercises = {
            new ListItem(" 1. Learning Names & Faces", "This exercise will teach you how to learn people's names."),
            new ListItem(" 2. Paying Attention", "This exercise will test how well you pay attention."),
            new ListItem(" 3. Memory Recall", "This exercise will help you improve your recall skills.")
    };

    mAdapter = new ArrayAdapter<ListItem>(view.getContext(), R.layout.mylist, exercises);
    exerciseListView.setAdapter(mAdapter);
    exerciseListView.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            String item = ((TextView) view).getText().toString();
            Log.d("ExercisesActivity", item + " #" + Integer.toString(position) + " selected");
            //Switch statements won't take longs, thankfully there's the position variable
            switch (position) {
                case 0:
                    startActivity(new Intent(getContext(), LearningNamesActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(getContext(), PayingAttentionActivity.class));
                    break;
                case 2:
                    startActivity(new Intent(getContext(), MemoryRecallActivity.class));
                    break;
            }
        }
    });

    return view;
}

这是行不通的:

//IRRELEVANT CODE ABOVE
@BindView(R.id.informationListView)
ListView informationList;
/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public InformationFragment() {
}

// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static InformationFragment newInstance(int columnCount) {
    InformationFragment fragment = new InformationFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_information_list, container, false);
    unbinder = ButterKnife.bind(getContext(), view);

    ListItem[] exercises = {
            new ListItem("1. Tell me about memory", ""),
            new ListItem("2. What can go wrong with my memory", ""),
            new ListItem("3. Helpful tips to improve my memory", "")
    };
    mAdapter = new ArrayAdapter<>(view.getContext(), R.layout.mylist, exercises);
informationList.setAdapter(mAdapter);
informationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0:
                    startActivity(new Intent(getContext(), LearnAboutMemoryActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(getContext(), WhatCanGoWrongActivity.class));
                    break;
                case 2:
                    //TODO: Use HelpfulTipsActivity as an "all-purpose" activity for holding tip Fragments
                    startActivity(new Intent(getContext(), HelpfulTipsActivity.class));
                    break;
             }
        }
});

return view;

这是错误日志:

06-17 12:20:15.674 13182-13182/com.tommy.mastersproject E/AndroidRuntime: FATAL EXCEPTION: main                                                                         Process: com.tommy.mastersproject, PID: 13182
Theme: themes:{com.android.settings=overlay:com.cyngn.hexo, default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tommy.mastersproject/com.tommy.mastersproject.activities.InformationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.tommy.mastersproject.fragments.InformationFragment.onCreateView(InformationFragment.java:90)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
at android.app.Activity.performStart(Activity.java:6268)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是 fragment 各自的布局文件: 工作正常

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <ListView
        android:id="@+id/exerciseListView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

无法正常工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <ListView
        android:id="@+id/informationListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

工作 fragment 中是否有一些东西我忘记放入不起作用的 fragment 中?

最佳答案

我认为问题出在onCreateView()内的InformationFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.fragment_information_list, container, false);
  unbinder = ButterKnife.bind(this, view);

  ListItem[] exercises = {
        new ListItem("1. Tell me about memory", ""),
        new ListItem("2. What can go wrong with my memory", ""),
        new ListItem("3. Helpful tips to improve my memory", "")
  };
  // rest of your code

问题:

While binding view you used getContext().So your informationList ListView remains Null and you were getting NullPointerException Remove getContext() instead add this.

关于java - ListView 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37880421/

相关文章:

android - 更改android Checkbox的大小

android - 通过谷歌播放控制台禁用某些 Android 版本的 apk

android - 如何在 minsdk <24 上将 Kotlin 与 Butterknife 10.1.0 一起使用?

java - 表单对象的 id 被设置为 0

java - 错误 : Classes that should be initialized at run time got initialized during image building: org. conscrypt.Conscrypt 被无意初始化

java - 如何为Android模拟 "try with resources"

android - 用于手机和平板电脑的不同布局的 Butterknife

Android Butterknife - 在 fragment 中绑定(bind)

java - JDBC/结果集错误

java - 使用图像和环绕图像的文本编辑 TextView