活动未启动,RecyclerView为空

时间:2016-06-26 16:05:37

标签: android

我目前正在学习编写Android应用程序代码,并且自己" The Big Nerd Ranch Guide"

当我完成 CriminalIntent 应用程序后,我开始构建另一个与 CriminalIntent 非常相似的应用程序。 (我只是想练习我学到的东西)

应用应该描述某种项目管理工具。

以下问题:无效!

当我启动应用程序时,我得到一个空白屏幕。整个项目的断点都没有被抓住。

启动活动应该是包含包含RecyclerView的片段的活动。

blank screen

这里是清单代码:

<activity android:name=".ProjectListActivity">
     <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <category android:name="android.intent.category.LAUNCHER"/>
     </intent-filter>
</activity>
<activity
      android:name=".ProjectActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme.NoActionBar">
</activity>

还有继承到其他活动的抽象类:

public abstract class SingleFragmentActivity extends FragmentActivity{

 protected abstract Fragment createFragment();

 @Override
 public void onCreate(Bundle savedInstanceState, PersistableBundle  persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_fragment_content);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_container);

    if (fragment == null) {
        fragment = createFragment();
        fm.beginTransaction()
                .add(R.id.fragment_container, fragment)
                .commit();
    }
 }
}

ProjectListActivity :(应该启动)

public class ProjectListActivity extends SingleFragmentActivity {

 @Override
 protected Fragment createFragment() {
     return new ProjectListFragment();
 }
}

ProjectListFragment:

public class ProjectListFragment extends Fragment {

private RecyclerView mProjectRecyclerView;
private ProjectAdapter mAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_project_list, container, false);

    mProjectRecyclerView = (RecyclerView) v.findViewById(R.id.project_recycler_view);
    mProjectRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    updateUI();

    return v;
}

private void updateUI() {
    ProjectLab projectLab = ProjectLab.getInstance(getActivity());
    List<Project> projects = projectLab.getProjects();

    mAdapter = new ProjectAdapter(projects);
    mProjectRecyclerView.setAdapter(mAdapter);
}




private class ProjectAdapter extends RecyclerView.Adapter<ProjectHolder> {

    private List<Project> mProjects;

    public ProjectAdapter(List<Project> projects) {
        mProjects = projects;
    }

    @Override
    public ProjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View v = layoutInflater.inflate(R.layout.list_item_project, parent, false);

        return new ProjectHolder(v);
    }

    @Override
    public void onBindViewHolder(ProjectHolder holder, int position) {
        Project project = mProjects.get(position);
        holder.bindProject(project);
    }

    @Override
    public int getItemCount() {
        return mProjects.size();
    }
}




private class ProjectHolder extends RecyclerView.ViewHolder {

    private Project mProject;
    private TextView mTitleTextView;
    private TextView mDescriptionTextView;
    private TextView mDateTextView;
    private CheckBox mSolvedCheckBox;

    public ProjectHolder(View itemView) {
        super(itemView);

        mTitleTextView = (TextView)
                itemView.findViewById(R.id.list_item_title_text_View);

        mDescriptionTextView = (TextView)
                itemView.findViewById(R.id.list_item_description_text_view);

        mDateTextView = (TextView)
                itemView.findViewById(R.id.list_item_date_text_view);

        mSolvedCheckBox = (CheckBox)
                itemView.findViewById(R.id.list_item_finished_check_box);
    }

    public void bindProject(Project project) {
        mProject = project;
        mTitleTextView.setText(mProject.getTitle());
        mDescriptionTextView.setText(mProject.getDescription());
        mDateTextView.setText(new SimpleDateFormat("EEEE, dd.MM.yyyy").format(mProject.getDate()));
        mSolvedCheckBox.setChecked(mProject.isFinished());
    }
}
}

对不起,如果我愚蠢的话。 :(

我无法调试它。

希望你能帮助我。

修改

XML的文件:

activity_fragment.xml

<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ndraeger.android.projecttrackstar.ProjectActivity">

<include layout="@layout/activity_fragment_content"/>

activity_fragment_content.xml:

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

fragment_project_list.xml:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                                    android:id="@+id/project_recycler_view"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent" />

list_item_project.xml:

<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">
 <CheckBox
    android:id="@+id/list_item_finished_check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="4dp"/>
 <TextView
    android:id="@+id/list_item_title_text_View"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/list_item_finished_check_box"
    tools:text="Project Title"/>
 <TextView
    android:id="@+id/list_item_description_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/list_item_title_text_View"
    android:layout_toLeftOf="@id/list_item_finished_check_box"
    tools:text="Project Description"/>
 <TextView
    android:id="@+id/list_item_date_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/list_item_description_text_view"
    android:layout_toLeftOf="@id/list_item_finished_check_box"
    tools:text="Project Date"/>

(关闭标签会中断;抱歉,无法更好地格式化)

1 个答案:

答案 0 :(得分:3)

在您的活动中尝试

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

     @Override
     public void onCreate(Bundle savedInstanceState, PersistableBundle  persistentState) {
        super.onCreate(savedInstanceState, persistentState);

     }