从helper类(适配器)启动Intent返回java.lang.NullPointerException:

时间:2017-10-10 05:11:53

标签: java android android-intent nullpointerexception

我正在创建一个使用Hashtag的应用。到目前为止,我使用AutoLinkTextClick!并且一切正常,除非有人点击已识别的主题标签时应用程序崩溃并出现错误:

  

java.lang.NullPointerException:尝试调用虚方法   ' java.lang.String android.content.Context.getPackageName()'在null   对象参考

所以我几乎已经阅读了StackOverflow关于可能导致它的原因并且人们建议使用getContext()getActvity(),但这两者都不起作用,因为我试图从一个适配器,而不是来自扩展活动或片段的类,所以我非常感谢你们的帮助。

非常感谢您在这件事上的时间和帮助。下面是我正在显示的适配器和错误的代码:)

package com.android.msahakyan.expandablenavigationdrawer.adapter;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v7.app.AlertDialog;

import com.android.msahakyan.expandablenavigationdrawer.AboutUsActivity;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.msahakyan.expandablenavigationdrawer.R;
import com.android.msahakyan.expandablenavigationdrawer.activity.FeedImageView;
import com.android.msahakyan.expandablenavigationdrawer.model.Feed;
import com.android.msahakyan.expandablenavigationdrawer.others.AppController;
import com.luseen.autolinklibrary.AutoLinkMode;
import com.luseen.autolinklibrary.AutoLinkOnClickListener;
import com.luseen.autolinklibrary.AutoLinkTextView;

import java.util.List;

public class FeedsListAdapter extends     RecyclerView.Adapter<FeedsListAdapter.MyHolder> {
private Activity activity;
private LayoutInflater inflater;
private List<Feed> feedItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
//ImageLoader imageLoader = VolleySingleton.getInstance(context).getImageLoader()
public FragmentManager f_manager;



public FeedsListAdapter(Activity activity, List<Feed> feedItems) {
    this.activity = activity;
    this.feedItems = feedItems;
}


//INITIALIE VH
//@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.feeds_home_row,parent,false);
    MyHolder holder=new MyHolder(v);
    return holder;
}

//BIND DATA
// @Override
public void onBindViewHolder(MyHolder holder, int position) {

    //ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    if (imageLoader == null)
      imageLoader = AppController.getInstance().getImageLoader();

    final Feed f = feedItems.get(position);

    //Post images
    holder.picture.setImageUrl(f.getPictureUrl(), imageLoader); // displays company profile image on post
    holder.sms_image.setImageUrl(f.getMessagePictureUrl(), imageLoader); // displays post image on post

    //autoLinkTextView.enableUnderLine();
    holder.messages.addAutoLinkMode(AutoLinkMode.MODE_HASHTAG);
holder.messages.setAutoLinkText("this the sample message #test i need to test hashtag onClink and launch intent"); //
    holder.messages.setAutoLinkOnClickListener(new AutoLinkOnClickListener() {
        @Override
        public void onAutoLinkTextClick(AutoLinkMode autoLinkMode, String matchedText) {
            Intent intent = new Intent(activity, AboutUsActivity.class);
            intent.putExtra("companyId", f.getCompanyId());
            activity.startActivity(intent);
        }
    });
    //holder.messages.setText(f.getMessage());
    //Linkify.addLinks(holder.messages, Linkify.ALL); //detect link that are in a message
    holder.date_created.setText(f.getDateCreated());
    holder.company.setText(f.getCompany());

}


/*
TOTAL ITEMS
 */

//@Override
public int getItemCount() {
    return feedItems.size();

}

/*
ADD DATA TO ADAPTER
 */
public void add(Feed s) {
    feedItems.add(s);
    notifyDataSetChanged();
}

/*
CLEAR DATA FROM ADAPTER
 */
public void clear() {
    feedItems.clear();
    notifyDataSetChanged();
}

/*
VIEW HOLDER CLASS
 */
class MyHolder extends RecyclerView.ViewHolder {

    FeedImageView sms_image;
    NetworkImageView picture;
    //TextView messages;
    AutoLinkTextView messages;
    //AutoLinkTextView autoLinkTextView;
    TextView date_created;
    TextView company;


    public MyHolder(View convertView) {
        super(convertView);


        this.sms_image = (FeedImageView) convertView.findViewById(R.id.sms_image);
        NetworkImageView thumbnail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
        this.picture = (NetworkImageView) convertView.findViewById(R.id.picture);
        //this.messages = (TextView) convertView.findViewById(R.id.messages);
        this.messages = (AutoLinkTextView) convertView.findViewById(R.id.messages);
        this.date_created = (TextView) convertView.findViewById(R.id.date_created);
        this.company = (TextView) convertView.findViewById(R.id.company);
        //TextView status = (TextView) convertView.findViewById(R.id.status);


    }

}

}

正在显示的错误是

10-10 08:08:06.128  28368-28368/com.android.msahakyan.expandablenavigationdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.android.msahakyan.expandablenavigationdrawer, PID: 28368
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ComponentName.<init>(ComponentName.java:128)
        at android.content.Intent.<init>(Intent.java:4666)
        at com.android.msahakyan.expandablenavigationdrawer.adapter.FeedsListAdapter$1.onAutoLinkTextClick(FeedsListAdapter.java:87)
        at com.luseen.autolinklibrary.AutoLinkTextView$1.onClick(AutoLinkTextView.java:77)
        at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
        at com.luseen.autolinklibrary.LinkTouchMovementMethod.onTouchEvent(LinkTouchMovementMethod.java:38)
        at android.widget.TextView.onTouchEvent(TextView.java:8689)
        at android.view.View.dispatchTouchEvent(View.java:9787)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2686)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2335)
        at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2461)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1780)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2853)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
        at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2422)
        at android.view.View.dispatchPointerEvent(View.java:10018)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4738)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4596)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4101)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4154)
        at android.view

1 个答案:

答案 0 :(得分:2)

您不应从RecyclerView适配器启动活动。相反,您需要创建一个侦听器,其中适配器所在的Activity / Fragment需要实现该侦听器。适配器应该只执行其工作,显示适配器的内容。

这样的事情:

public class FeedsListAdapter extends RecyclerView.Adapter<FeedsListAdapter.ViewHolder> {
    // ...

    /***** Creating OnItemClickListener *****/

    // Define listener member variable
    private OnItemClickListener listener;

    // Define the listener interface
    public interface OnItemClickListener {
        void onAutoLinkTextClick(String companyId);
    }

    // Define the method that allows the parent activity or fragment to define the listener
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void onBindViewHolder(MyHolder holder, int position) {

        ...
        holder.messages.setAutoLinkOnClickListener(new AutoLinkOnClickListener() {
            @Override
            public void onAutoLinkTextClick(AutoLinkMode autoLinkMode, String matchedText) {
              listener.onAutoLinkTextClick(f.getCompanyId());

            }
        });
        ...

    }
}

然后在Activity或Fragment上设置监听器:

FeedsListAdapter adapter = ...;
adapter.setOnItemClickListener(new ContactsAdapter.OnItemClickListener() {
    @Override
    public void onAutoLinkTextClick(String companyId) {
      Intent intent = new Intent(activity, AboutUsActivity.class);
       intent.putExtra("companyId", f.getCompanyId());
      activity.startActivity(intent);
    }
});