如何用android中的另一个片段替换片段?

时间:2014-04-13 12:38:21

标签: android android-intent android-fragments

我尝试通过按下按钮将片段替换为另一个片段。

按钮的代码是:

nextB.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            if(client_fizic_existentCB.isChecked())
                {
                    Fragment fragment2 = new ComandaClientFizicExistentFragment();
                    FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_container, fragment2); 
                    transaction.commit();
                }

        }
    });

代码不起作用。当我按下第一个片段中的按钮时,应用程序会崩溃。

有人知道为什么吗?

logcat的:

 04-13 12:46:30.902: W/dalvikvm(986): threadid=1: thread exiting with uncaught     exception (group=0x40a13300)
  04-13 12:46:30.914: E/AndroidRuntime(986): FATAL EXCEPTION: main
  04-13 12:46:30.914: E/AndroidRuntime(986): java.lang.NullPointerException
  04-13 12:46:30.914: E/AndroidRuntime(986):    at   info.androidhive.slidingmenu.ComandaClientFizicExistentFragment.onCreateView(ComandaClientF izicExistentFragment.java:47)
 04-13 12:46:30.914: E/AndroidRuntime(986):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
 04-13 12:46:30.914: E/AndroidRuntime(986):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
 04-13 12:46:30.914: E/AndroidRuntime(986):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
04-13 12:46:30.914: E/AndroidRuntime(986):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
04-13 12:46:30.914: E/AndroidRuntime(986):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
04-13 12:46:30.914: E/AndroidRuntime(986):  at android.os.Handler.handleCallback(Handler.java:615)
04-13 12:46:30.914: E/AndroidRuntime(986):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 12:46:30.914: E/AndroidRuntime(986):  at android.os.Looper.loop(Looper.java:137)
04-13 12:46:30.914: E/AndroidRuntime(986):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-13 12:46:30.914: E/AndroidRuntime(986):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 12:46:30.914: E/AndroidRuntime(986):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 12:46:30.914: E/AndroidRuntime(986):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-13 12:46:30.914: E/AndroidRuntime(986):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-13 12:46:30.914: E/AndroidRuntime(986):  at dalvik.system.NativeStart.main(Native Method)

第一个片段,ComandaNouaFragment:

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;


public class ComandaNouaFragment extends Fragment {

public CheckBox client_fizic_nouCB;
//public CheckBox client_fizic_existentCB;
public CheckBox client_juridic_nouCB;
public CheckBox client_juridic_existentCB;
public Button nextB;
public Intent i=null;

public ComandaNouaFragment(){

}

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


    View rootView = inflater.inflate(R.layout.fragment_comanda_noua1, container, false);

    client_fizic_nouCB=(CheckBox)rootView.findViewById(R.id.client_fizic_nouCB);
     final CheckBox client_fizic_existentCB=(CheckBox)rootView.findViewById(R.id.client_fizic_existentCB);
    client_juridic_nouCB=(CheckBox)rootView.findViewById(R.id.client_juridic_nouCB);
    client_juridic_existentCB=(CheckBox)rootView.findViewById(R.id.client_juridic_existentCB);
    nextB=(Button)rootView.findViewById(R.id.nextB);

    nextB.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(client_fizic_nouCB.isChecked())
            {}
            else
            if(client_fizic_existentCB.isChecked())
                {
                    Fragment fragment2 = new ComandaClientFizicExistentFragment();

                    FragmentTransaction transaction = getFragmentManager()
                            .beginTransaction();
                    transaction.replace(R.id.frame_container, fragment2)
                            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                            .addToBackStack(null).commit();
                }
            else
            if(client_juridic_nouCB.isChecked())
            {}
            else
            if(client_juridic_existentCB.isChecked())
            { }
        }
    });


    return rootView;
}

}

第二个片段,ComandaClientFizicExistentFragment:

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
import android.widget.Button;
 import android.widget.EditText;
 import android.widget.RadioButton;



public class ComandaClientFizicExistentFragment extends Fragment{


public Button selecteazaB;
public EditText cautareClientET;
public RadioButton dupaNumeRB;
public RadioButton dupaCnpRB;
private String numeClient="";
private String cnpClient="";
private Intent i=null;



public ComandaClientFizicExistentFragment(){}

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

    View rootView = inflater.inflate(R.layout.fragment_select_client_fizic_existent, container, false);
    selecteazaB=(Button)rootView.findViewById(R.id.selecteazaB);
    cautareClientET=(EditText)rootView.findViewById(R.id.cautareClientTV);
    dupaNumeRB=(RadioButton)rootView.findViewById(R.id.dupaNumeRB);
    dupaCnpRB=(RadioButton)rootView.findViewById(R.id.dupaCnpRB);



    if(dupaNumeRB.isChecked())
        numeClient=cautareClientET.getText().toString();
    else
        cnpClient=cautareClientET.getText().toString();

    selecteazaB.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //MainActivity ma= new MainActivity();
            //ma.trecere();
            //final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
            //ft.replace(R.id.details, new ComandaNouaAlegereProduseFragment, "NewFragmentTag"); 
            //ft.commit();

            //i = new Intent(getActivity(), ComandaNouaAlegereProduseFragment.class);
            //startActivity(i); 
            Fragment fragment3 = new ComandaNouaAlegereProduseFragment();
             FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_container, fragment3); 
                transaction.commit();


        }
    });

0 个答案:

没有答案