【问题标题】:What to do about "No view found for id..." in Android Activity with Fragments如何处理带有片段的 Android Activity 中的“No view found for id ...”
【发布时间】:2016-03-02 21:32:08
【问题描述】:

我希望我的应用程序以large-land 模式向用户显示此屏幕,以便他可以输入、按下按钮并在同一屏幕上查看所有输出。

对于任何其他模式,我希望上面显示的两个窗格一次显示一个,按此顺序,当用户按下按钮时会出现第二个屏幕,然后按下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}

containerdevice.xml(非大型土地)中FrameLayout 中的id,它在MainActivityonCreate 中的片段事务的.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}}
             mActivity=com.dslomer64.fragments.MainActivity@41e4d8a8
             mSavedViewState=
 {2131165188=android.view.AbsSavedState$1@41acfcc0, 2131165189=TextView.SavedState{41ec9d10 start=0 end=0 text=3}, 2131165190=android.view.AbsSavedState$1@41acfcc0}
         #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}}
             mActivity=com.dslomer64.fragments.MainActivity@41e4d8a8
     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:
         mActivity=com.dslomer64.fragments.MainActivity@41e4d8a8
         mContainer=android.app.Activity$1@41e4da18
         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)

【问题讨论】:

标签: java android xml android-fragments


【解决方案1】:

下面的代码产生所需的活动。与Fragments.

MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity
{
    public static boolean PORTRAIT_MODE;

    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.main);

        PORTRAIT_MODE = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT    );

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (PORTRAIT_MODE)
                {
                    EditText inputP = (EditText)findViewById(R.id.input_in_main_portrait);
                    Intent  outputActivity;
                            outputActivity = new Intent(mContext, OutputActivity.class);
                            outputActivity.putExtra("numberOfLetters", inputP.getText().toString());
                    startActivity(outputActivity);
                } else
                {
                    EditText inputL  = (EditText)findViewById(R.id.input_in_main_portrait);
                    TextView outputL = (TextView)findViewById(R.id.txvIn_output_fragment);
                    outputL.setText(DoStuff.createOutput(Integer.parseInt(inputL.getText().toString())));
                }
            }
        });
    }
}

DoStuff.java

public class DoStuff
{
        public static String createOutput(int k){
                String s = "";
                for(int i = 0; i < k; i++)
                        s += "\n" + (char)(i+65);
                return s;
        }
}

OutputActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class OutputActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent i = getIntent();
        String s = i.getStringExtra("numberOfLetters");
        Log.w("OutputActivity=====", "`````TRYING to be Displaying " + s + " letters in what? Portrait? " + MainActivity.PORTRAIT_MODE);

        setContentView(R.layout.output_fragment);

        TextView et = (TextView) findViewById(R.id.txvIn_output_fragment);
        et.setText(DoStuff.createOutput(Integer.parseInt(s)));
    }
}

InputFragment.java

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

public class InputFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        super.onCreateView(inflater, parent, savedInstanceState);

        return inflater.inflate(R.layout.input_fragment, parent, false);
    }

}

OutputFragment.java

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

public class OutputFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        super.onCreateView(inflater, parent, savedInstanceState);

        return inflater.inflate(R.layout.output_fragment, parent, false);
    }
}

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/tvIn_main_portrait"                                                      android:layout_width ="wrap_content" android:layout_height="wrap_content"
        android:text="How many?"
        />
    <EditText                                                                                       android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/input_in_main_portrait"
        android:text="4"
        />
    <Button
        android:id="@+id/btn"                                                                       android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Press to get output"
        />
</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>

布局\main.xml

<LinearLayout                                                                                       xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment                                                                                       android:layout_width="match_parent" android:layout_height="match_parent"
        android:id="@+id/main_fragment"

        android:name="com.dslomer64.fragthis.InputFragment"
        />
</LinearLayout>

layout-land\main.xml

    <LinearLayout                                                                                       xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"
        android:layout_weight="3"
        >

    <fragment                                                                                           android:layout_width="wrap_content" android:layout_height="match_parent"
        android:id="@+id/input_fragment"
        android:name="com.dslomer64.fragthis.InputFragment"

        />

    <fragment                                                                                           android:layout_width="wrap_content" android:layout_height="match_parent"

        android:id="@+id/output_fragment"

        android:name="com.dslomer64.fragthis.OutputFragment"
        />
    </LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多