0

我想在 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。请帮我。

4

5 回答 5

2

您正在使用contextnull因为您没有分配给它,所以此时将使用它。

您应该可以使用activityfromgetview方法。即context用 . 替换你的听众activity

于 2013-07-02T09:59:57.550 回答
2
private Activity context;

是你的问题,已经是null

YourActivityName.this您的Toast.

于 2013-07-02T10:00:03.383 回答
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();
于 2013-07-02T10:01:56.847 回答
0

使用此代码。

    Toast.makeText(ActivityName.this, ""+imageAndTexts1.get(position).getUrl().toString(), Toast.LENGTH_SHORT).show();

第一个变化是 ActivityName.this

第二个是:""+imageAndTexts1.get(position).getUrl().toString()

于 2013-07-02T10:03:13.577 回答
0

您的代码中的问题是您使用 getActivityContext() 有时会导致问题...而不是使用

     Toast.makeText(UrActivity.this; "UrlValue is" + imageAndTexts1.get(position).getUrl(), 3000).show();
于 2013-07-02T10:10:54.633 回答