Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
适配器中的Android上下文操作栏?_Android_Android Toolbar_Contextual Action Bar - Fatal编程技术网

适配器中的Android上下文操作栏?

适配器中的Android上下文操作栏?,android,android-toolbar,contextual-action-bar,Android,Android Toolbar,Contextual Action Bar,我正在尝试在我的应用程序中实现上下文actionbar。然而,当我尝试在适配器中使用Startactionmode方法时,我找不到这样做的方法 我试过: mActionMode = ((MainActivity)c).startActionMode(new MyActionModeCallback()); 这给了我一个错误 java.lang.NullPointerException:尝试调用虚拟方法的 在CustomerListAdapter$ViewHolder$1.onClick(Cus

我正在尝试在我的应用程序中实现上下文actionbar。然而,当我尝试在适配器中使用Startactionmode方法时,我找不到这样做的方法

我试过:

mActionMode = ((MainActivity)c).startActionMode(new MyActionModeCallback());
这给了我一个错误

java.lang.NullPointerException:尝试调用虚拟方法的 在CustomerListAdapter$ViewHolder$1.onClick(CustomerListAdapter.java:62)

爪哇语:62是

mActionMode = ((MainActivity)c).startActionMode(new MyActionModeCallback());
顺便说一句,我正在导入android.view.ActionMode;它是mainactivity实现actionbaractivity中的一个选项卡片段

我的适配器类

public class CustomerListAdapter extends RecyclerView.Adapter<CustomerListAdapter.ViewHolder>{

public ViewHolder(final View itemView,int ViewType, final Context c) {   
下面是我的适配器代码

    package com.thecueapps.cue_business;

    import android.content.Context;
    import android.support.v7.widget.CardView;
    import android.support.v7.widget.RecyclerView;
    import android.view.ActionMode;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.parse.ParseUser;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;


    public class CustomerListAdapter extends RecyclerView.Adapter<CustomerListAdapter.ViewHolder>{
    protected List<ParseUser> mCustomers;

    Context context;
    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView Name_fiedl;
        TextView Number_ppl_field;
        TextView Time_field;
        Context contxt;
        Context contxt1;
        private CardView cardView;
        private ImageView circleview;
        public ActionMode mActionMode;




        public ViewHolder(final View itemView,int ViewType, final Context c) {                 // Creating ViewHolder Constructor with View and viewType As a parameter
            super(itemView);
            contxt = c;

            //itemView.setClickable(true);
            //itemView.setOnClickListener(this);
            // Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
            Name_fiedl = (TextView) itemView.findViewById(R.id.text_name); // Creating TextView object with the id of textView from item_row.xml
            Number_ppl_field = (TextView) itemView.findViewById(R.id.number_ppl);// Creating ImageView object with the id of ImageView from item_row.xml
            circleview=(ImageView) itemView.findViewById(R.id.circle_ppl);
            Time_field=(TextView) itemView.findViewById(R.id.text_time);

            itemView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                   /* Toast.makeText(itemView.getContext(), "hi "
                            , Toast.LENGTH_SHORT).show();*/


    mActionMode = ((MainActivity)c).startActionMode(new       MyActionModeCallback());

                }
            });

            //Add Expand Area to a Card
            cardView = (CardView) itemView.findViewById(R.id.card_view);
            cardView.setRadius(0);

        }



    }



        CustomerListAdapter(List<ParseUser> customer){ // MyAdapter Constructor with titles and icons parameter
        // titles, icons, name, email, profile pic are passed from the main activity as we
       mCustomers=customer;



        //in adapter
    }



    @Override
    public CustomerListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row,parent,false); //Inflating the layout

            ViewHolder vhItem = new ViewHolder(v,viewType,context); //Creating ViewHolder and passing the object of type view

            return vhItem; // Returning the created object

            //inflate your layout and pass it to view holder
    }

    @Override
    public void onBindViewHolder(CustomerListAdapter.ViewHolder holder, int position) {

        ParseUser  customer=mCustomers.get(position);
        holder.Name_fiedl.setText(customer.getString(ParseConstants.KEY_USER_REAL_NAME)); // Setting the Text with the array of our Titles
        holder.Number_ppl_field.setText(String.valueOf(customer.getInt(ParseConstants.KEY_CUSTOMER_NUMBER_PPL)));// Settimg the image with array of our icons
        String time=customer.getString(ParseConstants.KEY_CUSTOMER_TIME);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm aa");
        try {
            Date date = dateFormat.parse(time);

             String out= dateFormat2.format(date);
            holder.Time_field.setText(out);
            //Log.e("Time", out);
        } catch (ParseException e) {
        }


        if(customer.getInt(ParseConstants.KEY_CUSTOMER_NUMBER_PPL)==3){
            holder.circleview.setImageResource(R.color.lightblue500);
        }
        else if(customer.getInt(ParseConstants.KEY_CUSTOMER_NUMBER_PPL)==4){
            holder.circleview.setImageResource(R.color.lightblue500);
        }
        else if(customer.getInt(ParseConstants.KEY_CUSTOMER_NUMBER_PPL)==5){
            holder.circleview.setImageResource(R.color.lightgreen500);
        }
        else if(customer.getInt(ParseConstants.KEY_CUSTOMER_NUMBER_PPL)>5){
            holder.circleview.setImageResource(R.color.lightgreen500);
        }

    }
    @Override
    public int getItemCount() {

        return mCustomers.size(); // the number of items in the list will be +1 the titles including the header view.
    }
    public void remove(String item) {
        int position = mCustomers.indexOf(item);
        mCustomers.remove(position);
        notifyItemRemoved(position);
    }
   }

