【发布时间】:2015-11-15 11:50:47
【问题描述】:
我的逻辑流程是这样的:
- Activity
ProjectBuilder.java创建一个子任务列表,使用RecyclerView和OnClickListener - 用户选择其中一个子任务,
ProjectBuilder.javaActivity 启动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