我想在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。请帮帮我。
答案 0 :(得分:2)
您正在使用context
,此时此版本将为null
,因为您未分配给它。
您应该可以使用activity
方法中的getview
。即用context
替换听众中的activity
。
答案 1 :(得分:2)
private Activity context;
是您的问题,已经是null
。
在YourActivityName.this
内使用Toast
。
答案 2 :(得分:0)
Activity testActivity;
在List<RssFeedStructure> imageAndTexts1 = null;
然后在构造函数中初始化它。
public RssReaderListAdapter(Activity activity,
List<RssFeedStructure> imageAndTexts) {
super(activity, 0, imageAndTexts);
imageAndTexts1 = imageAndTexts;
testActivity=activity; // Newly added
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
Now you can use the Toast message like this,
Toast.makeText(testActivity, imageAndTexts1.get(position).getUrl().toString(), Toast.LENGTH_SHORT).show();
答案 3 :(得分:0)
使用此代码。
Toast.makeText(ActivityName.this, ""+imageAndTexts1.get(position).getUrl().toString(), Toast.LENGTH_SHORT).show();
首先更改是ActivityName.this
,第二个是:&#34;&#34; + imageAndTexts1.get(position).getUrl()。toString()
答案 4 :(得分:0)
Toast.makeText(UrActivity.this; "UrlValue is" + imageAndTexts1.get(position).getUrl(), 3000).show();