class MyActionModeCallback implements ActionMode.Callback{


    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        actionMode.getMenuInflater().inflate(R.menu.menu_login, menu);
        return false;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        actionMode.setTitle("hihi");
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {

    }
}
  • 在我的适配器中:

    CustomerListAdapter(List < ParseUser > customer, Activity Activity)
    { // MyAdapter Constructor with titles and icons parameter
        // titles, icons, name, email, profile pic are passed from the main activity as we
        mCustomers = customer;
        mActivity= Activity;
    
    
        //in adapter
    } 
    
  • 但我仍然面临错误,显示非静态字段不能应用于静态字段

    这里:

    mActionMode = ((MainActivity)c).startActionMode(new MyActionModeCallback());
    
    上下文的
    c
    对象可能为空

    要在
    CustomerListAdapter
    类中获取活动上下文,请使用类构造函数:

    private Acivity mActivity;
    
    public CustomerListAdapter(Activity mActivity){
      this.mActivity=mActivity;
      ....
    }
    
    使用
    mActivity
    从活动调用
    startActionMode
    方法:

     mActionMode = mActivity.startActionMode(new MyActionModeCallback());
    
    在创建
    CustomerListAdapter
    class对象时,使用
    this
    MainActivity
    传递活动上下文:

    CustomerListAdapter adapter=new CustomerListAdapter(this);
    
    活跃

     public class Main extends AppCompatActivity {
         public Toolbar toolbar;
        AdapterEXT mAdapter;
        .....
         @Override
            protected void onCreate(Bundle savedInstanceState) {
         toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                assert getSupportActionBar() != null;
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
        ......
         List<DataXXX> data = new ArrayList<>();
         mAdapter = new AdapterEXT(Main.this , toolbar, data);
    

    你能提供一些上下文的周围代码吗?嗨,我已经添加了一些关于适配器的代码class@user3818938:可能
    c
    null
    ya,有什么我可以解决的吗?@user3818938:show
    CustomerListAdapter
    classcode@user3818938:使用适配器的单独类实现我的应答,在私人活动方面,我有点困惑;公共CustomerListAdapter(Activity mActivity){this.mActivity=mActivity;…}@user3818938:为什么要混淆?在CustomerListAdapter中添加构造函数,如我的回答中所示。我已编辑了该问题。我想我们快到了。只需要解决非静态问题issue@user3818938:将
    mActivity
    声明为
    static
    或从
    ViewHolder
    类中删除
    static
     mActionMode = mActivity.startActionMode(new MyActionModeCallback());
    
    CustomerListAdapter adapter=new CustomerListAdapter(this);
    
     public class Main extends AppCompatActivity {
         public Toolbar toolbar;
        AdapterEXT mAdapter;
        .....
         @Override
            protected void onCreate(Bundle savedInstanceState) {
         toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                assert getSupportActionBar() != null;
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayShowHomeEnabled(true);
        ......
         List<DataXXX> data = new ArrayList<>();
         mAdapter = new AdapterEXT(Main.this , toolbar, data);
    
     class AdapterEXT extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
            public Context context;
            public Toolbar toolbar;
         AdapterEXT (Context context, Toolbar toolbar, List<DataXXX> dataXXX) {
                 this.context = context;
                this.toolbar=toolbar;
                this.dataXXX = dataXXX;
            }
    
    toolbar.setTitle(context.getString(R.string.YourTXTfromXML));