第一个正式的Android教程崩溃了

时间:2014-08-26 15:28:53

标签: java android xml eclipse

我按照官方Android教程,从第一页到starting acivity page

结束

当我尝试在文本框中写一些内容时,Android应用程序崩溃了。 下面是logcat

08-26 17:02:22.560: I/Process(22730): Sending signal. PID: 22730 SIG: 9
08-26 17:02:22.740: D/ActivityThread(22775): handleBindApplication:com.example.andtest
08-26 17:02:22.740: D/ActivityThread(22775): setTargetHeapUtilization:0.25
08-26 17:02:22.740: D/ActivityThread(22775): setTargetHeapMinFree:524288
08-26 17:02:22.981: I/Adreno200-EGL(22775): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051_msm8960_JB_VANILLA_CL2997615_release_AU (CL2997615)
08-26 17:02:22.981: I/Adreno200-EGL(22775): Build Date: 04/11/13 Thu
08-26 17:02:22.981: I/Adreno200-EGL(22775): Local Branch: 
08-26 17:02:22.981: I/Adreno200-EGL(22775): Remote Branch: quic/mako_jb_mr1
08-26 17:02:22.981: I/Adreno200-EGL(22775): Local Patches: NONE
08-26 17:02:22.981: I/Adreno200-EGL(22775): Reconstruct Branch: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051 +  NOTHING
08-26 17:02:23.011: D/OpenGLRenderer(22775): Enabling debug mode 0
08-26 17:02:23.331: I/Timeline(22775): Timeline: Activity_idle id: android.os.BinderProxy@41a0a008 time:120759433
08-26 17:02:25.063: I/Timeline(22775): Timeline: Activity_launch_request id:com.example.andtest time:120761161
08-26 17:02:25.143: E/FragmentManager(22775): No view found for id 0x7f080000 (com.example.andtest:id/container) for fragment PlaceholderFragment{41a2e990 #0 id=0x7f080000}
08-26 17:02:25.143: E/FragmentManager(22775): Activity state:
08-26 17:02:25.153: E/FragmentManager(22775):   Local Activity 41a03f88 State:
08-26 17:02:25.163: E/FragmentManager(22775):     mResumed=false mStopped=false mFinished=false
08-26 17:02:25.163: E/FragmentManager(22775):     mLoadersStarted=false
08-26 17:02:25.163: E/FragmentManager(22775):     mChangingConfigurations=false
08-26 17:02:25.163: E/FragmentManager(22775):     mCurrentConfig={1.0 222mcc88mnc it_IT ldltr sw320dp w320dp h508dp 240dpi nrml long port finger -keyb/v/h -nav/h s.21 themeResource=null}
08-26 17:02:25.163: E/FragmentManager(22775):   Active Fragments in 41a04080:
08-26 17:02:25.163: E/FragmentManager(22775):     #0: PlaceholderFragment{41a2e990 #0 id=0x7f080000}
08-26 17:02:25.163: E/FragmentManager(22775):       mFragmentId=#7f080000 mContainerId=#7f080000 mTag=null
08-26 17:02:25.163: E/FragmentManager(22775):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
08-26 17:02:25.163: E/FragmentManager(22775):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
08-26 17:02:25.163: E/FragmentManager(22775):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
08-26 17:02:25.163: E/FragmentManager(22775):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
08-26 17:02:25.163: E/FragmentManager(22775):       mFragmentManager=FragmentManager{41a04080 in DisplayMessageActivity{41a03f88}}
08-26 17:02:25.163: E/FragmentManager(22775):       mActivity=com.example.andtest.DisplayMessageActivity@41a03f88
08-26 17:02:25.163: E/FragmentManager(22775):   Added Fragments:
08-26 17:02:25.163: E/FragmentManager(22775):     #0: PlaceholderFragment{41a2e990 #0 id=0x7f080000}
08-26 17:02:25.163: E/FragmentManager(22775):   FragmentManager misc state:
08-26 17:02:25.163: E/FragmentManager(22775):     mActivity=
08-26 17:02:25.173: E/FragmentManager(22775): com.example.andtest.DisplayMessageActivity@41a03f88
08-26 17:02:25.173: E/FragmentManager(22775):     mContainer=android.app.Activity$1@41a0bb90
08-26 17:02:25.173: E/FragmentManager(22775):     mCurState=2 mStateSaved=false mDestroyed=false
08-26 17:02:25.173: E/FragmentManager(22775):   Looper (main, tid 1) {41a088c0}
08-26 17:02:25.173: E/FragmentManager(22775):     (Total messages: 0, idling=false, quitting=false)
08-26 17:02:25.173: D/AndroidRuntime(22775): Shutting down VM
08-26 17:02:25.173: W/dalvikvm(22775): threadid=1: thread exiting with uncaught exception (group=0x41746ce0)
08-26 17:02:25.183: E/AndroidRuntime(22775): FATAL EXCEPTION: main
08-26 17:02:25.183: E/AndroidRuntime(22775): Process: com.example.andtest, PID: 22775
08-26 17:02:25.183: E/AndroidRuntime(22775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andtest/com.example.andtest.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080000 (com.example.andtest:id/container) for fragment PlaceholderFragment{41a2e990 #0 id=0x7f080000}
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.os.Looper.loop(Looper.java:136)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.ActivityThread.main(ActivityThread.java:5137)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at java.lang.reflect.Method.invoke(Method.java:515)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at dalvik.system.NativeStart.main(Native Method)
08-26 17:02:25.183: E/AndroidRuntime(22775): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080000 (com.example.andtest:id/container) for fragment PlaceholderFragment{41a2e990 #0 id=0x7f080000}
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.Activity.performStart(Activity.java:5240)
08-26 17:02:25.183: E/AndroidRuntime(22775):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
08-26 17:02:25.183: E/AndroidRuntime(22775):    ... 11 more
08-26 17:02:26.834: I/Process(22775): Sending signal. PID: 22775 SIG: 9

MainActivity.java

package com.example.andtest;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
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.widget.EditText;

public class MainActivity extends Activity
{
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null)
        {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @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();
        if (id == R.id.action_settings)
        {
            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_main, container,
                    false);
            return rootView;
        }
    }

    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);

    }
}

