读书人

第一个Android 程序无法运行希望高手

发布时间: 2012-04-24 14:15:38 作者: rapoo

第一个Android 程序无法运行,希望高手拯救...

Java code
package com.china.hello;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;publicclass HelloChina extends Activity {/**Calledwhentheactivityisfirstcreated.*/@Overridepublicvoid onCreate(Bundle saveInstanceState) {super.onCreate(saveInstanceState);//setContentView(R.layout.main);TextView tv = new TextView(this);tv.setText("helloWorld");setContentView(tv);}}

下载的是android2.2;
运行后出现一个手机界面不过说:The application has stopped unexpectedly, please try again ;
高手指点下...


[解决办法]
从这看来,程序没什么错。如果报错的话,看下错误日志报的什么错。
我估计你的清单文件有错(AndroidManifest.xml);
activity的中的android:name=""参数 是不是不是HelloChina 。
[解决办法]
明显错了,你的activity叫HelloChina,manifest里面写成HelloWorldActivity了
[解决办法]
探讨
Java code


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.china.hello"
android:versionCode="1"
android:ver……

[解决办法]
2楼说的没错,修改过来运行一下,试试。
[解决办法]
是这段代码出错了:

TextView tv = new TextView(this);
tv.setText("helloWorld");
setContentView(tv);

setContentView的参数只能是View类型,而tv是View下的控件,不能直接用,需要修改一下:


TextView tv = new TextView(this);
tv.setText("helloWorld");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

LinearLayout linearlayout = new LinearLayout(this)
linearlayout.addView(tv);
setContentView(linearlayout);
[解决办法]
6楼应该没有问题的吧!在清单文件错了android:name=".HelloWorldActivity",要和你的java 类名一致

[解决办法]
能放父类的地方就能放子类。

读书人网 >Android

热点推荐