我希望我的應用程序向large-land
模式中的用戶顯示此屏幕,以便他可以輸入,按下按鈕,並查看輸出全部在同一個屏幕上。如何處理「沒有查找ID ...」在Android活動與片段
對於任何其它模式時,欲被顯示一次一個以上所示的兩個窗格,以該順序,其中,所述第二屏幕出現當用戶按下該按鈕時,在此之後按下back
按鈕返回到「輸入」屏幕左側:
我做了什麼工作,在顯示第二屏幕上正確的輸出點,但隨後我得到「未找到ID視圖「container
然後發生異常
這裏上述用於第一屏幕是xml
:
device(large-land).xml
<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:id ="@+id/linear_layout_container_in_large_land_device_xml"
android:weightSum ="3"
>
<fragment
android:name="com.dslomer64.fragments.InputFragment" android:layout_weight ="2" android:layout_width="wrap_content" android:layout_height="match_parent"
android:id ="@+id/inputFragment"
tools:layout ="@layout/input_fragment"
/>
<fragment
android:name="com.dslomer64.fragments.OutputFragment" android:layout_weight ="1" android:layout_width="wrap_content" android:layout_height="match_parent"
android:id ="@+id/outputFragment"
tools:layout ="@layout/output_fragment"
/>
</LinearLayout>
device.xml
(用於非large-land
)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/container"
/>
input_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id ="@+id/tvInMainFragment" android:layout_width ="wrap_content" android:layout_height="wrap_content"
android:text="How many?"
/>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/etxIn_input_fragment"
android:text="3"
/>
<Button
android:id="@+id/btnIn_input_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Press to get output in OtherFragment"
/>
</LinearLayout>
output_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:id="@+id/txvIn_output_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Output will go here in OutputFragment"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device);
if (findViewById(R.id.container) != null) {
InputFragment inputFragment;
inputFragment = new InputFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, inputFragment)
.commit();
}
}
}
InputFragment.java
public class InputFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater,
ViewGroup viewGroup,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.input_fragment, viewGroup, false);
Button btn;
btn = (Button)view.findViewById(R.id.btnIn_input_fragment);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
OutputFragment outputFragment;
outputFragment = new OutputFragment();
if (getFragmentManager().findFragmentById(R.id.inputFragment) == null) {
getActivity().setContentView(R.layout.device);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, outputFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
getActivity().setContentView(R.layout.input_fragment);
EditText etx = (EditText) getActivity().findViewById(R.id.etxIn_input_fragment);
fillFragmentOtherWithInfo(etx.getText().toString());
}
});
return view;
}
public void fillFragmentOtherWithInfo(String ks)
{
getActivity().setContentView(R.layout.output_fragment);
TextView tvOther = (TextView)getActivity().findViewById(R.id.txvIn_output_fragment);
String s = "";
for(int i = 0; i < parseInt(ks); i++)
s += ("\n" + (char)(i+65));
tvOther.setText(s);
}
}
會發生什麼事都好,在顯示第二屏幕上的輸出點。它顯示正確,但隨後我馬上拿到前面帶有此消息的異常,我不知道該做些什麼什麼:
03-02 15:59:52.987 7901-7901/com.dslomer64.fragments E/FragmentManager: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ecc848 #1 id=0x7f070000}
container
是FrameLayout
在device.xml
的id
(非-large-land),在MainActivity
onCreate
的片段事務的.add
部分中引用。如果我使用.replace
而不是.add
,我會得到同樣的錯誤。
以下是錯誤的整個文本:
No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
Activity state:
Local Activity 41e4d8a8 State:
mResumed=true mStopped=false mFinished=false
mLoadersStarted=true
mChangingConfigurations=false
mCurrentConfig={1.15 311mcc480mnc en_US ldltr sw600dp w600dp h888dp 213dpi lrg port finger -keyb/v/h -nav/h skin=/system/framework/framework-res.apk s.16}
Active Fragments in 41e4d9a0:
#0: InputFragment{41e6d1d8 #0 id=0x7f070000}
mFragmentId=#7f070000 mContainerId=#7f070000 mTag=null
mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=2
mAdded=false mRemoving=true mResumed=false mFromLayout=false mInLayout=false
mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
mRetainInstance=false mRetaining=false mUserVisibleHint=true
mFragmentManager=FragmentManager{41e4d9a0 in MainActivity{41e4d8a8}}
[email protected]
mSavedViewState=
{[email protected], 2131165189=TextView.SavedState{41ec9d10 start=0 end=0 text=3}, [email protected]}
#1: OutputFragment{41ebf0f0 #1 id=0x7f070000}
mFragmentId=#7f070000 mContainerId=#7f070000 mTag=null
mState=0 mIndex=1 mWho=android:fragment:1 mBackStackNesting=1
mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
mRetainInstance=false mRetaining=false mUserVisibleHint=true
mFragmentManager=FragmentManager{41e4d9a0 in MainActivity{41e4d8a8}}
[email protected]
Added Fragments:
#0: OutputFragment{41ebf0f0 #1 id=0x7f070000}
Back Stack:
#0: BackStackEntry{41e6d288 #0}
mName=null mIndex=0 mCommitted=true
Operations:
Op #0: REPLACE InputFragment{41e6d1d8 #0 id=0x7f070000}
Back Stack Indices:
#0: BackStackEntry{41e6d288 #0}
#1: BackStackEntry{41ebf8d0 #1}
FragmentManager misc state:
[email protected]
[email protected]
mCurState=5 mStateSaved=false mDestroyed=false
ViewRoot:
mAdded=true mRemoved=false
mConsumeBatchedInputScheduled=false
mPendingInputEventCount=0
mProcessInputEventsScheduled=false
mTraversalScheduled=false
android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
Choreographer:
mFrameScheduled=false
mLastFrameTime=20289147 (31 ms ago)
View Hierarchy:
com.android.internal.policy.impl.PhoneWindow$DecorView{41e50e28 V.E..... R....... 0,0-800,1216}
com.android.internal.widget.ActionBarOverlayLayout{41e51e40 V.ED.... ........ 0,0-800,1216 #1020317 android:id/action_bar_overlay_layout}
android.widget.FrameLayout{41e53548 V.E..... ......I. 0,108-800,1216 #1020002 android:id/content}
android.widget.LinearLayout{41ec5d60 V.E..... ......I. 0,0-800,1108}
android.widget.TextView{41ec6080 V.ED.... ........ 0,0-14,101 #7f070007 app:id/txvIn_output_fragment}
com.android.internal.widget.ActionBarContainer{41e53a68 V.ED.... ........ 0,33-800,108 #1020318 android:id/action_bar_container}
com.android.internal.widget.ActionBarView{41e565d0 V.E..... ........ 0,0-800,75 #1020319 android:id/action_bar}
android.widget.LinearLayout{41e56d40 V.....C. ........ 11,0-248,75}
com.android.internal.widget.ActionBarView$HomeView{41e5abe0 V.E..... ........ 0,0-75,75}
android.widget.ImageView{41e5b0b0 G.ED.... ......I. 0,0-0,0 #102025a android:id/up}
android.widget.ImageView{41e5c1a8 V.ED.... ........ 5,5-69,69 #102002c android:id/home}
android.widget.LinearLayout{41e5d680 V.E..... ........ 75,21-237,54}
android.widget.TextView{41e5db28 V.ED.... ........ 0,0-151,33 #1020265 android:id/action_bar_title}
android.widget.TextView{41e5eb40 G.ED.... ......I. 0,0-0,0 #1020266 android:id/action_bar_subtitle}
com.android.internal.view.menu.ActionMenuView{41ea45b0 V.ED.... ......ID 800,0-800,75}
com.android.internal.widget.ActionBarContextView{41e5f438 G.E..... ......ID 0,0-0,0 #102031a android:id/action_context_bar}
com.android.internal.widget.ActionBarContainer{41e61e58 G.ED.... ......ID 0,0-0,0 #102031b android:id/split_action_bar}
Looper (main, tid 1) {41e42ba0}
(Total messages: 0, idling=false, quitting=false)
id=1: thread exiting with uncaught exception (group=0x41905cf8)
id=1: uncaught exception occurred
.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:698)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
id=1: calling UncaughtExceptionHandler
FATAL EXCEPTION: main
Process: com.dslomer64.fragments, PID: 24665
java.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:698)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
這可能會幫助你。非常相似http://stackoverflow.com/questions/9975523/fragments-and-orientation-change – Kuffs