2

我需要你的帮助,请不要生我的气!

我的第一个培训项目是创建一个新的 android 布局onCreate并在我的方法中加载

代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.new_layout);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.textView1, new PlaceholderFragment())
                .add(R.id.editText1, new PlaceholderFragment())
                .add(R.id.button1, new PlaceholderFragment())
                .commit();
    }
}

问题是,如果我评论If 语句,应用程序运行得非常好,但如果我删除评论,我无法运行应用程序!!


公共静态类 PlaceholderFragment 扩展片段 {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.new_layout, container, false);
        return rootView;
    }
}

有我的布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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"
    android:orientation="vertical"
    tools:ignore="HardcodedText" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
4

2 回答 2

0

It seems that you created a new layout named new_layout. Inside the new layout, you added some views and you try - with the condition in comments - to add a Fragment in your activity. The FragmentManager try to add the new fragment inside R.id.container as follows:

.add(R.id.container, new PlaceholderFragment()) // add(view id, fragment class)  

Then, you might be a NullPointerException because the activity cannot find this specific id into your layout.
The id container doesn't exist inside new_layout file.


Solution
If you want to use the Fragment Class, I'd suggest you to keep the activity_main layout for:

setContentView(R.layout.activity_main);  

which contains a FrameLayout with the right id. This view will receive and display the fragment and you can have your custom layout inside it as follows:

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        // call the custom layout here
        View rootView = inflater.inflate(R.layout.new_layout, container, false);

        // update, control, the views of the layout here  
        TextView text = (TextView) rootView.findViewById(R.id.textView1);
        text.setText("This is inside fragment");
        // same for other..

        return rootView;
    }
}
于 2014-04-29T23:42:34.620 回答
0

Your layout doesn't have a container to add the Fragment. Add this FrameLayout inside your Layout

<FrameLayout 
   android:id="@+id/container"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

于 2014-04-29T23:43:30.767 回答