Netbeans Android模拟器没有运行程序

时间:2015-09-23 21:02:13

标签: java android android-activity netbeans emulation

我正在尝试在Netbeans Android IDE中的模拟器上运行一个非常简单的Hello World程序。代码编译,模拟器启动(对于Android 4.0.3),但Hello World应用程序不在手机上。我错过了一些简单或错误的东西吗?代码如下:

package com.test.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class helloworld extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView text1 = new TextView(this);
        text1.setText(“Hello World”);
        setContentView(text1);
    }
}

1 个答案:

答案 0 :(得分:2)

您没有正确获取textview。你需要做这样的事情:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView text1 = (TextView) findViewById(R.id.yourcustomid);
        text1.setText(“Hello World”);
    }

这一行TextView text1 = (TextView) findViewById(R.id.yourcustomid);是如何获取您为活动创建的布局的ID(应该在main.xml中),这可能如下所示:

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/yourcustomid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</RelativeLayout>