DisplayMessageActivity.java

package com.example.andtest;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
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.widget.TextView;

public class DisplayMessageActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        if (savedInstanceState == null)
        {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        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);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_message, menu);
        return true;
    }

    @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();
        if (id == R.id.action_settings)
        {
            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;
        }
    }
}

activity_display_message.xml

<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="com.example.andtest.DisplayMessageActivity"
    tools:ignore="MergeRootFrame" />

activity_main.xml中

<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="com.example.andtest.MainActivity"
    tools:ignore="MergeRootFrame" />

fragment_display_message.xml

<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: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.andtest.DisplayMessageActivity$PlaceholderFragment" >

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

</RelativeLayout>

fragment_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context="com.example.andtest.MainActivity$PlaceholderFragment" >

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message"
        android:onClick="sendMessage" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.andtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.andtest.MainActivity" />
        </activity>
    </application>

</manifest>

的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_display_message">My Message</string>

</resources>

系统详情

  • Fedora 20;
  • Android 4.4.4;
  • Eclipse 4.4;
  • 亚行1.0.31。

3 个答案:

答案 0 :(得分:2)

首先我发布了一个错误的解决方案。对不起。这是出了什么问题:

在你的DisplayMessageActivity中,你要两次致电setContentView(...)。使用activity_display_message布局后,第二次使用 contaider-id 设置textView替换 activity_display_message-layout 。所以删除PlaceholderFragment的东西,一切都很好(它也不是教程的一部分)。

进一步说明

FragmentManager尝试在容器中添加PlaceholderFragment时,它意识到布局中没有ID为container的视图。那是因为布局(activity_display_message)已被TextView取代 (好像Fragment事务确实运行异步)

答案 1 :(得分:1)

activity_display_messageactivity_main布局都具有相同的ID:@+id/container,因此Android不会选择哪一个并抛出错误。

答案 2 :(得分:0)

//将文本视图设置为活动布局

setContentView(textView); // this is mistake, dont call setContentView twice, this is sin.