Android 从活动返回后插入新片段导致程序崩溃

Android 从活动返回后插入新片段导致程序崩溃,android,Android,我正在做一个简单的android编程培训。 我的应用程序包含一个人员列表。 单击“get count”,为结果启动一个新的活动,当用户返回到主活动时,它应该在一个新片段中显示人数。 问题是,当FragmentManager添加片段时(在.commit行中),应用程序崩溃。 这是我的代码: 主要活动: package com.example.myapp; import java.util.ArrayList; import java.util.Hashtable; im

我正在做一个简单的android编程培训。 我的应用程序包含一个人员列表。 单击“get count”,为结果启动一个新的活动,当用户返回到主活动时,它应该在一个新片段中显示人数。 问题是,当FragmentManager添加片段时(在.commit行中),应用程序崩溃。 这是我的代码:

主要活动:

package com.example.myapp;

    import java.util.ArrayList;
    import java.util.Hashtable;

    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v4.app.FragmentManager;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.view.View.OnClickListener;

    public class MainActivity extends ActionBarActivity {

        private final int GET_COUNT = 0;
        private Button mAddButton;
        private Button mAddPictureButton;
        private EditText mFirstName;
        private EditText mLastName;
        private EditText mId;

        private ListView listView; 

        protected ArrayList<Person> mPersonList;
        protected ArrayAdapter<Person> mAdapter; 


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView) findViewById(R.id.listView);
            if (mPersonList == null){
                mPersonList = new ArrayList<Person>();
            }

            if (mAdapter == null){
                mAdapter = new ArrayAdapter<Person>(this,
                        android.R.layout.simple_list_item_1,
                        mPersonList);
                listView.setAdapter(mAdapter);
            }

            mAddButton = (Button) findViewById(R.id.add);
            mAddButton.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      mId = (EditText) findViewById(R.id.id);
                      mFirstName = (EditText) findViewById(R.id.firstName);
                      mLastName = (EditText) findViewById(R.id.lastName);

                      Person p1 = new Person(Integer.parseInt(mId.getText().toString()),
                              mFirstName.getText().toString(),
                              mLastName.getText().toString());
                      mPersonList.add(p1);
                      mAdapter.notifyDataSetChanged();
                      mId.setText("");
                      mFirstName.setText("");
                      mLastName.setText("");
                  }
              });

            mAddPictureButton = (Button) findViewById(R.id.getCount);
            mAddPictureButton.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {

                      Intent intent = new Intent(MainActivity.this, GetCount.class);
                      intent.putExtra("personList", mPersonList);
                      startActivityForResult(intent, GET_COUNT);
                  }
              });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // Check which request we're responding to
            if (requestCode == GET_COUNT) {

                FragmentManager fm = getSupportFragmentManager();
                Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
                if (fragment == null) {
                fragment = new BlankFragment();
                fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment)
                .commit();
                }
                TextView tv= (TextView) findViewById(R.id.count);
                int count = data.getIntExtra("count", 0);
                tv.setText("" + count);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @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);
        }
    }
片段布局:

<RelativeLayout 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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.myapp.GetCount" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/counting" />

    </RelativeLayout>
<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="com.example.myapp.CountFragment" >

        <!-- TODO: Update blank fragment layout -->

      <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/count" />

    </FrameLayout>
日志:

