0

アクティビティ内にフラグメントを作成しようとしていますが、次の例外が発生しました:

java.lang.IllegalArgumentException: ID のビューが見つかりません

これは activity_main.xml です

<RelativeLayout 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">

    <EditText
        android:id="@+id/editText_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_str"
        android:ems="10" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_num"
        android:text="Update" />

</RelativeLayout>

content_layout.xml です

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</FrameLayout>

MainActivity.java です

package com.example.example24;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends FragmentActivity {
    private EditText editText_str, editText_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText_str = (EditText) findViewById(R.id.editText_str);
        editText_num = (EditText) findViewById(R.id.editText_num);
        Button btn_update = (Button) findViewById(R.id.btn_update);
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyFragment newFragment = new MyFragment();
                Bundle args = new Bundle();
                args.putString("str", editText_str.getText().toString());
                args.putInt("num", Integer.valueOf(editText_num.getText().toString()));
                newFragment.setArguments(args);
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

MyFragment.java です。

package com.example.example24;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String str = getArguments().getString("str");
        int num = getArguments().getInt("num");
        return inflater.inflate(R.layout.content_layout, container, false);
    }
}
4

2 に答える 2

0

プログラムでフラグメントをビュー グループに追加するには、xml にビュー グループが必要です。

http://developer.android.com/guide/components/fragments.html

add()の代わりに使用replace()

コンテナに追加された既存のフラグメントを置き換える場合に使用replace()します。

だからに変更

   transaction.add(R.id.fragment_container, newFragment);

に渡される最初の引数add()は、リソース ID で指定されViewGroupfragmentを配置する場所であり、2 番目のパラメータは追加するフラグメントです。

    <RelativeLayout 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">

    <EditText
        android:id="@+id/editText_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_str"
        android:ems="10" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText_num"
        android:text="Update" />
    <LinearLayout
            android:id="@+id/fragment_container"
            android:layout_below="@+id/btn_update"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
    </LinearLayout>


</RelativeLayout>
于 2013-10-01T19:05:21.260 に答える
0

レイアウトを使用していて、レイアウトの一部ではないR.layout.activity_mainFragment を設定しようとしています。R.id.fragment_container

于 2013-10-01T18:47:19.523 に答える