I have 2 fragments (different classes).
public class TodoMainFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.activity_list, container, false);
return v;
}
}
and
public class TodoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.activity_add_todo, container, false);
return v;
}
}
These both have an XML-file with the layout of each fragment. (This is for my TodoMainFragment). This is where my id=gradientLayout is.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/todo_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#292A2C"
android:padding="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.todo.MainActivity" >
<requestFocus />
<ImageButton
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:background="#00FFFFFF"
android:onClick="changeViewToNewTodo"
android:src="@drawable/ic_launcher" />
<ListView
android:id="@+id/gradientBackground"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/gradient" >
</ListView>
</RelativeLayout>
I also have a main_activity XML-file that is just an empty FrameLayout.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
Then onCreate() in my MainActivity I have
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TodoMainFragment mainFragment = new TodoMainFragment();
TodoFragment editFragment = new TodoFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, mainFragment);
transaction.add(R.id.fragment_container, editFragment);
transaction.replace(R.id.fragment_container, mainFragment);
transaction.commit();
//DB and lists
db = new TodoDbHelper(this);
todos = db.getAllTodos();
nrOfTodos = todos.size();
//For ListView
mListView = (ListView) findViewById(R.id.gradientBackground);
mAdapter = new TodoAdapter<String>(this, R.layout.text_view_item_default);
mListView.setAdapter(mAdapter);
createTodoItems();
}
The problem is that my mListView is null. From what I understood (which is probably wrong), shouldn't
transaction.add(R.id.fragment_container, mainFragment);
make it possible to get the gradientBackground? Now my app crashes on launch with a nullpointer exception but if I change the setContentView to activity_list (the XML for TodoMainFragment) it works. Any ideas are highly appreciated. I'm pretty new to this so go easy on me. :)