android - ListView 中的 textview.setText() 导致强制关闭

标签 android listview

这是我的代码,我知道某个地方有一个小错误,但作为 Android 菜鸟,我无法弄清楚。我已经进行了搜索,但没有结果。

SimpleListActivity.java:

public class SimpleListActivity extends ListActivity {
     /** Called when the activity is first created. */

        @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          setListAdapter(new myArrayAdapter(this, COUNTRIES));

          ListView lv = getListView();
          lv.setTextFilterEnabled(true);

          lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
              // When clicked, show a toast with the TextView text
              Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                  Toast.LENGTH_SHORT).show();
            }
          });
        }

        static final String[] COUNTRIES = new String[] {
            "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
            "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
            "Armenia", "Aruba", "Australia", "Austria"
          };
}

main.xml:

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

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

</LinearLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             >
             </CheckBox>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView">
            </TextView>

</LinearLayout>

myArrayAdapter.java:

public class myArrayAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final String[] values;

    public myArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);

        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.checkBox1);
        textView.setText(values[position]);
        return rowView;
    }

}

我不明白为什么我们使用主布局作为 ContentView。我的意思是官方 ListView 教程不使用它,并且列表工作得很好。但我在网上发现,当我使用findViewById时,我必须使用setContentView(R.layout.main);。我不明白如果id不在主视图中,那我为什么要使用它。

此外,此应用程序强制关闭。我已将问题范围缩小到 textView.setText(values[position]);。我哪里出错了?

更新:我删除了 setContentView,因为它给出了另一个错误,其中的解决方案发布在此处:ListView whose id attribute is 'android.R.id.list' Error when I have the ListView id set correctly

这是删除 setContentView 后的 logcat:

03-24 10:55:01.558: E/AndroidRuntime(7279): FATAL EXCEPTION: main
03-24 10:55:01.558: E/AndroidRuntime(7279): java.lang.NullPointerException
03-24 10:55:01.558: E/AndroidRuntime(7279):     at com.deepakmittal.simplelist.myArrayAdapter.getView(myArrayAdapter.java:32)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.AbsListView.obtainView(AbsListView.java:1467)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.ListView.fillDown(ListView.java:670)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.ListView.fillFromTop(ListView.java:727)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.ListView.layoutChildren(ListView.java:1598)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.AbsListView.onLayout(AbsListView.java:1273)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.os.Looper.loop(Looper.java:130)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at android.app.ActivityThread.main(ActivityThread.java:3835)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at java.lang.reflect.Method.invoke(Method.java:507)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-24 10:55:01.558: E/AndroidRuntime(7279):     at dalvik.system.NativeStart.main(Native Method)

第32行对应textView.setText(values[position]);

最佳答案

ListActivity 的默认布局由屏幕中央的单个全屏列表组成。但是,如果您愿意,您可以通过在 onCreate() 中使用 setContentView() 设置您自己的 View 布局来自定义屏幕布局。为此,您自己的 View 必须包含一个 ID 为“@+id/list”的 ListView 对象...因此,只有当您需要自定义布局时,您才应该使用 setContentView() ,否则...无需使用 R。 layout.main..现在强制关闭您的应用程序..您应该发布 logcat...以便很容易找出错误所在.. 并且我认为没有任何错误...除了更改

  android:id="@+id/listView1" in main.xml

to 

android:id="@+id/list"

因为我复制了代码并尝试了..它对我来说效果很好..

关于android - ListView 中的 textview.setText() 导致强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9849060/

相关文章:

android - 想要使用 Activity android 运行和停止服务

java - 内部存储 : Why is getFilesDir() giving me the wrong folder?

Android - MapView 包含在 Listview 中

python - 连接 ListView 和详细 View -django

Android - 从所有来源获取所有联系人

java - 如何让 TextView 在 Android Studio 中充当按钮?

java - 主要 Activity 在启动屏幕返回之前被破坏

android - 调用 addHeaderView() 后,我在适配器的 getview() 中得到了标题布局

android - 在 ListView 中查看寻呼机?

android - 我的 ListView 无法正常工作