package com.test.ExampleListview;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
System.out.println("created arraylist");
final ButtonArrayAdapter adapter = new ButtonArrayAdapter(this,
values);
listview.setAdapter(adapter);
System.out.println("created adapter and set it");
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
list.remove(item);
adapter.notifyDataSetChanged();
}
});
}
}
package com.test.ExampleListview;
/**
* Created by User on 17/06/13.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ButtonArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public ButtonArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, 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.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.buttontext);
ImageView imageView = (ImageView) rowView.findViewById(R.id.buttonimage);
textView.setText(values[position]);
imageView.setImageResource(R.drawable.cool);
return rowView;
}
}
06-07 12:58:43.874 15319-15319/com.test.ExampleListview D/MyActivity: index=2
06-07 12:58:43.924 15319-15319/com.test.ExampleListview D/AndroidRuntime: Shutting down VM
06-07 12:58:43.924 15319-15319/com.test.ExampleListview W/dalvikvm: threadid=1: thread
exiting with uncaught exception (group=0x40a71930)
06-07 12:58:43.944 15319-15319/com.test.ExampleListview E/AndroidRuntime: FATAL EXCEPTION:
main java.lang.NullPointerException at
com.test.ExampleListview.ButtonArrayAdapter.getView(ButtonArrayAdapter.java:31)`
我已经在线阅读了一个教程,它确实帮助了ArrayAdapters的基础知识。
http://www.vogella.com/articles/AndroidListView/article.html
但是,当我尝试使用自定义ArrayAdapter时,应用程序不会出现编译错误但无法启动。我想知道我做了什么明显错误的事情,我花了几个小时测试一些东西,我担心我的基本Android知识意味着我错过了一些明显的东西。我甚至无法在logcat中找到我的println!我不认为它做任何事情,但立即崩溃......
感谢。
答案 0 :(得分:0)
看看这些行:
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.buttontext);
ImageView imageView = (ImageView) rowView.findViewById(R.id.buttonimage);
textView.setText(values[position]);
你在textView上获得了NPE。所以textView可能是指一个无效的对象。这意味着rowView.findViewById(R.id.buttontext)
失败了。检查xml文件中的每个名称或字段,可能会显示错误。
如果您仍有问题,请在此处发布您的文件内容。
答案 1 :(得分:0)
在此代码块中:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.buttontext);
ImageView imageView = (ImageView) rowView.findViewById(R.id.buttonimage);
textView.setText(values[position]);
imageView.setImageResource(R.drawable.cool);
return rowView;
}
尝试以下方法:
values != null
为真。LayoutInflater inflater = LayoutInflater.from(context);
以提高可读性。View rowView = inflater.inflate(R.layout.rowlayout, null, false)
(null父级)。该列表将自行添加视图。答案 2 :(得分:0)
这与我的rowlayout有什么关系吗?
Android工作室说我的“命名空间是未绑定的”?
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+id/buttontext"
android:drawableLeft="@+id/buttonimage"
android:gravity="left|center_vertical">
</RelativeLayout>