Android的建立第一个应用基础 [英] Android Building Your First App Basics

查看:170
本文介绍了Android的建立第一个应用基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难的时候得到作为第一个应用项目的Andr​​oid开发者网站上的第二个步骤,介绍了应显示的结果:的developer.android.com/training/basics/firstapp/starting-activity.html#receivetheintent

我创建的第一个意图和运行时我收到一个空白屏幕的android没有输入元素项目然而复制的所有其他code。这里的仿真器是什么样子:
http://s1278.beta.photobucket.com/user/cetmrw791346/media/1_zps116f17a9.png.html

我已经设置了运行配置了Nexus类型下与512MB RAM的分配,所以我不完全知道这可能是与有关Java的SDK(7.0)(JDK而不是JRE安装问题),或者它可能是可能的Andr​​oid SDK中。我敢肯定我已经正确设置一切。我使用Eclipse(我pretty确保它是一个IDE),用于移动开发者创建,然后从文件一个新的Andr​​oid应用程序的项目,新项目。下面是我的Package Explorer中的样子: HTTP://s1278.beta .photobucket.com /用户/ cetmrw791346 /媒体/ 2_zps0f2b94a2.png.html

我不确定如何进一步解决这个问题,真的会AP preciate任何额外的帮助。您的帮助再次感谢。

这是相关文件:

  ** **的Andr​​oidManifest.xml
< XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.firstapp
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =17/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=com.example.firstapp.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=com.example.firstapp.DisplayMessageActivity
            机器人:标签=@字符串/ title_activity_display_message
            机器人:parentActivityName =com.example.firstapp.MainActivity>
            &所述;元数据
                机器人:名字=android.support.PARENT_ACTIVITY
                机器人:值=com.example.firstapp.MainActivity/>
        < /活性GT;
    < /用途>< /清单>    ** ** MainActivity.java
    包com.example.firstapp;    进口android.app.Activity;
    进口android.content.Intent;
    进口android.os.Bundle;
    进口android.view.Menu;
    进口android.view.View;
    进口android.widget.EditText;    公共类MainActivity延伸活动{        公共最后静态字符串EXTRA_MESSAGE =com.example.myfirstapp.MESSAGE;        @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);
        }        @覆盖
        公共布尔onCreateOptionsMenu(菜单菜单){
            //充气菜单;如果是present这增加了项目操作栏。
            。getMenuInflater()膨胀(R.menu.main,菜单);
            返回true;
        }
        / **当用户点击发送按钮调用* /
        公共无效的sendMessage(查看视图){
            意向意图=新意图(这一点,DisplayMessageActivity.class);
            EDITTEXT的EditText =(EditText上)findViewById(R.id.edit_message);
            字符串消息= editText.getText()的toString()。
            intent.putExtra(EXTRA_MESSAGE,消息);
            startActivity(意向);
        }
    }    ** ** activity_main.xml中
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=横向
        工具:上下文=MainActivity。>
      < EditText上机器人:ID =@ + ID / edit_message
            机器人:layout_weight =1
            机器人:layout_width =0dp
            机器人:layout_height =WRAP_CONTENT
            机器人:提示=@字符串/ edit_message/>
    <按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=@字符串/ button_send
             安卓的onClick =的sendMessage/>    < / LinearLayout中>
    ** **的strings.xml
    < XML版本=1.0编码=UTF-8&GT?;
    <资源>        <字符串名称=APP_NAME>我的第一个应用程序< /串>
        <字符串名称=edit_message>输入消息< /串>
        <字符串名称=button_send>发送和LT; /串>
        <字符串名称=menu_settings>设置< /串>
        <字符串名称=title_activity_main> MainActivity< /串>
        <字符串名称=title_activity_display_message> DisplayMessageActivity< /串>
        <字符串名称=action_settings>设置< /串>
        <字符串名称=参考hello world>世界,你好<!/串>    < /资源>    activity_display_message.xml
    <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:paddingBottom会=@扪/ activity_vertical_margin
        机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
        机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
        机器人:paddingTop =@扪/ activity_vertical_margin
        工具:上下文=DisplayMessageActivity。>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=@字符串/参考hello world/>    < / RelativeLayout的>
    ** ** DisplayMessageActivity.java
    包com.example.firstapp;    进口android.os.Bundle;
    进口android.app.Activity;
    进口android.view.Menu;
    进口android.view.MenuItem;
    进口android.support.v4.app.NavUtils;
    进口android.annotation.TargetApi;
    进口android.os.Build;    公共类DisplayMessageActivity延伸活动{        @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_display_message);
            //显示操作栏中的向上按钮。
            setupActionBar();
        }        / **
        *设置{@link android.app.ActionBar},如果API是可用的。
        * /
        @TargetApi(Build.VERSION_ codeS.HONEYCOMB)
        私人无效setupActionBar(){
            如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
                getActionBar()setDisplayHomeAsUpEnabled(真)。
            }
        }
        @覆盖
        公共布尔onOptionsItemSelected(菜单项项){
            开关(item.getItemId()){
            案例android.R.id.home:
                //这个ID重新presents主页或向上按钮。在这种情况下
                //活动,向上按钮显示。使用NavUtils允许用户
                //浏览了在应用结构中的一个级别。对于
                //更多详细信息,请参见Android设计的导航模式:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                //
                NavUtils.navigateUpFromSameTask(本);
                返回true;
            }
            返回super.onOptionsItemSelected(项目);
        }    }


解决方案

看来你还没仍会执行您的应用程序(模拟器仍然启动)。

我很新的Java和Android的(只是这几个星期,下面的在线课程),但我发现模拟器很慢而且我真的建议你在真实的设备插入,并用它来运行该应用程序。

当我连接银河S2 Linux和单击运行,Eclipse允许您将其用于执行应用程序。在我以下课程的例子中,应用程序启动,在短短几秒钟,而在模拟器中运行它们是痛苦的。

如果你仍然需要使用仿真器,您可以通过编辑虚拟设备的属性在ADT和标志[X]使用快照切换加快速度。通过激活这个标志,你会不会断电和电源中的虚拟设备每次:当您关闭它,它目前的状态将作为一个快照保存到磁盘,当你再次运行它,你将不再需要等待它启动。快照将被使用,虚拟设备将启动非常快的。

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

    }

解决方案

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.

这篇关于Android的建立第一个应用基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