Android Building Your First App Basics

2019-08-28 01:37发布

问题:

I'm having a difficult time getting the results that should be displayed as described in the second step of the First App project on the android developer website: developer.android.com/training/basics/firstapp/starting-activity.html#receivetheintent

I've created the first intent and copied all other code however upon running the project I receive a blank android screen with no input elements. Here's what the emulator looks like: http://s1278.beta.photobucket.com/user/cetmrw791346/media/1_zps116f17a9.png.html

I've set the Run Configuration under the Nexus type with an allocation of 512MB RAM so I'm not exactly sure if this might have something to do with an installation problem regarding the Java SDK (7.0) (JDK not the JRE) or if it could possible be the Android SDK. I'm fairly certain I've set everything up correctly. I'm using The Eclipse (I'm pretty sure it's an IDE) for Mobile Developers then creating a new Android App project from File, New Project. Here's what my Package Explorer looks like: http://s1278.beta.photobucket.com/user/cetmrw791346/media/2_zps0f2b94a2.png.html

I'm unsure as how to further troubleshoot the problem and would really appreciate any additional help. Thanks again for the help.

And here are the relevant files:

**AndroidManifest.xml**


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

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

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

</manifest>

    **MainActivity.java**


    package com.example.firstapp;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    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);
        }

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


        /** Called when the user clicks the Send button */
        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);
        }
    }

    **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"
        tools:context=".MainActivity" >
      <EditText android:id="@+id/edit_message"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/edit_message" />
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send"
             android:onClick="sendMessage" />

    </LinearLayout>


    **strings.xml**


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

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

    </resources>

    activity_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=".DisplayMessageActivity" >

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

    </RelativeLayout>


    **DisplayMessageActivity.java**


    package com.example.firstapp;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.support.v4.app.NavUtils;
    import android.annotation.TargetApi;
    import android.os.Build;

    public class DisplayMessageActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_display_message);
            // Show the Up button in the action bar.
            setupActionBar();
        }

        /**
        * Set up the {@link android.app.ActionBar}, if the API is available.
        */
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        private void setupActionBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                //
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

    }

回答1:

It doesn't look like your emulator has started up yet. Wait for it to boot to the homescreen, and then your app should run.



回答2:

A couple of points:

  1. It seems you aren't letting your app actually start. The first screen you posted is just the "boot" screen of your emulator
  2. Have you tried switching to the debug perspective in Eclipse? At the bottom you'll see what Eclipse is actually doing. You have to switch to the console view and/or view the logcat to see a bit more detail, but that should actually help you in your efforts.
  3. If you have trouble starting up your Emulator, you can test it by itself. You have (for instance) the option to select the second of the two Android icons that are in the upper bar in Eclipse. It should be the one that says "Android Virtual Device Manager". When you select it, it shows you your configured Emulators, though you can configure new ones as well. You can start one of those in advance and see how they work.


回答3:

It seems that you have not still executed your app (the emulator is still booting).

I'm quite new to both Java and Android (just a few weeks on it, following an online course) but I found the emulator really slow and I'd really advice you to plug in a real device and use it for running the app.

When connecting my Galaxy S2 to Linux and clicking RUN, Eclipse allows you to use it for execute the app. In the examples of the course I'm following, the apps starts in just a couple of seconds, while running them in the emulator is painful.

If you still need to use the emulator, you can speed it up by editing the properties of your virtual device in ADT and switching on the flag "[X] Use snapshot". By activating this flag, you won't "power off" and "power on" the "virtual device" each time: when you close it, its current state will be saved to disk as an snapshot and when you run it again, you won't need to wait for it to boot. The snapshot will be used and the virtual device will startup very fast.



回答4:

Got some similar problem with real device. After have been working well on helloworld, keep on displaying HelloWorld after some changes in the code(building the UI). That is the stack i've no idea to resolve...