简体   繁体   English

Android应用开发-应用崩溃

[英]Android app development - App crashes

MainActivity.java 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.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";
        @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;
            }
        }
        /** Called when the user clicks the Send button */
        public void sendMessage(View view) {
            // Do something in response to button
            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 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;
        }
    }

}

AndroidManifest.xml AndroidManifest.xml中

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myfirstapp.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="com.example.myfirstapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.myfirstapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>

</manifest>

LOGCAT logcat的

05-04 15:10:46.451: I/dalvikvm(26714): Debugger is active
05-04 15:10:46.561: I/System.out(26714): Debugger has connected
05-04 15:10:46.561: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:46.762: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:46.962: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:47.162: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:47.362: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:47.562: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:47.763: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:47.963: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:48.163: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:48.363: I/System.out(26714): waiting for debugger to settle...
05-04 15:10:48.574: I/System.out(26714): debugger has settled (1482)
05-04 15:10:49.154: I/Adreno-EGL(26714): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I6fa4c648a0ee7dc96f2718bf5d00dc891e0e766bDate: 02/06/14
05-04 15:10:49.194: D/OpenGLRenderer(26714): Enabling debug mode 0
05-04 15:10:49.384: I/Timeline(26714): Timeline: Activity_idle id: android.os.BinderProxy@41a3e538 time:30429837
05-04 15:11:00.847: I/Timeline(26714): Timeline: Activity_launch_request id:com.example.myfirstapp time:30441292
05-04 15:11:00.857: I/Choreographer(26714): Skipped 31 frames!  The application may be doing too much work on its main thread.
05-04 15:11:01.017: E/FragmentManager(26714): No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41b02f98 #0 id=0x7f05003c}
05-04 15:11:01.017: E/FragmentManager(26714): Activity state:
05-04 15:11:01.017: D/FragmentManager(26714):   Local FragmentActivity 41af9790 State:
05-04 15:11:01.017: D/FragmentManager(26714):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
05-04 15:11:01.017: D/FragmentManager(26714):     mLoadersStarted=false
05-04 15:11:01.027: D/FragmentManager(26714):   Active Fragments in 41af9b30:
05-04 15:11:01.027: D/FragmentManager(26714):     #0: PlaceholderFragment{41b02f98 #0 id=0x7f05003c}
05-04 15:11:01.027: D/FragmentManager(26714):       mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
05-04 15:11:01.027: D/FragmentManager(26714):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
05-04 15:11:01.027: D/FragmentManager(26714):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
05-04 15:11:01.037: D/FragmentManager(26714):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
05-04 15:11:01.037: D/FragmentManager(26714):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
05-04 15:11:01.037: D/FragmentManager(26714):       mFragmentManager=FragmentManager{41af9b30 in DisplayMessageActivity{41af9790}}
05-04 15:11:01.037: D/FragmentManager(26714):       mActivity=com.example.myfirstapp.DisplayMessageActivity@41af9790
05-04 15:11:01.037: D/FragmentManager(26714):   Added Fragments:
05-04 15:11:01.037: D/FragmentManager(26714):     #0: PlaceholderFragment{41b02f98 #0 id=0x7f05003c}
05-04 15:11:01.037: D/FragmentManager(26714):   FragmentManager misc state:
05-04 15:11:01.047: D/FragmentManager(26714):     mActivity=com.example.myfirstapp.DisplayMessageActivity@41af9790
05-04 15:11:01.047: D/FragmentManager(26714):     mContainer=android.support.v4.app.FragmentActivity$2@41af9ba8
05-04 15:11:01.047: D/FragmentManager(26714):     mCurState=2 mStateSaved=false mDestroyed=false
05-04 15:11:01.047: D/FragmentManager(26714):   View Hierarchy:
05-04 15:11:01.047: D/FragmentManager(26714):     com.android.internal.policy.impl.PhoneWindow$DecorView{41afb608 V.E..... ... 0,0-0,0}
05-04 15:11:01.057: D/FragmentManager(26714):       com.android.internal.widget.ActionBarOverlayLayout{41afbc48 V.ED.... ... 0,0-0,0 #1020322 android:id/action_bar_overlay_layout}
05-04 15:11:01.057: D/FragmentManager(26714):         android.widget.FrameLayout{41afc858 V.E..... ... 0,0-0,0 #1020002 android:id/content}
05-04 15:11:01.057: D/FragmentManager(26714):           android.widget.TextView{41b03248 V.ED.... ... 0,0-0,0}
05-04 15:11:01.067: D/FragmentManager(26714):         com.android.internal.widget.ActionBarContainer{41afcc58 V.ED.... ... 0,0-0,0 #1020323 android:id/action_bar_container}
05-04 15:11:01.067: D/FragmentManager(26714):           com.android.internal.widget.ActionBarView{41afd160 V.E..... ... 0,0-0,0 #1020324 android:id/action_bar}
05-04 15:11:01.067: D/FragmentManager(26714):             android.widget.LinearLayout{41afd6d0 VFE...C. ... 0,0-0,0}
05-04 15:11:01.077: D/FragmentManager(26714):               com.android.internal.widget.ActionBarView$HomeView{41afe7b0 V.E..... ... 0,0-0,0}
05-04 15:11:01.077: D/FragmentManager(26714):                 android.widget.ImageView{41afeb48 V.ED.... ... 0,0-0,0 #102025e android:id/up}
05-04 15:11:01.077: D/FragmentManager(26714):                 android.widget.ImageView{41afeea8 V.ED.... ... 0,0-0,0 #102002c android:id/home}
05-04 15:11:01.087: D/FragmentManager(26714):               android.widget.LinearLayout{41b000a0 G.E..... ... 0,0-0,0}
05-04 15:11:01.087: D/FragmentManager(26714):                 android.widget.TextView{41b003b8 V.ED.... ... 0,0-0,0 #1020269 android:id/action_bar_title}
05-04 15:11:01.087: D/FragmentManager(26714):                 android.widget.TextView{41b01040 G.ED.... ... 0,0-0,0 #102026a android:id/action_bar_subtitle}
05-04 15:11:01.097: D/FragmentManager(26714):           com.android.internal.widget.ActionBarContextView{41b01660 G.E..... ... 0,0-0,0 #1020325 android:id/action_context_bar}
05-04 15:11:01.097: D/FragmentManager(26714):         com.android.internal.widget.ActionBarContainer{41b01b30 G.ED.... ... 0,0-0,0 #1020326 android:id/split_action_bar}

I have been testing it on a Nexus 4. The program is suppose to display whatever is enetered in the text box when the "Send" button is pressed. 我已经在Nexus 4上进行过测试。该程序假定在按下“发送”按钮时显示文本框中显示的所有内容。 Instead it just crashes saying "unfortunately, My First App has stopped". 相反,它只是崩溃地提示“不幸的是,我的第一个应用程序已停止”。

Here is the Problem as the LogCat says: 正如LogCat所说的,这是问题所在:

No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41b02f98 #0 id=0x7f05003c}

there is no view(Null) that returned from onCreateView function of your Placeholder Fragment: 没有从占位符片段的onCreateView函数返回的view(Null):

View rootView = inflater.inflate(R.layout.fragment_display_message,
                container, false);

you could add a framelayout with an Id inside your layout and use it as placeholder for PlaceHolderFragment class. 您可以在布局内添加带有Id的framelayout,并将其用作PlaceHolderFragment类的占位符。 then return that inflated view in onCreateView of PlaceHolderFragment class 然后在PlaceHolderFragment类的onCreateView中返回该放大视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM