Android找不到id的视图

时间:2014-10-14 12:29:00

标签: android eclipse android-layout android-fragments fragment

我刚开始使用Eclipse中的android开发,并且正在关注开发人员教程。在试验片段时,我在Logcat中遇到了这个错误"找不到id的视图...."我试图通过几个类似的帖子来查看这个错误,但不知何故,解决方案对我不起作用(或者我错过了这一点)。无论如何这是我的情况。我有一个活动和两个片段,我动态想要显示。问题是当我用第二个片段替换第一个片段然后点击后退按钮时我得到了这个错误。以下是相关代码:

OnCreate我的主要活动(名为MainAcitity2):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);

    // Check that the activity is using the layout version with
    // the fragment_container FrameLayout
    if (findViewById(R.id.fragment_container) != null) {

        if (savedInstanceState != null) {
            return;
        }

        myFirstFragment firstFragment = new myFirstFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        firstFragment.setArguments(getIntent().getExtras());

        transaction.add(R.id.fragment_container, firstFragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }        
}

这是我主要活动的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp2.MainActivity2" >     

在myFirstFragment中,我使用代码在Fragments之间进行通信,即通过在我的主要活动中实现的指定和接口,以及在我的情况下创建seconde Fragments并传递一些字符串文本。她在我的主要活动中实现了这个界面:

    public void OnMyButtonClicked(String thetext){

    Bundle args = new Bundle();
    args.putString("THETEXT", thetext);

    mySecondFragment newFragment = new mySecondFragment();
    newFragment.setArguments(args);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    transaction.commit();       

}

这是mySecondFragment的onCreateView代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

    String message = "NIET ONTVANGEN";

    message=this.getArguments().getString("THETEXT");           
    System.out.println("DE MESSAGE NA OVERDRACHT IS:"+message);

    //create the TextView
    TextView textView = new TextView(getActivity());
    textView.setTextSize(40);
    textView.setText(message);

    getActivity().setContentView(textView);

    View view = inflater.inflate(R.layout.my_second_fragment,container, false);

    return view;
}

到目前为止一切正常。首先显示第一个片段。然后,当我单击按钮并调用实现的界面时,第二个片段显示并替换第一个片段(并显示我通过的一些文本),但是当我单击后退按钮时,会给出错误。下面是一些更可能相关的代码

myFirstFragment的布局xml

    <?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/firstfragment_view"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText 
        android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />    

    <Button
        android:id="@+id/text_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:layout_toRightOf="@id/edit_message" />

    <Button 
        android:id="@+id/audio_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_play"
        android:layout_below="@id/text_input" />  

</RelativeLayout>

mySecondFragment的布局:

    <?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/secondfragment_view"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myfirstapp2.DisplayMessageActivity" >
    <ImageView 
        android:id="@+id/myLargeImage"      
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

这是LogCat的输出:

   10-14 14:47:35.936: E/FragmentManager(26088): No view found for id 0x7f05003c (com.example.myfirstapp2:id/fragment_container) for fragment myFirstFragment{42a4bb70 #0 id=0x7f05003c}
10-14 14:47:35.936: E/FragmentManager(26088): Activity state:
10-14 14:47:35.946: E/InputEventSender(26088): Exception dispatching finished signal.
10-14 14:47:35.946: E/MessageQueue-JNI(26088): Exception in MessageQueue callback: handleReceiveCallback
10-14 14:47:35.976: E/MessageQueue-JNI(26088): java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp2:id/fragment_container) for fragment myFirstFragment{42a4bb70 #0 id=0x7f05003c}
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1533)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:498)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:180)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v7.app.ActionBarActivity.onBackPressed(ActionBarActivity.java:251)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.app.Activity.onKeyUp(Activity.java:2295)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.KeyEvent.dispatch(KeyEvent.java:3197)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.app.Activity.dispatchKeyEvent(Activity.java:2529)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchKeyEvent(ActionBarActivityDelegateICS.java:258)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2165)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4635)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4602)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4167)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4221)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4190)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4301)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4198)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4358)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4167)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4221)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4190)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4198)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4167)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4221)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4190)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4334)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4494)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2267)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1915)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1906)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2244)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.os.MessageQueue.nativePollOnce(Native Method)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.os.MessageQueue.next(MessageQueue.java:138)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.os.Looper.loop(Looper.java:123)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at android.app.ActivityThread.main(ActivityThread.java:5586)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at java.lang.reflect.Method.invokeNative(Native Method)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at java.lang.reflect.Method.invoke(Method.java:515)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
10-14 14:47:35.976: E/MessageQueue-JNI(26088):  at dalvik.system.NativeStart.main(Native Method)
10-14 14:47:35.976: E/AndroidRuntime(26088): FATAL EXCEPTION: main
10-14 14:47:35.976: E/AndroidRuntime(26088): Process: com.example.myfirstapp2, PID: 26088
10-14 14:47:35.976: E/AndroidRuntime(26088): java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp2:id/fragment_container) for fragment myFirstFragment{42a4bb70 #0 id=0x7f05003c}
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1533)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:498)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:180)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v7.app.ActionBarActivity.onBackPressed(ActionBarActivity.java:251)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.app.Activity.onKeyUp(Activity.java:2295)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.KeyEvent.dispatch(KeyEvent.java:3197)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.app.Activity.dispatchKeyEvent(Activity.java:2529)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchKeyEvent(ActionBarActivityDelegateICS.java:258)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2165)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4635)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4602)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4167)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4221)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4190)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4301)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4198)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4358)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4167)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4221)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4190)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4198)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4167)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4221)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4190)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4334)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4494)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2267)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1915)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1906)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2244)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.os.MessageQueue.nativePollOnce(Native Method)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.os.MessageQueue.next(MessageQueue.java:138)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.os.Looper.loop(Looper.java:123)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at android.app.ActivityThread.main(ActivityThread.java:5586)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at java.lang.reflect.Method.invokeNative(Native Method)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at java.lang.reflect.Method.invoke(Method.java:515)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
10-14 14:47:35.976: E/AndroidRuntime(26088):    at dalvik.system.NativeStart.main(Native Method)

我做错了什么?

0 个答案:

没有答案