当前位置: 代码迷 >> Android >> 第一个Android 程序无法运行,希望高手拯救.该怎么解决
  详细解决方案

第一个Android 程序无法运行,希望高手拯救.该怎么解决

热度:19   发布时间:2016-05-01 21:28:15.0
第一个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 类名一致

------解决方案--------------------
能放父类的地方就能放子类。
  相关解决方案