2 u012236302 u012236302 于 2014.03.18 18:10 提问

eclipse安卓helloworld程序显示问题

初次用安卓AVD
但是结果什么都没有显示

代码:
package com.example.third;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity{
public void oncreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello world");
setContentView(tv);
}
}

结果:
[2014-03-18 17:52:22 - Third] ------------------------------
[2014-03-18 17:52:22 - Third] Android Launch!
[2014-03-18 17:52:22 - Third] adb is running normally.
[2014-03-18 17:52:22 - Third] Performing com.example.third.MainActivity activity launch
[2014-03-18 17:52:22 - Third] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'AVD1.1'
[2014-03-18 17:52:22 - Third] Uploading Third.apk onto device 'emulator-5554'
[2014-03-18 17:52:24 - Third] Installing Third.apk...
[2014-03-18 17:52:55 - Third] Success!
[2014-03-18 17:52:55 - Third] Starting activity com.example.third.MainActivity on device emulator-5554

程序显示就是一片空白
求前辈多多指教

1个回答

slamzwl
slamzwl   2014.03.18 20:52

推荐你了解一下android的目录结构,在这里,setContentView()括号里面的应该对应的是一个*.xml的文件,就是android的界面布局文件,所以我们在setContentView()中会填写R.layout.activity_main之类的界面布局文件,所以,你以上的代码在setContentView中出现了错误,自然不会出现helloworld,所以正确的代码是setContentView(R.layout.activity_main);其次,申明一个TextView,但是这个TextView必须在activity_main.xml文件中存在,所以在TextView对象赋值时应该是TextView tv = this.(activity_main.xml文件中对应的TextView的id),然后对TextView.Text进行赋值,然后进行编译和运行,才会有结果

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!