2010-12-03 114 views
0

我已成功将Eclipse和Android SDK安装到我的Mac。但是,当我使用下面的代码运行该程序。它总是给我错误。 “对不起!该应用程序你好,哈里斯(过程com.example.helloandroid)意外停止,请。再试我的Android Hello World应用程序不会执行或显示?

//package com.example.helloandroid; 

import com.example.helloandroid.R; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 


public class HelloAndroid extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView tv = new TextView(this); 
     tv.setText("Hello, Harris Family."); 
     setContentView(tv); 
    } 
} 
+0

在eclipse中打开DDMS透视图并查找标题为“LogCat”的窗格。这将包含所谓的“堆栈跟踪”。请找到并发布它,我们可以看到实际发生的情况。有关堆栈跟踪的简要示例,请参阅http://www.javaworld.com/javaworld/javatips/jw-javatip124.html。 – 2010-12-03 16:49:38

+0

看起来你正在尝试遵循开发者网站上的[“Hello,World”](http://developer.android.com/resources/tutorials/hello-world.html)例子,但你已经有了一些异常的变化。你有什么理由为你评论第一行?您为特定目的添加了“import com.example.helloandroid.R”行吗? – eldarerathis 2010-12-03 16:59:14

回答

1

我认为这个问题是你直接设置的TextView的内容查看。您应该更好地利用布局来代替。

0

这是因为你没有设置一个布局来看,你只能有一个不带父一个TextView。

先设置好p的arent布局为您的文本视图像这样的东西:

this.setContentView(sv); 

添加内容:

TextView tv = new TextView(this); 
tv.setText("Greetings"); 
tv.setGravity(Gravity.CENTER_HORIZONTAL); 
tv.setTextSize(18); 
ll.addView(tv); 

现在与添加视图布局:

ScrollView sv = new ScrollView(ViewPlay.this); 
     LinearLayout ll = new LinearLayout(ViewPlay.this); 
     ll.setOrientation(LinearLayout.VERTICAL); 

然后添加你的文字图动态显示到上面的屏幕可能非常棘手,所以尽可能使用xml。

相关问题