android上的setContentView麻烦

时间:2014-04-29 23:38:22

标签: android setcontentview

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

我的第一个培训项目是创建新的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();
    }
}

问题是,如果我评论 如果声明该应用运行完全正常,但如果我删除评论我无法运行APP! !


    public static class PlaceholderFragment extends Fragment {

    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>

2 个答案:

答案 0 :(得分:0)

您似乎创建了一个名为new_layout的新布局。在新布局中,您添加了一些视图,并尝试使用评论中的条件在您的活动中添加FragmentFragmentManager尝试在 R.id.container 中添加新片段,如下所示:

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

然后,您可能是NullPointerException,因为活动无法在您的布局中找到此特定ID id container文件中不存在new_layout


<强>解决方案
如果你想使用片段类,我建议你保留activity_main布局:

setContentView(R.layout.activity_main);  

包含具有正确ID的FrameLayout。此视图将接收并显示片段,您可以在其中包含自定义布局,如下所示:

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

答案 1 :(得分:0)

您的布局没有容器来添加Fragment。在FrameLayout

中添加此Layout
<FrameLayout 
   android:id="@+id/container"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">