11-04 04:34:10.082: D/libEGL(4656): loaded /system/lib/egl/libEGL_genymotion.so
11-04 04:34:10.082: D/(4656): HostConnection::get() New Host Connection established 0xb7f189f8, tid 4656
11-04 04:34:10.110: D/libEGL(4656): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
11-04 04:34:10.114: D/libEGL(4656): loaded /system/lib/egl/libGLESv2_genymotion.so
11-04 04:34:10.222: W/EGL_genymotion(4656): eglSurfaceAttrib not implemented
11-04 04:34:10.222: E/OpenGLRenderer(4656): Getting MAX_TEXTURE_SIZE from GradienCache
11-04 04:34:10.230: E/OpenGLRenderer(4656): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
11-04 04:34:10.230: D/OpenGLRenderer(4656): Enabling debug mode 0
11-04 04:34:39.834: W/EGL_genymotion(4656): eglSurfaceAttrib not implemented
11-04 04:34:41.242: D/AndroidRuntime(4656): Shutting down VM
11-04 04:34:41.242: W/dalvikvm(4656): threadid=1: thread exiting with uncaught exception (group=0xa4c1b648)
11-04 04:34:41.246: E/AndroidRuntime(4656): FATAL EXCEPTION: main
11-04 04:34:41.246: E/AndroidRuntime(4656): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.example.myapp/com.example.myapp.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.os.Looper.loop(Looper.java:137)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at java.lang.reflect.Method.invoke(Method.java:525)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at dalvik.system.NativeStart.main(Native Method)
11-04 04:34:41.246: E/AndroidRuntime(4656): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at com.example.myapp.MainActivity.onActivityResult(MainActivity.java:92)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.Activity.dispatchActivityResult(Activity.java:5322)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
11-04 04:34:41.246: E/AndroidRuntime(4656):     ... 11 more
编辑: 阅读这篇文章:崩溃

检查:

if (requestCode == GET_COUNT) {
  if (resultCode == RESULT_OK) { // add this check
    // then proceed to whatever you want to do
  }
}
另外,确保

TextView tv= (TextView) findViewById(R.id.count);
存在,即BlankFragment在使用前已完全膨胀并可用

最后但并非最不重要的一点是,我认为,您必须在这个
框架布局
部分中有一个
部分-但我不确定这部分

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


我做了你最初的两个建议,但仍然很失败。如果将片段布局放在节中,片段将是静态的(您无法替换它),不需要使用fragmentManager插入。崩溃是否与您在OP中发布的日志相同?如果是,然后我用一篇文章的链接编辑了答案,这篇文章解释了为什么会出现异常以及如何解决它。对它有相同的日志。你读过我建议如何解决这个问题的这篇文章吗?
11-04 04:34:10.082: D/libEGL(4656): loaded /system/lib/egl/libEGL_genymotion.so
11-04 04:34:10.082: D/(4656): HostConnection::get() New Host Connection established 0xb7f189f8, tid 4656
11-04 04:34:10.110: D/libEGL(4656): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
11-04 04:34:10.114: D/libEGL(4656): loaded /system/lib/egl/libGLESv2_genymotion.so
11-04 04:34:10.222: W/EGL_genymotion(4656): eglSurfaceAttrib not implemented
11-04 04:34:10.222: E/OpenGLRenderer(4656): Getting MAX_TEXTURE_SIZE from GradienCache
11-04 04:34:10.230: E/OpenGLRenderer(4656): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
11-04 04:34:10.230: D/OpenGLRenderer(4656): Enabling debug mode 0
11-04 04:34:39.834: W/EGL_genymotion(4656): eglSurfaceAttrib not implemented
11-04 04:34:41.242: D/AndroidRuntime(4656): Shutting down VM
11-04 04:34:41.242: W/dalvikvm(4656): threadid=1: thread exiting with uncaught exception (group=0xa4c1b648)
11-04 04:34:41.246: E/AndroidRuntime(4656): FATAL EXCEPTION: main
11-04 04:34:41.246: E/AndroidRuntime(4656): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.example.myapp/com.example.myapp.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.os.Looper.loop(Looper.java:137)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at java.lang.reflect.Method.invoke(Method.java:525)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at dalvik.system.NativeStart.main(Native Method)
11-04 04:34:41.246: E/AndroidRuntime(4656): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at com.example.myapp.MainActivity.onActivityResult(MainActivity.java:92)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.Activity.dispatchActivityResult(Activity.java:5322)
11-04 04:34:41.246: E/AndroidRuntime(4656):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
11-04 04:34:41.246: E/AndroidRuntime(4656):     ... 11 more
if (requestCode == GET_COUNT) {
  if (resultCode == RESULT_OK) { // add this check
    // then proceed to whatever you want to do
  }
}
TextView tv= (TextView) findViewById(R.id.count);
 <FrameLayout
     android:id="@+id/fragmentContainer"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
 />