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

时间:2012-03-24 04:06:00

标签: 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。我的意思是官方列表视图教程不使用它,列表工作正常。但我在网上发现,当我使用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]);

4 个答案:

答案 0 :(得分:2)

ListActivity具有默认布局,该布局由屏幕中央的单个全屏列表组成。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。要做到这一点,你自己的视图必须包含一个ID为“@ + id / list”的ListView对象....所以只有你需要一个自定义的布局你应该使用setContentView()或者其他..不需要使用R. layout.main ..现在为你的应用程序强制关闭..你应该发布logcat ...这样就很容易找出错误的位置.. 和我认为没有任何错误 ...除了改变

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

to 

android:id="@+id/list"

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

答案 1 :(得分:1)

而不是这个

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

使用:

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

答案 2 :(得分:0)

在没有查看Logcat的情况下,我猜你的问题实际上在于你提供的setListAdapter(ListAdapter)与ArrayAdapter而不是请求的ListAdapter - 但这不应该编译..

另一种可能性是你对'values'数组进行了不正确的反转(即访问带有值[position]的越界位置)。

是的 - 我猜测的是设置ListView,ListViewActivity将遍历列表'获取视图',直到它得到一个空视图。您需要做的是检查该位置是否在值数组的边界内。如果是这样,请设置文本视图,就像您正常做的那样。如果没有,则返回null。

答案 3 :(得分:-1)

试试这段代码

ArrayAdapter<String> adapter;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);

        lv = (ListView) findViewById(R.id.edit_lv);
        arrayList = new ArrayList<HashMap<String, String>>();

        mDbHelper = new Database_creat(Show.this);
        mDbHelper.Open();
        adapter= new ArrayAdapter<String>  
                          (this,ndroid.R.layout.simple_list_item_1,COUNTRIES);



        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }

        });

    }