我的逻辑流程是这样的:
ProjectBuilder.java
使用创建子任务列表
RecyclerView
和OnClickListener
ProjectBuilder.java
活动启动Fragment
。 我已经测试了OnClickListener
正在运行,并且在fragmentTransaction.commit();
中调用行ProjectBuilder.java
时会出现问题。我没有在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
答案 0 :(得分:0)
试试这个
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();
答案 1 :(得分:0)
您应该使用支持库中的Fragment
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager;
您还需要使用getSupportFragmentManager
,因此请使用
FragmentManager fragmentManager = getSupportedFragmentManager();
而不是
FragmentManager fragmentManager = getFragmentManager();
答案 2 :(得分:0)
你必须实施
@Override
public void onFragmentInteraction(Uri uri) {
}
ActivityMain类中的此方法。