Android:java.lang.RuntimeException:无法启动活动ComponentInfo:找不到视图

时间:2013-11-19 19:30:58

标签: android

使用Android Studio(AS)0.3.6,尝试在http://developer.android.com/training/index.html进行教程。不幸的是,教程中的类扩展了Activity,AS中生成的代码中的类扩展了ActionBarActivity。此外,AS现在使用片段,教程没有解决这些问题。我尽可能地单独留下AS生成的代码,并调整教程提供的内容。显然我犯了一个错误(或错误),因为当我在文本字段中放入一些文本并按下模拟器上的按钮时,我得到“不幸的是,App One已停止”。

我正在附加崩溃的屏幕日志。请告诉我应该发布的代码位。我有Main和DisplayMessage Activity java文件以及main和DisplayMessage的活动和片段xml文件(以及清单,菜单和字符串xml文件等)。

我感谢任何意见。我的轮子旋了一个星期。谢谢 - 彼得

11-19 14:19:03.460      884-884/com.example.appone E/FragmentManager﹕ No view found for id 0x7f07003c (com.example.appone:id/container) for fragment PlaceholderFragment{b1e54ae0 #0 id=0x7f07003c}
11-19 14:19:03.460      884-884/com.example.appone E/FragmentManager﹕ Activity state:
11-19 14:19:03.460      884-884/com.example.appone D/FragmentManager﹕ Local FragmentActivity b1dfadc0 State:
11-19 14:19:03.490      884-884/com.example.appone D/FragmentManager﹕ mCreated=truemResumed=false mStopped=false mReallyStopped=false
11-19 14:19:03.500      884-884/com.example.appone D/FragmentManager﹕ mLoadersStarted=false
11-19 14:19:03.500      884-884/com.example.appone D/FragmentManager﹕ Active Fragments in b1dfb030:
11-19 14:19:03.500      884-884/com.example.appone D/FragmentManager﹕ #0: PlaceholderFragment{b1e54ae0 #0 id=0x7f07003c}
11-19 14:19:03.520      884-884/com.example.appone D/FragmentManager﹕ mFragmentId=#7f07003c mContainerId=#7f07003c mTag=null
11-19 14:19:03.520      884-884/com.example.appone D/FragmentManager﹕ mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
11-19 14:19:03.540      884-884/com.example.appone D/FragmentManager﹕ mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
11-19 14:19:03.540      884-884/com.example.appone D/FragmentManager﹕ mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
11-19 14:19:03.560      884-884/com.example.appone D/FragmentManager﹕ mRetainInstance=false mRetaining=false mUserVisibleHint=true
11-19 14:19:03.560      884-884/com.example.appone D/FragmentManager﹕ mFragmentManager=FragmentManager{b1dfb030 in DisplayMessageActivity{b1dfadc0}}
11-19 14:19:03.560      884-884/com.example.appone D/FragmentManager﹕ mActivity=com.example.appone.DisplayMessageActivity@b1dfadc0
11-19 14:19:03.560      884-884/com.example.appone D/FragmentManager﹕ Added Fragments:
11-19 14:19:03.560      884-884/com.example.appone D/FragmentManager﹕ #0: PlaceholderFragment{b1e54ae0 #0 id=0x7f07003c}
11-19 14:19:03.580      884-884/com.example.appone D/FragmentManager﹕ FragmentManager misc state:
11-19 14:19:03.580      884-884/com.example.appone D/FragmentManager﹕ mActivity=com.example.appone.DisplayMessageActivity@b1dfadc0
11-19 14:19:03.600      884-884/com.example.appone D/FragmentManager﹕ mContainer=android.support.v4.app.FragmentActivity$2@b1dfb0a8
11-19 14:19:03.610      884-884/com.example.appone D/FragmentManager﹕ mCurState=2 mStateSaved=false mDestroyed=false
11-19 14:19:03.610      884-884/com.example.appone D/FragmentManager﹕ View Hierarchy:
11-19 14:19:03.610      884-884/com.example.appone D/FragmentManager﹕ com.android.internal.policy.impl.PhoneWindow$DecorView{b1e43ca0 V.E..... ... 0,0-0,0}
11-19 14:19:03.630      884-884/com.example.appone D/FragmentManager﹕ com.android.internal.widget.ActionBarOverlayLayout{b1e44350 V.ED.... ... 0,0-0,0 #1020310 android:id/action_bar_overlay_layout}
11-19 14:19:03.650      884-884/com.example.appone D/FragmentManager﹕ android.widget.FrameLayout{b1e44fd8 V.E..... ... 0,0-0,0 #1020002 android:id/content}
11-19 14:19:03.650      884-884/com.example.appone D/FragmentManager﹕ android.widget.TextView{b1e43828 V.ED.... ... 0,0-0,0}
11-19 14:19:03.660      884-884/com.example.appone D/FragmentManager﹕ com.android.internal.widget.ActionBarContainer{b1e45450 V.ED.... ... 0,0-0,0 #1020311 android:id/action_bar_container}
11-19 14:19:03.660      884-884/com.example.appone D/FragmentManager﹕ com.android.internal.widget.ActionBarView{b1e459d0 V.E..... ... 0,0-0,0 #1020312 android:id/action_bar}
11-19 14:19:03.670      884-884/com.example.appone D/FragmentManager﹕ android.widget.LinearLayout{b1e45fb8 VFE...C. ... 0,0-0,0}
11-19 14:19:03.670      884-884/com.example.appone D/FragmentManager﹕ com.android.internal.widget.ActionBarView$HomeView{b1e47108 V.E..... ... 0,0-0,0}
11-19 14:19:03.670      884-884/com.example.appone D/FragmentManager﹕ android.widget.ImageView{b1e47518 V.ED.... ... 0,0-0,0 #102025a android:id/up}
11-19 14:19:03.670      884-884/com.example.appone D/FragmentManager﹕ android.widget.ImageView{b1e478f0 V.ED.... ... 0,0-0,0 #102002c android:id/home}
11-19 14:19:03.680      884-884/com.example.appone D/FragmentManager﹕ android.widget.LinearLayout{b1e48cc0 G.E..... ... 0,0-0,0}
11-19 14:19:03.680      884-884/com.example.appone D/FragmentManager﹕ android.widget.TextView{b1e49050 V.ED.... ... 0,0-0,0 #1020264 android:id/action_bar_title}
11-19 14:19:03.680      884-884/com.example.appone D/FragmentManager﹕ android.widget.TextView{b1e49d50 G.ED.... ... 0,0-0,0 #1020265 android:id/action_bar_subtitle}
11-19 14:19:03.690      884-884/com.example.appone D/FragmentManager﹕ com.android.internal.widget.ActionBarContextView{b1e4a3a8 G.E..... ... 0,0-0,0 #1020313 android:id/action_context_bar}
11-19 14:19:03.690      884-884/com.example.appone D/FragmentManager﹕ com.android.internal.widget.ActionBarContainer{b1e4a8f0 G.ED.... ... 0,0-0,0 #1020314 android:id/split_action_bar}
11-19 14:19:03.700      884-884/com.example.appone D/AndroidRuntime﹕ Shutting down VM
11-19 14:19:03.700      884-884/com.example.appone W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1af3b90)
11-19 14:19:03.750      884-884/com.example.appone E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.appone, PID: 884
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appone/com.example.appone.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f07003c (com.example.appone:id/container) for fragment PlaceholderFragment{b1e54ae0 #0 id=0x7f07003c}
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            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:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f07003c (com.example.appone:id/container) for fragment PlaceholderFragment{b1e54ae0 #0 id=0x7f07003c}
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
            at android.app.Activity.performStart(Activity.java:5253)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2149)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.access$700(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4998)
            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:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
            at dalvik.system.NativeStart.main(Native Method)
11-19 14:19:03.790      377-389/system_process W/ActivityManager﹕ Force finishing activity com.example.appone/.DisplayMessageActivity
11-19 14:19:03.850      377-389/system_process W/ActivityManager﹕ Force finishing activity com.example.appone/.MainActivity

第二项活动:

package com.example.appone;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.TextView;

public class DisplayMessageActivity extends ActionBarActivity {

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

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
            return rootView;
        }
    }

}

第二个活动视图:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayMessageActivity"
    tools:ignore="MergeRootFrame" />

第二个活动片段视图:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity$PlaceholderFragment">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

0 个答案:

没有答案