android hello world app help

时间:2011-08-15 20:03:33

标签: java android

我正在学习Android应用程序,我制作了这个应用程序,它不起作用

  package com.android.test;

  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.Toast;

  public class HelloWorld extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          Button button = (Button) findViewById(R.id.Button01);
          button.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  Toast.makeText(HelloWorld.this, "Hello World", Toast.LENGTH_SHORT).show();
              }
          });
      }
  }

但它不起作用!请告诉我有什么问题并帮我解决!1

2 个答案:

答案 0 :(得分:0)

将吐司线更改为:

Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_SHORT).show();

答案 1 :(得分:0)

编辑java代码:

导航到以下路径:

HelloWorld \ app \ src \ main \ java \ com \ example \ rjchakraborty \ helloworld \ main.java(应用程序名称,用户名会有所不同)

输入下面给出的完整代码,并注意区分大小写。

import android.widget.Toast;

import android.view.View.OnClickListener;

import android.content.Context;

import android.view.View;

EditText name;

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

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Capture our button from layout

Button button = (Button)findViewById(R.id.tap);

// Register the onClickListener with the implementation above

button.setOnClickListener(taplistener);

}

// Create an anonymous implementation of OnClickListener

private OnClickListener taplistener = new OnClickListener() {

public void onClick(View v) {

long id = 0;

// Do something when the button is clicked

try {

name = (EditText)findViewById(R.id.name);

Context context = getApplicationContext();

CharSequence text = “Hello ” + name.getText() +”!”;

int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);

toast.show();

}

catch(Exception e) {

Context context = getApplicationContext();

CharSequence text = e.toString() + “ID = ” + id;

int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);

toast.show();

}

}

};

}

如果你仍然有问题开发你的第一个Android Hello世界应用程序,那么你可以看看这个一步一步的教程,它将指导你整个项目:           Android App Development with Hello World Project