如何从android中的基本适配器类调用另一个片段

时间:2016-02-18 10:34:38

标签: android android-fragments

我正在使用自定义适配器填充列表视图。现在我想转到列表项目内按钮的另一个片段。但是我收到了一个名为

的错误
java.lang.IllegalArgumentException: No view found for id 0x7f0d0098 (android.lotus.com.androidmis:id/containerView) for fragment Chat_support_fragment{22168830 #2 id=0x7f0d0098}
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                                  at android.os.Handler.handleCallback(Handler.java:733)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:149)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5061)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
                                                                                  at dalvik.system.NativeStart.main(Native Method)

任何人都可以告诉我如何实现这一点,这是我的基本适配器代码

import java.util.List;
import java.util.Objects;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.lotus.com.androidmis.Chat_support_fragment;
import android.lotus.com.androidmis.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lotus.mis.modle.Complaints.Complaint;
import com.lotus.mis.modle.MyAppApplication;
import com.subtabs.complaints.Complaints_history;
import com.subtabs.complaints.Complaints_pending;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Text;


public class Chat_history_Adapter extends BaseAdapter {

    Context context;
    public List<Complaint> objects;
    ProgressDialog progressdialog;
    String UserID;
    MyAppApplication mApp;
    public Boolean flag_bit=false;
    public int pos;
    FragmentManager fm;

    public Chat_history_Adapter(Context context,int resource,List<Complaint> objects,FragmentManager fm)
    {
        this.context = context;
        this.objects = objects;
        this.fm = fm;
    }

    @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;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        //  View POItemRow = inflater.inflate(R.layout.activity_poitem_listrow,parent,false);

        View PurchaseOrderRow = LayoutInflater.from(context).inflate(R.layout.layout_chat_history_adapter,null);
        pos = position;
        progressdialog = new ProgressDialog(context);

        mApp  = ((MyAppApplication)context.getApplicationContext());
        final Complaint TR = (Complaint)objects.get(position);

        final String RequestNO = TR.getTicketNo();
        final String reqID = TR.getComplaintID();

        Complaint Complaintlist = (Complaint) objects.get(position);

        //TextView txt_PONo = (TextView) PurchaseOrderRow.findViewById(R.id.txt_PONo);
        //TextView txt_SuppName = (TextView) PurchaseOrderRow.findViewById(R.id.txt_SuppName);
        TextView username = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_empname);
        username.setText(mApp.getmGlobal_UserName());
        TextView userid = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_userid);
        userid.setText(mApp.getmGlobal_UserID());
        TextView module_name  = (TextView)PurchaseOrderRow.findViewById(R.id.txt_module_name);
        module_name.setText(Complaintlist.getText2());
        TextView Ref_no = (TextView)PurchaseOrderRow.findViewById(R.id.txt_ref_no);
        Ref_no.setText(Complaintlist.getTicketNo());
        TextView created_by = (TextView)PurchaseOrderRow.findViewById(R.id.txt_created_by_complaint);
        created_by.setText(Complaintlist.getCreatedBy());
        TextView created_Date= (TextView)PurchaseOrderRow.findViewById(R.id.txt_created_date_complaint);
        created_Date.setText(Complaintlist.getCreatedDate());
        TextView complaint_sub = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_sub);
        complaint_sub.setText(Complaintlist.getComplaintName());
        TextView complaint_body = (TextView)PurchaseOrderRow.findViewById(R.id.txt_complaint_body);
        complaint_body.setText(Complaintlist.getText1());
        final Button chat_support = (Button)PurchaseOrderRow.findViewById(R.id.btn_chat_support);
        chat_support.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //calling another fragment from adapter class


                Chat_support_fragment chat_support_fragment = new Chat_support_fragment();
                FragmentTransaction sales_xfragmentTransaction = fm.beginTransaction();
                sales_xfragmentTransaction.replace(R.id.containerView, chat_support_fragment).commit(); //error occurring



                Toast.makeText(context,"Chat support",Toast.LENGTH_SHORT).show();


            }
        });

这是我的被叫活动:

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class Chat_support_fragment extends Fragment {

    View Root_view;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Root_view = inflater.inflate(R.layout.layout_chat_support_fragment,container,false);

        return Root_view;
    }
}

这是我的fragment_layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="android.lotus.com.androidmis.Chat_support_fragment">

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </FrameLayout>


</FrameLayout>

2 个答案:

答案 0 :(得分:0)

只需使用此

即可
  public void onClick(View view) {
     Chat_support_fragment fragment2 = new Chat_support_fragment ();
     FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
     FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction();
     fragmentTransaction.replace(R.id.fragment1, fragment2);
     fragmentTransaction.commit();
    }

此处R.id.fragment1是片段的ID

另外,您确保导入import android.support.v4.app.Fragment; 在您的Chat_support_fragment

希望这有助于你

答案 1 :(得分:0)

android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, new Chat_support_fragment());
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();