没有致命的例外,无法开始活动

时间:2014-03-27 18:53:09

标签: android android-intent

我正准备开发我的第一个应用程序。昨天有一些小小的成功,但是 今天,每当我尝试传递并在新活动中显示意图时,应用程序就会崩溃。 由于我找不到我的错误,我从头开始使用google" myFirstApp"教程, 尽管如此,我发现自己也有同样的崩溃事件 完全遵循说明。 我没有发现我的错误,所以我礼貌地请你帮我找到错误。

MainActivity:

package com.example.myfirstapp;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.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;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    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);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().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;
        }
    }

}

活动我将意图传递给:

package com.example.myfirstapp;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.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;
import android.os.Build;

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);


        if (savedInstanceState == null) {
            getSupportFragmentManager().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.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;
        }
    }

}

logcat的:

03-27 19:32:07.786: D/libEGL(3929): loaded /system/lib/egl/libEGL_mali.so
03-27 19:32:07.796: D/libEGL(3929): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-27 19:32:07.796: D/libEGL(3929): loaded /system/lib/egl/libGLESv2_mali.so
03-27 19:32:07.816: D/OpenGLRenderer(3929): Enabling debug mode 0
03-27 19:32:11.470: E/FragmentManager(3929): No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41c05db0 #0 id=0x7f05003c}
03-27 19:32:11.470: E/FragmentManager(3929): Activity state:
03-27 19:32:11.470: D/FragmentManager(3929):   Local FragmentActivity 41bf9838 State:
03-27 19:32:11.470: D/FragmentManager(3929):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
03-27 19:32:11.470: D/FragmentManager(3929):     mLoadersStarted=false
03-27 19:32:11.470: D/FragmentManager(3929):   Active Fragments in 41bf9a70:
03-27 19:32:11.490: D/FragmentManager(3929):     #0: PlaceholderFragment{41c05db0 #0 id=0x7f05003c}
03-27 19:32:11.490: D/FragmentManager(3929):       mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
03-27 19:32:11.490: D/FragmentManager(3929):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
03-27 19:32:11.490: D/FragmentManager(3929):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
03-27 19:32:11.490: D/FragmentManager(3929):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
03-27 19:32:11.490: D/FragmentManager(3929):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
03-27 19:32:11.490: D/FragmentManager(3929):       mFragmentManager=FragmentManager{41bf9a70 in DisplayMessageActivity{41bf9838}}
03-27 19:32:11.500: D/FragmentManager(3929):       mActivity=com.example.myfirstapp.DisplayMessageActivity@41bf9838
03-27 19:32:11.500: D/FragmentManager(3929):   Added Fragments:
03-27 19:32:11.500: D/FragmentManager(3929):     #0: PlaceholderFragment{41c05db0 #0 id=0x7f05003c}
03-27 19:32:11.500: D/FragmentManager(3929):   FragmentManager misc state:
03-27 19:32:11.500: D/FragmentManager(3929):     mActivity=com.example.myfirstapp.DisplayMessageActivity@41bf9838
03-27 19:32:11.500: D/FragmentManager(3929):     mContainer=android.support.v4.app.FragmentActivity$2@41bf9ae8
03-27 19:32:11.500: D/FragmentManager(3929):     mCurState=2 mStateSaved=false mDestroyed=false
03-27 19:32:11.500: D/FragmentManager(3929):   View Hierarchy:
03-27 19:32:11.500: D/FragmentManager(3929):     com.android.internal.policy.impl.PhoneWindow$DecorView{41bfb3e8 V.E..... ... 0,0-0,0}
03-27 19:32:11.500: D/FragmentManager(3929):       android.widget.LinearLayout{41bfba50 V.E..... ... 0,0-0,0}
03-27 19:32:11.500: D/FragmentManager(3929):         com.android.internal.widget.ActionBarContainer{41bfc5d8 V.ED.... ... 0,0-0,0 #10203f9 android:id/action_bar_container}
03-27 19:32:11.510: D/FragmentManager(3929):           com.android.internal.widget.ActionBarView{41bfcb20 V.E..... ... 0,0-0,0 #10203fa android:id/action_bar}
03-27 19:32:11.510: D/FragmentManager(3929):             android.widget.LinearLayout{41c00de0 G....... ... 0,0-0,0}
03-27 19:32:11.510: D/FragmentManager(3929):               android.widget.ImageView{41c01a48 G.ED.... ... 0,0-0,0 #1020255 android:id/up}
03-27 19:32:11.510: D/FragmentManager(3929):               android.widget.LinearLayout{41c01d48 V.E..... ... 0,0-0,0}
03-27 19:32:11.510: D/FragmentManager(3929):                 android.widget.TextView{41c020b0 V.ED.... ... 0,0-0,0 #1020268 android:id/action_bar_title}
03-27 19:32:11.510: D/FragmentManager(3929):                 android.widget.TextView{41c02e20 G.ED.... ... 0,0-0,0 #1020269 android:id/action_bar_subtitle}
03-27 19:32:11.510: D/FragmentManager(3929):             com.android.internal.widget.ActionBarView$HomeView{41bfdec8 VFE...C. ... 0,0-0,0}
03-27 19:32:11.520: D/FragmentManager(3929):               android.widget.ImageView{41bff3e0 V.ED.... ... 0,0-0,0 #1020255 android:id/up}
03-27 19:32:11.520: D/FragmentManager(3929):               android.widget.ImageView{41bff6e0 V.ED.... ... 0,0-0,0 #102002c android:id/home}
03-27 19:32:11.520: D/FragmentManager(3929):           com.android.internal.widget.ActionBarContextView{41c03438 G.E..... ... 0,0-0,0 #10203fb android:id/action_context_bar}
03-27 19:32:11.520: D/FragmentManager(3929):         android.widget.FrameLayout{41c03950 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
03-27 19:32:11.520: D/FragmentManager(3929):           android.widget.TextView{41c05590 V.ED.... ... 0,0-0,0}
03-27 19:32:11.530: D/dalvikvm(3929): GC_CONCURRENT freed 189K, 10% free 9571K/10567K, paused 13ms+3ms, total 52ms
03-27 19:32:11.530: D/FragmentManager(3929):         com.android.internal.widget.ActionBarContainer{41c03d40 G.ED.... ... 0,0-0,0 #10203fc android:id/split_action_bar}
03-27 19:32:11.530: D/AndroidRuntime(3929): Shutting down VM
03-27 19:32:11.530: W/dalvikvm(3929): threadid=1: thread exiting with uncaught exception (group=0x411d52a0)
03-27 19:32:11.540: E/AndroidRuntime(3929): FATAL EXCEPTION: main
03-27 19:32:11.540: E/AndroidRuntime(3929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41c05db0 #0 id=0x7f05003c}
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.ActivityThread.access$700(ActivityThread.java:134)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.os.Looper.loop(Looper.java:137)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.ActivityThread.main(ActivityThread.java:4867)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at java.lang.reflect.Method.invoke(Method.java:511)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at dalvik.system.NativeStart.main(Native Method)
03-27 19:32:11.540: E/AndroidRuntime(3929): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41c05db0 #0 id=0x7f05003c}
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.Activity.performStart(Activity.java:5057)
03-27 19:32:11.540: E/AndroidRuntime(3929):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
03-27 19:32:11.540: E/AndroidRuntime(3929):     ... 11 more

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:orientation="horizontal" >

    <EditText
        android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />

</LinearLayout>

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

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

</RelativeLayout>

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

问候, 约翰

3 个答案:

答案 0 :(得分:1)

  

引起:java.lang.IllegalArgumentException:找不到id的视图   片段的0x7f05003c(com.example.myfirstapp:id / container)   PlaceholderFragment {41c05db0#0 id = 0x7f05003c}

ViewGroup中没有container代码activity_main.xml

但你有

getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment());

在您的xml中有一个ViewGroup,例如FrameLayout,标识为container

编辑:

public class MainActivity extends Activity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

activity_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:orientation="horizontal" >

    <EditText
        android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />

</LinearLayout>

然后

public class DisplayMessageActivity extends Activity {

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

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    setContentView(textView);
    }

}

无需activity_display_message.xml。删除所有不需要的代码

答案 1 :(得分:0)

当您在活动中充气的布局中不存在要添加或替换片段的视图的ID时,抛出此异常。

所以关于你发布的代码,我认为(我必须看到R.layout.activity_display_message xml才能确定)问题是这个布局上不存在container视图,所以在这一行:< / p>

 getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();

DisplayMessageActivity抛出异常。

答案 2 :(得分:0)

我找到了这个帖子,因为我遇到了同样的问题。在尝试了各种建议后,没有成功,我回到教程并仔细阅读说明。这解决了我的问题。虽然我认为在DisplayMessageActivity#onCreate()中编辑代码的指令可能更清晰。

我删除了一些生成的代码,最后显示了标题为&#34;显示消息&#34;

的部分中显示的内容
@Override
public 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);
}

它有效!