问题描述
- 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程序显示就是一片空白
求前辈多多指教
解决方案
推荐你了解一下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进行赋值,然后进行编译和运行,才会有结果