如何从android中的基本适配器调用getFragmentManager

时间:2016-02-05 06:40:28

标签: android nullpointerexception fragment

在我的应用程序中,我使用base adapter fragment class内的nullpointer exception填充了列表视图。这里我想通过单击基本适配器上的按钮来调用另一个片段我做了一些事情,但它显示了由于片段管理器而导致fragment manager 任何人都可以告诉我如何从android中的base adapter调用base adapter 这是我package com.adapter.sales; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.lotus.com.androidmis.R; import android.lotus.com.androidmis.Sales; import android.lotus.com.androidmis.Salesteam; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.lotus.mis.modle.UserDetails; public class Emp_Spinner_Adapter extends BaseAdapter { Context context; List<UserDetails> objects; String Flaglist = "3"; FragmentManager fragment; public Emp_Spinner_Adapter(Context context, int resource, List<UserDetails> objects) { this.context = context; this.objects = objects; } @Override public int getCount() { // TODO Auto-generated method stub return objects.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return objects.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @SuppressLint("ResourceAsColor") @Override public View getView(int position, View convertView, ViewGroup parent) { View Row = LayoutInflater.from(context).inflate( R.layout.layout_adapter_sales_team_members, null); TextView txt_item = (TextView) Row.findViewById(R.id.txt_item); ImageView img_count = (ImageView) Row.findViewById(R.id.img_count); ImageView details_sales = (ImageView)Row.findViewById(R.id.img_details_sales); final UserDetails UserDetails_item = (UserDetails) objects .get(position); txt_item.setText(" " + UserDetails_item.getUserName()); final int temp_count = Integer .valueOf(UserDetails_item.GetWorkStatus()); if (temp_count > 0) { img_count.setImageResource(R.drawable.ic_lotus_team); img_count.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = UserDetails_item.getUserName(); //Intent i = new Intent(context, Sales.class); Bundle bundle = new Bundle(); bundle.putString("key_username", UserDetails_item.getUserName()); bundle.putString("key_userid", UserDetails_item.getUserID()); bundle.putString("report_username", Salesteam.txt_levels .getText().toString()); bundle.putString("key_FlagList", Flaglist); Salesteam sales_team = new Salesteam(); sales_team.setArguments(bundle); FragmentTransaction xfragmentTransaction = fragment.beginTransaction(); xfragmentTransaction.replace(R.id.containerView, sales_team).commit(); //i.putExtras(bundle); //context.startActivity(i); } }); } details_sales.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(context, Sales.class); Bundle bundle = new Bundle(); bundle.putString("key_username", UserDetails_item.getUserName()); bundle.putString("key_userid", UserDetails_item.getUserID()); bundle.putString("report_username", Salesteam.txt_levels.getText().toString()); bundle.putString("key_FlagList", Flaglist); Sales sales_fragment = new Sales(); sales_fragment.setArguments(bundle); FragmentTransaction xfragmentTransaction = fragment.beginTransaction(); xfragmentTransaction.replace(R.id.containerView, sales_fragment).commit(); // bundle.putString("report_username", TempUser.getUserName()); // bundle.putString("report_user_id", TempUser.getUserID()); intent.putExtras(bundle); context.startActivity(intent); } }); if (position % 2 == 1) { Row.setBackgroundColor(Color.WHITE); } else { Row.setBackgroundColor(Color.LTGRAY); } img_count.setAlpha(255); txt_item.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); return Row; } }

中的代码
title.setText(data.getJSONObject(0).getString("announcement_title"));//announcement_title
        content.setText(data.getJSONObject(0).getString("announcement_content"));//announcement_content
        date.setText(data.getJSONObject(0).getString("announcement_date"));//announcement_date

3 个答案:

答案 0 :(得分:2)

如果是Emp_Spinner_Adapter构造函数,则在其中传递另一个FragmentManager变量,并将其初始化为预定义的fragmentmanager变量:

 public Emp_Spinner_Adapter(Context context, int resource,
        List<UserDetails> objects,FragmentManager manager ) {
    this.context = context;
    this.objects = objects;
    this.fragment=manager;

}

现在在MainActivity中调用Emp_Spinner_Adapter时,在活动中创建一个FragmentManager变量并初始化它,然后将它传递给object.Hope,这将有所帮助。

答案 1 :(得分:0)

只需将您的活动参考放在Adapter的构造函数

Activity mActivity;

 public Emp_Spinner_Adapter(Activity context, int resource,
            List<UserDetails> objects) {
        this.context = context;
        this.objects = objects;
this.mActivity= context;

    } 

然后将其用作

mActivity.getFragmentManager();

答案 2 :(得分:0)

如果要在适配器内移动片段而不是使用此片段

img_count.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    YourFragment fragment= new YourFragment ();
                Bundle bundle = new Bundle();
                bundle.putString("Id", "1");
                fragment.setArguments(bundle);
                FragmentManager fragmentManager =  ((Activity) mContext).getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).addToBackStack(null).commit();
}
  });