开发者

My Android Hello World Application will not execute or display?

开发者 https://www.devze.com 2023-01-28 13:57 出处:网络
I have successfully installed the Eclipse, and Android SDK to my Mac.However when I run the program using the code below.It always gives me the error.\"Sorry! The application Hello, Harris(process com

I have successfully installed the Eclipse, and Android SDK to my Mac. However when I run the program using the code below. It always gives me the error. "Sorry! The application Hello, Harris(process com.example.helloandroid) has stopped unexpectedly. Please try again.

//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 activit开发者_如何学Goy is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("Hello, Harris Family.");
        setContentView(tv);
    }
}


I think the problem is that you directly set the TextView as the ContentView. You should better use a layout instead.


Is your activity inserted in the application manifest? http://developer.android.com/guide/topics/manifest/activity-element.html


This is because you have not set a layout view, you only have a textview with no parent.

First set up the parent layout for your text view with something like this:

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

Then add your text view with :

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

Now add the view to the layout with:

this.setContentView(sv);

Adding content to the screen dynamically as above can be very thorny, so where possible use xml.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号