java.lang.IllegalArgumentException:找不到视图

时间:2015-04-23 22:08:46

标签: android android-layout android-fragments android-activity

我想要做的事情:从Master / Detail流程项目开始,我试图让用户按下ActionMenu中的一个项目,该项目应该显示一个新数据可以的片段投入。

问题:我尝试启动片段时出错。我做错了什么?

粘贴代码后,我觉得我弄得一团糟。仍然需要帮助。

错误:

  

java.lang.IllegalArgumentException:找不到ID为0x7f090040(com.example.androidtest:id / AddItem_fragment)的片段,用于片段AddItem {5b161e7#1 id = 0x7f090040}

ItemListActivity.java:

public class ItemListActivity extends ActionBarActivity implements
    ItemListFragment.Callbacks {

private boolean mTwoPane;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_list);

    if (findViewById(R.id.item_detail_container) != null) {
        mTwoPane = true;
        ((ItemListFragment) getSupportFragmentManager().findFragmentById(
                R.id.item_list)).setActivateOnItemClick(true);
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_actions, menu);
    setTitle("Shoppinglistan");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_add:
            openAddItem();
            return true;
        case R.id.action_send:
            //openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onItemSelected(String id) {
    if (mTwoPane) {
        Bundle arguments = new Bundle();
        arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.item_detail_container, fragment).commit();            

    } else {
        Intent detailIntent = new Intent(this, ItemDetailActivity.class);
        detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
        startActivity(detailIntent);
    }
}

public void openAddItem() {
    AddItem additem = new AddItem();
    getSupportFragmentManager().beginTransaction()
    .add(R.id.AddItem_fragment, additem).commit();
}

public static class AddItem extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.additem_layout,
                container, false);
        return rootView;
    }
}

}

activity_item_detail.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.androidtest.ItemDetailActivity"
tools:ignore="MergeRootFrame" >

    <fragment 
    android:name="com.example.androidtest.AddItem"
    android:id="@+id/AddItem_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp" 

    />

</FrameLayout>

additem_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

你的问题就在这一行

public void openAddItem() {
AddItem additem = new AddItem();
getSupportFragmentManager().beginTransaction()
.add(R.id.AddItem_fragment, additem).commit();
}

你已经在这里了

 <fragment 
android:name="com.example.androidtest.AddItem"
android:id="@+id/AddItem_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" 
/>

你无法以编程方式重新创建它,所有你需要做的就是删除以编程方式添加的片段,这将是存在的,因为(用不同的语言表示它的堆栈)..

修改

要解决您的评论,首先在调用ItemDetailFragment时为您的片段添加标记,例如"ItemDetailFragment"&amp;而不是openAddItem()中的先前代码替换它们

public void openAddItem() {
getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().
       findFragmentByTag("ItemDetailFragment"));
getSupportFragmentManager().popBackStackImmediate(); // 
// if your fragment is still not showing then uncomment the below line
//getSupportFragmentManager().beginTransaction().show(getSupportFragmentManager().
     findFragmentById(the_id_of_ur_fragment));
}

希望它足够好

答案 1 :(得分:0)

从文档中,添加方法:

  

abstract FragmentTransaction add(int containerViewId,Fragment fragment)

     

使用null标记调用add(int,Fragment,String)。

在您的代码中:

public void openAddItem() {
    AddItem additem = new AddItem();
    getSupportFragmentManager().beginTransaction().add(R.id.AddItem_fragment, additem).commit();
}

此代码将在您的视图中找到标识为R.id.AddItem_frament的视图,并且它将尝试将与传递的片段关联的视图附加到该视图。由于在您的布局中没有ViewGroup作为容器,因此启动了合法的IllegalArgumentException,因为ID不存在。你要做的是提供一个容器,你可以在其中添加所有片段并传递给这个id的事务的add方法。