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

colie_li 2012-04-22 04:01:09

package com.china.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public
class HelloChina extends Activity {

/**
Called
when
the
activity
is
first
created.
*/

@Override

public
void 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 ;
高手指点下...
...全文
185 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Marco_Lee 2012-04-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

是这段代码出错了:

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

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


TextView tv = new TextView(this);
tv.setT……
[/Quote]

TextView不是继承View?又是java不学好跑来弄android的。。lz还真被你误导了。。。
zgzleo 2012-04-22
  • 打赏
  • 举报
回复
能放父类的地方就能放子类。
LoveAndroid520 2012-04-22
  • 打赏
  • 举报
回复
6楼应该没有问题的吧!在清单文件错了android:name=".HelloWorldActivity",要和你的java 类名一致
AMinfo 2012-04-22
  • 打赏
  • 举报
回复
是这段代码出错了:

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);
ihrthk 2012-04-22
  • 打赏
  • 举报
回复
2楼说的没错,修改过来运行一下,试试。
zgzleo 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
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……
[/Quote]

有错。。你看这一句:
android:name=".HelloWorldActivity"
它说明你要运行的Activity的名字应该是HelloWorldActivity。。你看你的程序里面那个Activity的名字是HelloChina。。程序运行的时候,它根据HelloWorldActivity这个名字来找,当然会找不到,就要报错,应该要报什么classNotFound的异常。
你把名字改一下,随便你改程序里的还是清单文件的,一样就行。
Ps:以后你写程序,每自己写了一个Activity,都需要在清单文件里面注册。
Marco_Lee 2012-04-22
  • 打赏
  • 举报
回复
明显错了,你的activity叫HelloChina,manifest里面写成HelloWorldActivity了
colie_li 2012-04-22
  • 打赏
  • 举报
回复

<?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:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloWorldActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>





没错吧
zgzleo 2012-04-22
  • 打赏
  • 举报
回复
从这看来,程序没什么错。如果报错的话,看下错误日志报的什么错。
我估计你的清单文件有错(AndroidManifest.xml);
activity的中的android:name=""参数 是不是不是HelloChina 。

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