Android 列表视图异常
Posted
技术标签:
【中文标题】Android 列表视图异常【英文标题】:Android List View Exception 【发布时间】:2014-05-19 11:47:07 【问题描述】:我正在尝试开发安卓应用。我想以一种很好的方式显示用户联系方式,我使用的是ListView
,但有例外。
这里是代码:
package com.example.list;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Display extends Activity implements OnItemClickListener
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma ;
Button select;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
getAllContacts(this.getContentResolver());
ListView lv= (ListView)findViewById(android.R.id.list);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
// adding
select = (Button) findViewById(R.id.button1);
select.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
StringBuilder checkedcontacts= new StringBuilder();
System.out.println(".............."+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
if(ma.mCheckStates.get(i)==true)
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
else
System.out.println("Not Checked......"+name1.get(i).toString());
Toast.makeText(Display.this, checkedcontacts,1000).show();
);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
// TODO Auto-generated method stub
ma.toggle(arg2);
public void getAllContacts(ContentResolver cr)
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(".................."+phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
phones.close();
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1,tv;
CheckBox cb;
MyAdapter()
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
// TODO Auto-generated method stub
return name1.size();
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return position;
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return 0;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
// vi = mInflater.inflate(R.layout.row, null);
vi = mInflater.inflate(R.layout.gg, null);
TextView tv= (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
// cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ name1.get(position));
tv1.setText("Phone No :"+ phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
public boolean isChecked(int position)
return mCheckStates.get(position, false);
public void setChecked(int position, boolean isChecked)
mCheckStates.put(position, isChecked);
System.out.println("hello...........");
notifyDataSetChanged();
public void toggle(int position)
setChecked(position, !isChecked(position));
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
这里是显示 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Display" >
<ListView
android:id="@+android:id/list"
android:layout_
android:layout_
android:layout_above="@+id/button1" />
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/Select" />
<TextView
android:layout_
android:layout_
android:text="@string/hello_world" />
和行xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+android:id/textView1"
android:layout_
android:layout_
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_
android:layout_
android:text="TextView" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_
android:layout_
android:text="@string/CheckBox" />
这里是 logcat:
04-07 12:35:37.249: E/AndroidRuntime(18080): FATAL EXCEPTION: main
04-07 12:35:37.249: E/AndroidRuntime(18080): java.lang.NullPointerException
04-07 12:35:37.249: E/AndroidRuntime(18080): at com.example.list.Display$MyAdapter.getView(Display.java:137)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.widget.AbsListView.obtainView(AbsListView.java:2624)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.widget.ListView.measureHeightOfChildren(ListView.java:1254)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.widget.ListView.onMeasure(ListView.java:1166)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-07 12:35:37.249: E/AndroidRuntime(18080): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-07 12:35:37.249: E/AndroidRuntime(18080): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2581)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.View.measure(View.java:16829)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2186)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1349)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1532)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6339)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.Choreographer.doFrame(Choreographer.java:561)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.os.Handler.handleCallback(Handler.java:730)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.os.Looper.loop(Looper.java:137)
04-07 12:35:37.249: E/AndroidRuntime(18080): at android.app.ActivityThread.main(ActivityThread.java:5450)
04-07 12:35:37.249: E/AndroidRuntime(18080): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 12:35:37.249: E/AndroidRuntime(18080): at java.lang.reflect.Method.invoke(Method.java:525)
04-07 12:35:37.249: E/AndroidRuntime(18080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-07 12:35:37.249: E/AndroidRuntime(18080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-07 12:35:37.249: E/AndroidRuntime(18080): at dalvik.system.NativeStart.main(Native Method)
我不知道那里有什么问题。
【问题讨论】:
cb
为空。您注释掉了初始化它的行,但您仍然尝试在其上调用 setTag(position)
...第 137 行 :)
【参考方案1】:
在 listview 适配器中检查您的 getView()
方法,您已注释了复选框 cb 初始化行
// cb = (CheckBox) vi.findViewById(R.id.checkBox1);
另外 row.xml 布局应该是膨胀的,而不是 gg.xml
所以getView()
应该如下所示:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.row, null);
TextView tv= (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ name1.get(position));
tv1.setText("Phone No :"+ phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
希望对你有所帮助..
【讨论】:
【参考方案2】:试试这个
@Override
public View getView(final int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.row, null);
TextView tv= (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ name1.get(position));
tv1.setText("Phone No :"+ phno1.get(position));
cb.setTag(position);
cb.setOnCheckedChangeListener(this);
cb.setChecked(mCheckStates.get(position, false));
return vi;
【讨论】:
以上是关于Android 列表视图异常的主要内容,如果未能解决你的问题,请参考以下文章