我有2个片段(不同的类)。
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;
}
}
和
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;
}
}
这两个文件都有一个XML文件,其中包含每个片段的布局。 (这是我的TodoMainFragment)。这是我的id = gradientLayout的地方。
<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>
我还有一个main_activity XML文件,它只是一个空的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>
然后在我的MainActivity中使用onCreate()
@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();
}
问题是我的mListView为空。根据我的理解(这可能是错误的),不应该
transaction.add(R.id.fragment_container, mainFragment);
是否可以获得gradientBackground?现在我的应用程序在启动时崩溃并出现一个nullpointer异常,但是如果我将setContentView更改为activity_list(TodoMainFragment的XML),它就可以工作了。任何想法都受到高度赞赏。我对此很陌生,所以对我很轻松。 :)