处理片段

时间:2017-01-04 12:32:29

标签: android xml android-fragments

无法创建起始视图

这是logcat存储和相关的代码

01/04 17:06:19: Launching app
Cold swapped changes.
$ adb shell am start -n "com.practice.android.criminalintent/com.practice.android.criminalintent.CrimeListActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 17536 on device orangebrute [emulator-5554]
W/System: ClassLoader referenced unknown path: /data/app/com.practice.android.criminalintent-2/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.practice.android.criminalintent, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.practice.android.criminalintent-2/lib/x86
E/FragmentManager: No view found for id 0x7f0b0056 (com.practice.android.criminalintent:id/select_dialog_listview) for fragment CrimeListFragment{394d0f9 #0 id=0x7f0b0056}
E/FragmentManager: Activity state:
D/FragmentManager:   Local FragmentActivity 79f083e State:
D/FragmentManager:     mCreated=truemResumed=false mStopped=false mReallyStopped=false
D/FragmentManager:     mLoadersStarted=false
D/FragmentManager:   Active Fragments in 12d969f:
D/FragmentManager:     #0: CrimeListFragment{394d0f9 #0 id=0x7f0b0056}
D/FragmentManager:       mFragmentId=#7f0b0056 mContainerId=#7f0b0056 mTag=null
D/FragmentManager:       mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
D/FragmentManager:       mAdded=true mRemoving=false mFromLayout=false mInLayout=false
D/FragmentManager:       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
D/FragmentManager:       mRetainInstance=false mRetaining=false mUserVisibleHint=true
D/FragmentManager:       mFragmentManager=FragmentManager{12d969f in HostCallbacks{247c3ec}}
D/FragmentManager:       mHost=android.support.v4.app.FragmentActivity$HostCallbacks@247c3ec
D/FragmentManager:   Added Fragments:
D/FragmentManager:     #0: CrimeListFragment{394d0f9 #0 id=0x7f0b0056}
D/FragmentManager:   FragmentManager misc state:
D/FragmentManager:     mHost=android.support.v4.app.FragmentActivity$HostCallbacks@247c3ec
D/FragmentManager:     mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@247c3ec
D/FragmentManager:     mCurState=2 mStateSaved=false mDestroyed=false
D/FragmentManager:   View Hierarchy:
D/FragmentManager:     com.android.internal.policy.DecorView{ac883b5 V.E..... ... 0,0-0,0}
D/FragmentManager:       android.widget.LinearLayout{27f6a4a V.E..... ... 0,0-0,0}
D/FragmentManager:         android.view.ViewStub{1defdbb G.E..... ... 0,0-0,0 #10203e8 android:id/action_mode_bar_stub}
D/FragmentManager:         android.widget.FrameLayout{6e332d8 V.E..... ... 0,0-0,0 #1020002 android:id/content}
D/FragmentManager:           android.widget.RelativeLayout{34aa631 V.E..... ... 0,0-0,0 #7f0b0057 app:id/activity_criminal}
D/FragmentManager:             android.widget.FrameLayout{51e116 V.E..... ... 0,0-0,0 #7f0b0058 app:id/fragment_container}
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.practice.android.criminalintent, PID: 17536
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.practice.android.criminalintent/com.practice.android.criminalintent.CrimeListActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0b0056 (com.practice.android.criminalintent:id/select_dialog_listview) for fragment CrimeListFragment{394d0f9 #0 id=0x7f0b0056}
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0b0056 (com.practice.android.criminalintent:id/select_dialog_listview) for fragment CrimeListFragment{394d0f9 #0 id=0x7f0b0056}
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1292)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                      at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                      at android.app.Activity.performStart(Activity.java:6679)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated.

java file 1

package com.practice.android.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.List;


public class CrimeListFragment extends Fragment {
    private RecyclerView mCrimeRecyclerView;
    private CrimeAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
        mCrimeRecyclerView = (RecyclerView) view
                .findViewById(R.id.crime_recycler_view);
        mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return view;
    }
    private void updateUI() {
        CrimeLab crimeLab = CrimeLab.get(getActivity());
        List<Crime> crimes = crimeLab.getCrimes();
        mAdapter = new CrimeAdapter(crimes);
        mCrimeRecyclerView.setAdapter(mAdapter);
    }
    private class CrimeHolder extends RecyclerView.ViewHolder {
        private TextView mTitleTextView;
        private Crime mCrime;
        private TextView mDateTextView;
        private CheckBox mSolvedCheckBox;
        public CrimeHolder(View itemView) {
            super(itemView);

            mTitleTextView = (TextView)
                    itemView.findViewById(R.id.list_item_crime_title_text_view);
            mDateTextView = (TextView)
                    itemView.findViewById(R.id.list_item_crime_date_text_view);
            mSolvedCheckBox = (CheckBox)
                    itemView.findViewById(R.id.list_item_crime_solved_check_box);
        }
        public void bindCrime(Crime crime) {
            mCrime = crime;
            mTitleTextView.setText(mCrime.getTitle());
            mDateTextView.setText(mCrime.getDate().toString());
            mSolvedCheckBox.setChecked(mCrime.isSolved());
        }
    }
    private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
        private List<Crime> mCrimes;
        public CrimeAdapter(List<Crime> crimes) {
            mCrimes = crimes;
        }
        @Override
        public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater
                    .inflate(R.layout.list_item_crime, parent, false);
            return new CrimeHolder(view);
        }
        @Override
        public void onBindViewHolder(CrimeHolder holder, int position) {
            Crime crime = mCrimes.get(position);
            holder.bindCrime(crime);
        }
        @Override
        public int getItemCount() {
            return mCrimes.size();
        }
    }
}

相当多的java文件和布局。如果需要,将上传

全新的android.Any帮助表示赞赏:)

2 个答案:

答案 0 :(得分:0)

请提供替换片段的视图的正确ID。

FragmentManager fragmentManager = getSupportFragmentManager();                 fragmentManager.beginTransaction()。replace(R.id.note_list_container,fragment).commit();

请提供容器R.id.note_list_container的正确ID,您希望在其中替换您的片段。它必须在您的活动布局中。框架布局可以是。所以请提供FrameLayout的正确ID。

答案 1 :(得分:0)

我遇到了同样的问题,我的解决方案是使单个窗格id具有相同的名称 在这里查看

one pane

two panes

两个文件中每个帧布局的ID必须相同