调用.commit()时,Android应用程序会抛出致命异常

时间:2014-06-09 15:43:15

标签: android android-fragments

对于Android片段我是相当新的,并且我试图制作一个用户点击“提交”按钮后加载新片段的应用。单击“提交”按钮时,我想要发生的是,为正在显示的现有片段交换新片段。 LogCat显示调用commit()方法时发生的错误。有关为何抛出此错误的任何解释?

MainActivity.java

package com.example.flightfragmenttest;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends ActionBarActivity {

    Spinner modelInputSpinner, specInputSpinner;

    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();

    GensetFragment genFrag = new GensetFragment();
    ResultsFragment resFrag = new ResultsFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.fragmentContainer) != null) {

            if (savedInstanceState != null) {
                return;
            }

            transaction.add(R.id.fragmentContainer, genFrag).commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void search(View v) {

        modelInputSpinner = (Spinner) findViewById(R.id.modelSpinner);
        specInputSpinner = (Spinner) findViewById(R.id.specSpinner);

        addModelItemSelectedListener();
        addSpecItemSelectedListener();

        genFrag.search(modelInputSpinner, specInputSpinner);

        transaction.replace(R.id.fragmentContainer, resFrag);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    private void addSpecItemSelectedListener() {

        specInputSpinner
        .setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent,
                    View view, int position, long id) {

                //
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });

    }

    private void addModelItemSelectedListener() {

        modelInputSpinner
        .setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent,
                    View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });

    }

}

第一个片段

package com.example.flightfragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Spinner;

public class GensetFragment extends Fragment {

    Genset userInput = new Genset();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_genset, container,
                false);


        return rootView;
    }


    public void search(Spinner model, Spinner spec) {

            userInput.setModelNumber(model.getSelectedItem()
                    .toString());
            userInput.setSpecNumber(spec.getSelectedItem()
                    .toString());

            System.out.println(userInput.getModelNumber() + " " + userInput.getSpecNumber());

    }

}

第二片段

package com.example.flightfragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ResultsFragment extends Fragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        System.out.println("Step in to results");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_results, container, false);

        System.out.println("Step in to results");
        return rootView;
    }



}

logcat的

06-09 11:36:00.494: E/AndroidRuntime(2982): FATAL EXCEPTION: main
06-09 11:36:00.494: E/AndroidRuntime(2982): Process: com.example.flightfragmenttest, PID: 2982
06-09 11:36:00.494: E/AndroidRuntime(2982): java.lang.IllegalStateException: Could not execute method of the activity
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.view.View$1.onClick(View.java:3823)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.view.View.performClick(View.java:4438)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.view.View$PerformClick.run(View.java:18422)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.os.Handler.handleCallback(Handler.java:733)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.os.Looper.loop(Looper.java:136)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at java.lang.reflect.Method.invoke(Method.java:515)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at dalvik.system.NativeStart.main(Native Method)
06-09 11:36:00.494: E/AndroidRuntime(2982): Caused by: java.lang.reflect.InvocationTargetException
06-09 11:36:00.494: E/AndroidRuntime(2982):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at java.lang.reflect.Method.invoke(Method.java:515)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.view.View$1.onClick(View.java:3818)
06-09 11:36:00.494: E/AndroidRuntime(2982):     ... 11 more
06-09 11:36:00.494: E/AndroidRuntime(2982): Caused by: java.lang.IllegalStateException: commit already called
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
06-09 11:36:00.494: E/AndroidRuntime(2982):     at com.example.flightfragmenttest.MainActivity.search(MainActivity.java:61)
06-09 11:36:00.494: E/AndroidRuntime(2982):     ... 14 more

2 个答案:

答案 0 :(得分:2)

您无法重复使用片段事务。致电beginTransaction()创建一个新的。

此外,仅在onCreate()或稍后的活动生命周期中访问片段管理器可能更好。

答案 1 :(得分:1)

不要将FragmentTransaction作为您活动的班级成员。每次要更改片段时都会获得一个新的FragmentTransaction,例如

getSupportFragmentManager().beginTransaction()
    .replace(R.id.fragmentContainer, resFrag);
    .addToBackStack(null);
    .commit();