我的第一个应用程序教程"不幸的是我的第一个应用已停止。"

时间:2014-10-09 17:15:51

标签: java android eclipse

我目前正在浏览Android Developer教程,并且是最基本的教程,它将意图从一个活动传递到另一个活动并显示结果。

Eclipse显示应用程序没有错误,一切正常(不起作用),直到我插入这些代码行:

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

我正在查看LogCat并设法解决了一个错误,我只是错误地命名了一些错误,但现在又被我难倒了。这是我当前的LogCat输出:

10-09 18:09:03.882: D/Send Message Button(9652): Pressed
10-09 18:09:03.917: E/FragmentManager(9652): No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.917: E/FragmentManager(9652): Activity state:
10-09 18:09:03.917: D/FragmentManager(9652):   Local FragmentActivity 422d1598 State:
10-09 18:09:03.922: D/FragmentManager(9652):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
10-09 18:09:03.922: D/FragmentManager(9652):     mLoadersStarted=false
10-09 18:09:03.922: D/FragmentManager(9652):   Active Fragments in 422d1808:
10-09 18:09:03.922: D/FragmentManager(9652):     #0: PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.922: D/FragmentManager(9652):       mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
10-09 18:09:03.922: D/FragmentManager(9652):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
10-09 18:09:03.927: D/FragmentManager(9652):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-09 18:09:03.927: D/FragmentManager(9652):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-09 18:09:03.927: D/FragmentManager(9652):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-09 18:09:03.927: D/FragmentManager(9652):       mFragmentManager=FragmentManager{422d1808 in DisplayMessageActivity{422d1598}}
10-09 18:09:03.927: D/FragmentManager(9652):       mActivity=com.example.myfirstapp.DisplayMessageActivity@422d1598
10-09 18:09:03.927: D/FragmentManager(9652):   Added Fragments:
10-09 18:09:03.927: D/FragmentManager(9652):     #0: PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.927: D/FragmentManager(9652):   FragmentManager misc state:
10-09 18:09:03.927: D/FragmentManager(9652):     mActivity=com.example.myfirstapp.DisplayMessageActivity@422d1598
10-09 18:09:03.927: D/FragmentManager(9652):     mContainer=android.support.v4.app.FragmentActivity$2@422d1880
10-09 18:09:03.927: D/FragmentManager(9652):     mCurState=2 mStateSaved=false mDestroyed=false
10-09 18:09:03.927: D/FragmentManager(9652):   View Hierarchy:
10-09 18:09:03.927: D/FragmentManager(9652):     com.android.internal.policy.impl.PhoneWindow$DecorView{422d31a8 V.E..... ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652):       com.android.internal.widget.ActionBarOverlayLayout{422d37e8 V.ED.... ... 0,0-0,0 #1020313 android:id/action_bar_overlay_layout}
10-09 18:09:03.932: D/FragmentManager(9652):         android.widget.FrameLayout{422d43f8 V.E..... ... 0,0-0,0 #1020002 android:id/content}
10-09 18:09:03.932: D/FragmentManager(9652):           android.widget.TextView{422e3b30 V.ED.... ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652):         com.android.internal.widget.ActionBarContainer{422d47f8 V.ED.... ... 0,0-0,0 #1020314 android:id/action_bar_container}
10-09 18:09:03.932: D/FragmentManager(9652):           com.android.internal.widget.ActionBarView{422d4d00 V.E..... ... 0,0-0,0 #1020315 android:id/action_bar}
10-09 18:09:03.932: D/FragmentManager(9652):             android.widget.LinearLayout{422d5270 VFE...C. ... 0,0-0,0}
10-09 18:09:03.932: D/FragmentManager(9652):               com.android.internal.widget.ActionBarView$HomeView{422d6350 V.E..... ... 0,0-0,0}
10-09 18:09:03.937: D/FragmentManager(9652):                 android.widget.ImageView{422d66e8 V.ED.... ... 0,0-0,0 #102025a android:id/up}
10-09 18:09:03.937: D/FragmentManager(9652):                 android.widget.ImageView{422d6a48 V.ED.... ... 0,0-0,0 #102002c android:id/home}
10-09 18:09:03.937: D/FragmentManager(9652):               android.widget.LinearLayout{422d7c40 G.E..... ... 0,0-0,0}
10-09 18:09:03.937: D/FragmentManager(9652):                 android.widget.TextView{422d7f58 V.ED.... ... 0,0-0,0 #1020265 android:id/action_bar_title}
10-09 18:09:03.937: D/FragmentManager(9652):                 android.widget.TextView{422d8be0 G.ED.... ... 0,0-0,0 #1020266 android:id/action_bar_subtitle}
10-09 18:09:03.937: D/FragmentManager(9652):           com.android.internal.widget.ActionBarContextView{422d9200 G.E..... ... 0,0-0,0 #1020316 android:id/action_context_bar}
10-09 18:09:03.937: D/AndroidRuntime(9652): Shutting down VM
10-09 18:09:03.937: W/dalvikvm(9652): threadid=1: thread exiting with uncaught exception (group=0x41f55ba8)
10-09 18:09:03.942: E/AndroidRuntime(9652): FATAL EXCEPTION: main
10-09 18:09:03.942: E/AndroidRuntime(9652): Process: com.example.myfirstapp, PID: 9652
10-09 18:09:03.942: E/AndroidRuntime(9652): 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{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.os.Looper.loop(Looper.java:136)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at java.lang.reflect.Method.invoke(Method.java:515)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at dalvik.system.NativeStart.main(Native Method)
10-09 18:09:03.942: E/AndroidRuntime(9652): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{422e3888 #0 id=0x7f05003c}
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.Activity.performStart(Activity.java:5241)
10-09 18:09:03.942: E/AndroidRuntime(9652):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
10-09 18:09:03.942: E/AndroidRuntime(9652):     ... 11 more

MainActivity.java

package com.example.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
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 ActionBarActivity {

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

    public void sendMessage(View view) {

        Log.d("Send Message Button", "Pressed");

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

DisplayMessageActivity.java

package com.example.myfirstapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayMessageActivity extends ActionBarActivity {

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

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

    }

    @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:

10-09 18:24:43.317: W/dalvikvm(9771): threadid=1: thread exiting with uncaught exception (group=0x41f55ba8)
10-09 18:24:43.322: E/AndroidRuntime(9771): FATAL EXCEPTION: main
10-09 18:24:43.322: E/AndroidRuntime(9771): Process: com.example.myfirstapp, PID: 9771
10-09 18:24:43.322: E/AndroidRuntime(9771): 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{4228bce8 #0 id=0x7f05003c}
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.os.Looper.loop(Looper.java:136)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at java.lang.reflect.Method.invoke(Method.java:515)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at dalvik.system.NativeStart.main(Native Method)
10-09 18:24:43.322: E/AndroidRuntime(9771): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4228bce8 #0 id=0x7f05003c}
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.Activity.performStart(Activity.java:5241)
10-09 18:24:43.322: E/AndroidRuntime(9771):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
10-09 18:24:43.322: E/AndroidRuntime(9771):     ... 11 more

3 个答案:

答案 0 :(得分:2)

您已将视图R.layout.activity_display_message替换为文本视图。布局R.layout.activity_display_message包含将片段放入其中的容器。

IllegalArgument在您的setContentView中传递,在您的情况下是textView,因此您的mainActivity中的容器ID丢失,您的片段仍然附加到您的活动,这就是发生异常的原因。

答案 1 :(得分:1)

虽然你没有提供xml。检查您的xml文件并查找容器因为它显示找不到容器的视图。尝试使用框架布局

添加或替换它
10-09 18:09:03.942: E/AndroidRuntime(9652): 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{422e3888 #0 id=0x7f05003c}

答案 2 :(得分:0)

在setContentView中,您必须设置在/ res / layout文件夹中定义的布局。在那里你可以创建你的textView ... 例如setContentView(R.layout.myFirstLayout);