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 ステートメントにコメントを付けると、アプリは完全に正常に動作しますが、コメントを削除すると、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>
4

2 に答える 2

0

という名前の新しいレイアウトを作成したようですnew_layout。新しいレイアウト内で、いくつかのビューを追加し、コメントの条件を使用してFragment、アクティビティに a を追加しようとしました。次のように、 R.id.containerFragmentManager内に新しいフラグメントを追加しようとします。

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

次に、NullPointerExceptionアクティビティがこの特定の ID をレイアウト内で見つけることができないため、あなたは a である可能性があります。
はファイルid container内に存在しませんnew_layout


解決策
Fragment Class を使用する場合は、次のactivity_mainレイアウト を維持することをお勧めします。

setContentView(R.layout.activity_main);  

FrameLayout正しいIDを持つaが含まれています。このビューはフラグメントを受け取って表示し、次のようにその中にカスタム レイアウトを含めることができます。

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

あなたのレイアウトには、を追加するコンテナがありませんFragment。これをあなたの中に追加しFrameLayoutてください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 に答える