首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的第一个Android应用程序的输出没有显示

我的第一个Android应用程序的输出没有显示
EN

Stack Overflow用户
提问于 2012-08-31 07:32:23
回答 3查看 2.8K关注 0票数 0

我是Android的初学者,正在尝试运行Hello,但是当模拟器出现时,只显示Android图像。我的输出“你好世界”从未出现过。

MainActivity.class

代码语言:javascript
复制
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView text = new TextView(this);
        text.setText("Hello World, Android");
        setContentView(text);
       // setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

当模拟器弹出时,我可以等待5-10分钟,但是输出却没有显示出来。我没有在任何XML文件中更改任何内容。我在试着测试“你好世界”程序。

我在使用Eclipse。

编辑

activity_main.xml

代码语言:javascript
复制
<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

androidManifest.XML

代码语言:javascript
复制
<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="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-31 07:35:06

您是否将Activity添加到AndroidManifest文件中?

示例

代码语言:javascript
复制
<application android:label="@string/app_name"
    android:icon="@drawable/ic_launcher">
    <activity android:name=".YourActivity">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

更新

变化

代码语言:javascript
复制
setContentView(text)

代码语言:javascript
复制
setContentView(R.layout.activity_main) 

这意味着您将使用预定义的XML文件作为此Activity的布局。

如果您想要更改xml中的TextView:

代码语言:javascript
复制
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find the TextView in the XML file defined by setContentView
    TextView text = (TextView) findViewById(R.id.mTextView);
    text.setText("Your text");
}

不要忘记将`android:id="@+id/mTextView“属性添加到XML中的TextView中。

票数 3
EN

Stack Overflow用户

发布于 2012-08-31 07:38:26

您必须编辑manifest.xml:

代码语言:javascript
复制
<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
票数 0
EN

Stack Overflow用户

发布于 2012-08-31 07:54:09

我建议您“改进”您的MainActivity:

代码语言:javascript
复制
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  //The XML where the text view is
        TextView text = (TextView) findViewById(R.id.TEXTVIEWID); // The id of the text view
        text.setText("Hello World, Android");

    }
}

那么xml应该是:

代码语言:javascript
复制
<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" >

    <TextView
        android:id="@+id/TEXTVIEWID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

对于基础知识,您只需遵循教程即可!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12210797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档