调用new Activity时出现IllegalArgumentException

时间:2016-07-19 09:01:05

标签: android android-fragments android-intent illegalargumentexception

在我的MainActivity中,我有一个片段,我将其替换为其他片段类。

替换MainActivity中的片段:

getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, MainFragment.newInstance())
                .commit();

MainActivity的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        app:titleTextColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>

这很好用。

问题出现在我的Fragment类中。我想从片段类中调用一个新的Activty(DetailRfrdPastActivity)。只要我向DetailRfrdPastActivity XML添加一个元素,app就会停止,并显示以下错误:

  

java.lang.IllegalArgumentException:找不到id 0x7f0c0071的视图   (com.scherrer.robin.chvote:id / touch_outside)片段   MainFragment {610d48e#0 id = 0x7f0c0071}                                                                                  在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1098)                                                                                  在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)                                                                                  在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)                                                                                  在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)                                                                                  在   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:532)                                                                                  在android.os.Handler.handleCallback(Handler.java:739)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:148)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

在片段类中,我开始这样的活动:

public class MainFragment extends Fragment implements SearchView.OnQueryTextListener, FlexibleAdapter.OnItemClickListener {

public static MainFragment newInstance() {
    return new MainFragment();
}

private RecyclerView mRecyclerView;
private FlexibleAdapter<IFlexible> mAdapter;
private List<IFlexible> originalItems;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_main, container, false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.lvRef);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);

    this.mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    originalItems = AdapterData.getRfrdList();

    mAdapter = new FlexibleAdapter<>(AdapterData.getRfrdList());
    mAdapter.showAllHeaders();
    mAdapter.initializeListeners(this);

    mRecyclerView.setAdapter(mAdapter);
    mAdapter.enableStickyHeaders();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_main, menu);

    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
}

@Override
public boolean onQueryTextChange(String query) {
    List<IFlexible> copyOriginalItems = new ArrayList<>(originalItems);

    mAdapter.setSearchText(query);
    mAdapter.filterItems(copyOriginalItems);

    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onItemClick(int itemPosition) {
    IFlexible clickedItem = mAdapter.getItem(itemPosition);

    if (clickedItem instanceof ItemRfrdPast) {
        Intent detailRfrdPast = new Intent(getActivity().getApplicationContext(), DetailRfrdPastActivity.class);
        detailRfrdPast.putExtra("pastRfrd", ((ItemRfrdPast) clickedItem).getPastRfrd());
        getActivity().startActivity(detailRfrdPast);
    }

    return true;
}

}

片段的XML:

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/lvRef"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

<include layout="@layout/sticky_header_layout"/>

DetailRfrdPastActivity是MainActivity的子活动。 我无法弄清楚错误的位置,因为片段和被调用的活动应该没有任何关系。

清单:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".activities.MainActivity"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activities.DetailRfrdPastActivity"
        android:parentActivityName=".activities.MainActivity" >

    </activity>
</application>

3 个答案:

答案 0 :(得分:1)

ftw,我认为从ViewHolder设置一个onClickListener并使用所选项调用一个回调就行了。

For Instance:

来自FlexibleAdapter类

public interface OnIFlexibleClickListener {

    void onIFlexibleClick(IFlexible iFlexible);

}

private OnIFlexibleClickListener listener;

public setOnIFlexibleClickListener(OnIFlexibleClickListener listener) {
    this.listener = listener;
}

public IFlexible getIFlexible(int position) {
        return iFlexibleList.get(position); //Your iFlexible List
}

public class FindViewHolder extends RecyclerView.ViewHolder {
    private TextView textView; //Replace the TextView with your own view.

    public FindViewHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView;
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onIFlexibleClick(getIFlexible(getAdapterPosition()));
                }
            }
        });
    }
}

从MainFragment类中,实现OnIFlexibleClickListener

@Override
public void onIFlexibleClick(IFlexible iFlexible) {
    if (iFlexible instanceof ItemRfrdPast) {
        ItemRfrdPast itemRfrdPast = ((ItemRfrdPast) iFlexible).getPastRfrd()

        Intent intent = new Intent(getContext(), DetailRfrdPastActivity.class);
        intent.putExtra(PAST_RFRD, itemRfrdPast); //public static final String PAST_RFRD = "pastRfrd";
        getActivity().startActivityForResult(intent, DetailRfrdPastActivity.REQUEST_CODE); //create a public static final int REQUEST_CODE
    }
}

我认为这应该解决它。欢呼声。

答案 1 :(得分:0)

显示您的MainFragment xml文件中缺少id: id / touch_outside 的视图。请检查xml中的此ID以及包含的XML

答案 2 :(得分:0)

替换

getSupportFragmentManager().beginTransaction() 

getChildFragmentManager().beginTransaction()

为什么会这样?

  

可能是因为它找不到具有给定ID的片段,因为它位于不同的FragmentManager

参考difference