Nikolaus Dräger Nikolaus Dräger - 5 months ago 20
Android Question

Activity not launching, RecyclerView is empty

I'm currently learning to code Android apps and got myself "The Big Nerd Ranch Guide".

As I finished the CriminalIntent app I started building another app which is pretty similar to CriminalIntent. (I just want to practise the things i learned)

The app should describe some sort of project management tool.

Following problem: Nothing works!

When I launch the app I get a blank screen. Breakpoints throughout the whole project aren't being caught.

The launching Activity should be the one including the Fragment that includes the RecyclerView.

blank screen

Here's the Manifest code:

<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>


There's the abstract class that inherits to both other Activities:

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: (should be launched)

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());
}
}
}


Sorry, if I'm dumb. :(

I cannot debug it.

Hope you can help me.

EDIT

XML-Files:

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"/>




(closing tags cut off; sorry, couldn't format it any better)

Answer

In your activity try

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

Not

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

     }
Comments