我对Android很新,我正在尝试创建一个列表视图,它将显示我创建的类的详细信息
我的活动是:
public class UserList extends ListActivity {
private ListView lv;
private ArrayList<Player> players;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
lv = (ListView) findViewById(R.id.UserListView);
this.players = new ArrayList<Player>();
this.players.add(new Player("AAAA"));
this.players.add(new Player("BBBB"));
PlayerAdapter pa = new PlayerAdapter(this, R.layout.userlist_list_item, this.players);
lv.setAdapter(pa);
}
我的适配器是这样的:
public class PlayerAdapter extends ArrayAdapter<Player> {
private ArrayList<Player> players;
private Context context;
public PlayerAdapter(Context context, int textViewResourceId,
ArrayList<Player> players) {
super(context, textViewResourceId, players);
// TODO Auto-generated constructor stub
this.players = players;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.userlist_list_item, null);
}
Player p = players.get(position);
TextView name = (TextView) convertView.findViewById(R.id.ListNameData);
name.setText(p.getName());
TextView money = (TextView)convertView.findViewById(R.id.ListMoneyData);
money.setText(""+p.getCurrentmoney());
TextView exp = (TextView)convertView.findViewById(R.id.ListExpData);
exp.setText(""+p.getExp());
return convertView;
}
活动XML只有一个列表视图,列表项XML是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/ListNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textSize="15sp"
android:text="@string/username" />
<TextView
android:id="@+id/ListNameData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/ListMoneyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textSize="15sp"
android:text="@string/money" />
<TextView
android:id="@+id/ListMoneyData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/ListEXPTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textSize="15sp"
android:text="@string/expList" />
<TextView
android:id="@+id/ListExpData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical" >
<Button
android:id="@+id/ListSwitchTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/switchto" />
<Button
android:id="@+id/ListDeleteUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/deleteuser" />
</LinearLayout>
我的堆栈跟踪:
[2014-07-10 20:34:25 - ddms] null
java.lang.NullPointerException
at org.eclipse.debug.internal.ui.DebugUIPlugin.launchInBackground(DebugUIPlugin.java:1286)
at org.eclipse.debug.ui.DebugUITools.launch(DebugUITools.java:753)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.debugRunningApp(AndroidLaunchController.java:178)
at com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController.clientChanged(AndroidLaunchController.java:1749)
at com.android.ddmlib.AndroidDebugBridge.clientChanged(AndroidDebugBridge.java:926)
at com.android.ddmlib.Device.update(Device.java:774)
at com.android.ddmlib.Client.update(Client.java:903)
at com.android.ddmlib.HandleWait.handleWAIT(HandleWait.java:88)
at com.android.ddmlib.HandleWait.handleChunk(HandleWait.java:66)
at com.android.ddmlib.MonitorThread.callHandler(MonitorThread.java:414)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:322)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
它立即崩溃,我无法弄清楚如何解决它。 感谢您阅读并希望帮助我
答案 0 :(得分:0)
好的,您的问题出在您的适配器中。问题出在您的convertView
上;您创建了一个新视图v
,这是您期望使用的视图。相反,您使用始终为null的convertView
。这会导致NPE
或NullPointerException
被抛出。
简单的解决方案就是放弃v
。您实际上不需要为通用视图创建新名称; convertView
可以正常使用。或者,您可以在v
充气后将convertView
分配给v
。
答案 1 :(得分:-1)