我想在 setOnClickListener() 中显示imageAndTexts1.get(position).getUrl()的变量值。
这是我的代码
public class RssReaderListAdapter extends ArrayAdapter<RssFeedStructure> {
List<RssFeedStructure> imageAndTexts1 = null;
public RssReaderListAdapter(Activity activity,
List<RssFeedStructure> imageAndTexts) {
super(activity, 0, imageAndTexts);
imageAndTexts1 = imageAndTexts;
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.rssfeedadapter_layout, null);
TextView textView = (TextView) rowView.findViewById(R.id.feed_text);
TextView timeFeedText = (TextView) rowView
.findViewById(R.id.feed_updatetime);
ImageView imageView = (ImageView) rowView.findViewById(R.id.feed_image);
try {
Log.d("rssfeed", "imageAndTexts1.get(position).getImgLink() :: "
+ imageAndTexts1.get(position).getImgLink() + " :: "
+ imageAndTexts1.get(position).getTitle()+ " :: "
+ imageAndTexts1.get(position).getUrl());
textView.setText(imageAndTexts1.get(position).getTitle());
SpannableString content = new SpannableString(imageAndTexts1.get(
position).getPubDate());
content.setSpan(new UnderlineSpan(), 0, 13, 0);
timeFeedText.setText(content);
textView.setOnClickListener(new View.OnClickListener() {
private Activity context;
@Override
public void onClick(View v) {
Toast.makeText(context.getApplicationContext(), imageAndTexts1.get(position).getUrl().toString(), Toast.LENGTH_SHORT).show();
/* String url = imageAndTexts1.get(position).getUrl().toString();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i); */
}
});
当我运行此代码时,它可以显示来自 Log.d() 的文本。但是当我在模拟器中单击 textView 时,它会显示这样的错误。
07-02 09:49:10.844: E/AndroidRuntime(968): FATAL EXCEPTION: main
07-02 09:49:10.844: E/AndroidRuntime(968): java.lang.NullPointerException
07-02 09:49:10.844: E/AndroidRuntime(968): at com.amit.adapter.RssReaderListAdapter$1.onClick(RssReaderListAdapter.java:84)
07-02 09:49:10.844: E/AndroidRuntime(968): at android.view.View.performClick(View.java:4204)
07-02 09:49:10.844: E/AndroidRuntime(968): at android.view.View$PerformClick.run(View.java:17355)
07-02 09:49:10.844: E/AndroidRuntime(968): at android.os.Handler.handleCallback(Handler.java:725)
07-02 09:49:10.844: E/AndroidRuntime(968): at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 09:49:10.844: E/AndroidRuntime(968): at android.os.Looper.loop(Looper.java:137)
07-02 09:49:10.844: E/AndroidRuntime(968): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-02 09:49:10.844: E/AndroidRuntime(968): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 09:49:10.844: E/AndroidRuntime(968): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 09:49:10.844: E/AndroidRuntime(968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-02 09:49:10.844: E/AndroidRuntime(968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-02 09:49:10.844: E/AndroidRuntime(968): at dalvik.system.NativeStart.main(Native Method)
在 log.d() 中,它可以无错误地显示文本。但是在 setOnClickListener() 中,当我单击列表中的文本时,它会显示 NullPointerException。请帮我。