Android应用程序在调用.commit()时引发致命异常

Android应用程序在调用.commit()时引发致命异常,android,android-fragments,Android,Android Fragments,我对android片段相当陌生,我正在尝试制作一个应用程序,一旦用户点击提交按钮,它就会加载一个新片段。当单击Submit按钮时,我希望发生的是一个新片段被替换为显示的现有片段。LogCat显示调用commit()方法时发生的错误。有没有解释为什么会抛出此错误 MainActivity.java package com.example.flightfragmenttest; import android.support.v4.app.FragmentTransaction; import an

我对android片段相当陌生,我正在尝试制作一个应用程序,一旦用户点击提交按钮,它就会加载一个新片段。当单击Submit按钮时,我希望发生的是一个新片段被替换为显示的现有片段。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;

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

不能重用片段事务。调用
beginTransaction()
创建一个新的


另外,最好只在活动生命周期的
onCreate()
或更高版本中访问片段管理器。

您不能重用片段事务。调用
beginTransaction()
创建一个新的


另外,最好只在活动生命周期的
onCreate()
或更高版本中访问片段管理器。

您不能重用片段事务。调用
beginTransaction()
创建一个新的


另外,最好只在活动生命周期的
onCreate()
或更高版本中访问片段管理器。

您不能重用片段事务。调用
beginTransaction()
创建一个新的


另外,最好只在活动生命周期的
onCreate()
或更高版本中访问片段管理器。

不要将
片段事务作为活动的类成员。每次您想要更改片段时,获取一个新的
FragmentTransaction
,例如

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

不要将
碎片事务
保留为活动的类成员。每次您想要更改片段时,获取一个新的
FragmentTransaction
,例如

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

不要将
碎片事务
保留为活动的类成员。每次您想要更改片段时,获取一个新的
FragmentTransaction
,例如

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

不要将
碎片事务
保留为活动的类成员。每次您想要更改片段时,获取一个新的
FragmentTransaction
,例如

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

从LogcatPost发布堆栈跟踪从LogcatPost发布堆栈跟踪从LogcatPost发布堆栈跟踪从LogcatPost发布堆栈跟踪非常感谢您的提示。我已经绞尽脑汁和谷歌搜索了好几天,试图找出错误的原因!非常感谢你的提示。我已经绞尽脑汁和谷歌搜索了好几天,试图找出错误的原因!非常感谢你的提示。我已经绞尽脑汁和谷歌搜索了好几天,试图找出错误的原因!非常感谢你的提示。我已经绞尽脑汁和谷歌搜索了好几天,试图找出错误的原因!