【问题标题】:fragmentTransaction.commit() crashes appfragmentTransaction.commit() 使应用程序崩溃
【发布时间】:2015-11-15 11:50:47
【问题描述】:

我的逻辑流程是这样的:

  1. ActivityProjectBuilder.java 创建一个子任务列表,使用 RecyclerViewOnClickListener
  2. 用户选择其中一个子任务,ProjectBuilder.java Activity 启动Fragment

我已经测试了OnClickListener 是否可以正常工作,而当我的ProjectBuilder.java 中调用fragmentTransaction.commit(); 行时,就会出现问题。我在我的 logcat 中看不到任何错误,也没有抛出任何错误。应用程序崩溃了。

ProjectBuilder.java

public class ProjectBuilder extends AppCompatActivity implements ProjectBuilderAdapter.ProjectBuilderClickListener {

private Toolbar mToolbar;
private Context thisContext;

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;

private ProjectBuilderAdapter myProjectBuilderAdapter;

//for Log.d ; debugging
private static final String TAG = "ProjectBuilder";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    thisContext = ProjectBuilder.this;

    //Set view and populate title for the toolbar
    setContentView(R.layout.activity_project_builder);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    mToolbar.setNavigationIcon(R.drawable.ic_home);
    getSupportActionBar().setTitle(R.string.new_project_home);

    //Build the list of items in RecyclerView
    mRecyclerView = (RecyclerView) findViewById(R.id.project_builder_list);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    myProjectBuilderAdapter = new ProjectBuilderAdapter(constructProjectBuilderList(),thisContext);
    mRecyclerView.setAdapter(myProjectBuilderAdapter);
    myProjectBuilderAdapter.setProjectBuilderClickListener(this);
}


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

    return true;
}

// This method creates an ArrayList that has ProjectBuilderListItem objects
public List<ProjectBuilderListItem> constructProjectBuilderList() {

    List<ProjectBuilderListItem> list = new ArrayList<>();

    //code to build list goes here

    return list;
}

@Override
public void onListItemClicked(View view, int position) {

    ImageSetBuilder newFragment = new ImageSetBuilder();

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, newFragment);

    Log.d(TAG, "I'm here in ProjectBuilder's onListItemClicked");

    //Problem lies here
    fragmentTransaction.commit();

}

activity_project_builder.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_project_builder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.congyitan.tncassistant.ProjectBuilder">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#2E5CB8"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/project_builder_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"/>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

Logcat

11-15 19:11:14.708 30771-30771/com.example.congyitan.tncassistant D/ProjectBuilderAdapter: I'm here in Adapter's onClick and getAdapterPosition is 5
11-15 19:11:14.708 30771-30771/com.example.congyitan.tncassistant D/ProjectBuilder: I'm here in ProjectBuilder's onListItemClicked

【问题讨论】:

  • 您也可以发布您的堆栈跟踪吗?
  • 刚刚做了,但我怀疑它是否非常有用。我没有看到任何错误。

标签: android android-fragments android-activity


【解决方案1】:

试试这个

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (shouldAddToBackStack)
                ft.addToBackStack(tag);
            else {
                getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
            ft.replace(R.id.container, fragment, tag)
                    .commit();
            getSupportFragmentManager().executePendingTransactions();

【讨论】:

  • 对不起,但这对我不起作用。 shouldAddToBackStack 和 tag 是什么?
  • shouldAddToBackStack 是一个布尔变量,如果您想将其添加到后台堆栈中,您可以将其设为 true
  • 标签是简单的字符串,你可以给任何东西
  • 添加try catch并尝试捕获异常
【解决方案2】:

您应该使用支持库中的Fragment

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager;

你还需要使用getSupportFragmentManager所以,使用

FragmentManager fragmentManager = getSupportedFragmentManager();

而不是

FragmentManager fragmentManager = getFragmentManager();

【讨论】:

    【解决方案3】:

    你必须执行

    @Override
        public void onFragmentInteraction(Uri uri) {
    }
    

    ActivityMain 类中的这个方法。

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 2017-11-10
      • 2012-08-18
      • 2012-02-12
      • 2014-03-18
      • 2014-11-14
      • 2018-12-05
      • 2012-08-25
      • 1970-01-01
      相关资源
      最近更新 更多