03-19 23:38:21.952: E/AndroidRuntime(400): FATAL EXCEPTION: main
03-19 23:38:21.952: E/AndroidRuntime(400): java.lang.NullPointerException
03-19 23:38:21.952: E/AndroidRuntime(400): at net.website.custom_listview.Lazy_Adapter_Custom_Listview$1.onClick(Lazy_Adapter_Custom_Listview.java:75)
03-19 23:38:21.952: E/AndroidRuntime(400): at android.view.View.performClick(View.java:2485)
03-19 23:38:21.952: E/AndroidRuntime(400): at android.view.View$PerformClick.run(View.java:9080)
03-19 23:38:21.952: E/AndroidRuntime(400): at android.os.Handler.handleCallback(Handler.java:587)
03-19 23:38:21.952: E/AndroidRuntime(400): at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 23:38:21.952: E/AndroidRuntime(400): at android.os.Looper.loop(Looper.java:123)
03-19 23:38:21.952: E/AndroidRuntime(400): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 23:38:21.952: E/AndroidRuntime(400): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:38:21.952: E/AndroidRuntime(400): at java.lang.reflect.Method.invoke(Method.java:507)
03-19 23:38:21.952: E/AndroidRuntime(400): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 23:38:21.952: E/AndroidRuntime(400): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 23:38:21.952: E/AndroidRuntime(400): at dalvik.system.NativeStart.main(Native Method)
我不确定下面的代码有什么问题,但我得到null异常(艺术家)
ps:textview确实存在于布局文件中。
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row_custom_listview, null);
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView link = (TextView)vi.findViewById(R.id.txtLink);
// Setting all values in listview
artist.setText(song.get(Main_Activity_Custom_Listview.KEY_TITLE_ENGLISH));
link.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int duration = Toast.LENGTH_SHORT;
TextView tx =(TextView)v.findViewById(R.id.txtLink); //null exception???
TextView artist = (TextView)v.findViewById(R.id.artist); // null exception???
Log.v("link>>", "onItemClick at position" + artist.getText());
//Toast toast = Toast.makeText(PlayListActivity.this, tx.getText(), duration);
//toast.show();
}
});
return vi;
}
答案 0 :(得分:1)
public View getView(final int position, View convertView, ViewGroup parent) {
.
.
.
vi.findViewById(R.id.dots).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MessagesActivity.list.getChildAt(((int) MessagesActivity.list.getItemIdAtPosition(position))-MessagesActivity.list.getFirstVisiblePosition()).findViewById(R.id.top_header).setBackgroundResource(R.layout.list_selector);
}
.
.
.
}
也许这可以帮到你
适合我
答案 1 :(得分:0)
问题在于此行[{1}},您为TextView link.setOnClickListener(....
设置了OnClickListener
,因此TextView将成为传递给link
回调的View参数,你肯定无法从中获取(findViewById)你的艺术家视图。
我认为你打算做onClick
。即便如此,我认为你打算做的是适当的,因为你可以覆盖ListView的vi.setOnClickListener()
回调,你不需要为每个回调设置OnClickListener。你的listview项目就像你在代码片段中所做的那样。
修改强>
看看AdapterView.OnItemClickListener。
然后你会做这样的事情:
onItemClick
答案 2 :(得分:0)
我想你在另一篇文章中提到了类似的东西。我曾提到你不应该这样做。您需要阅读onClick()功能。
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row_custom_listview, null);
//Make them final so that you can access them in your onClick()
final TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
final TextView tx =(TextView)vi.findViewById(R.id.txtLink);
TextView link = (TextView)vi.findViewById(R.id.txtLink);
// Setting all values in listview
artist.setText(song.get(Main_Activity_Custom_Listview.KEY_TITLE_ENGLISH));
link.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int duration = Toast.LENGTH_SHORT;
Log.v("link>>", "onItemClick at position" + artist.getText());
}
});
return vi;
}