【问题标题】:what can i do about these errors? [closed]我能做些什么来处理这些错误? [关闭]
【发布时间】:2011-12-05 15:54:59
【问题描述】:
package com.duncan.hello.world;

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

import com.duncan.hello.world.R;

public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Button aButton;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    aButton = (Button) this.findViewById(R.id.button1);

    aButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(HelloWorld.this, OtherActivity.class);
            startActivity(i);
        }});
}
}

我现在只有一个错误,因为我导入了按钮。错误在第 23 行,显示“HelloWorld 无法解析为类型”

【问题讨论】:

  • 你可以导入你使用的类,对于初学者:)
  • -1 表示非建设性问题。
  • 这是我从上一个问题中得到的教程,所以不要对我大喊大叫。
  • 在 eclipse 中做 control+shift+o 来自动导入(可能找不到所有的库,但所有的 android 库都应该没问题)
  • 如果您使用 eclipse,则 eclipse 会建议您修正几乎所有您提到的错误。 13&16:导入Button类,19:导入View,20:导入Intent,将HelloWorld.this改为HelloWorldActivity.this

标签: android eclipse button


【解决方案1】:

您需要导入您正在使用的类。 Button 无法解析为类型,因为您尚未导入它。

它应该在你运行代码之前给你一个错误。

【讨论】:

  • 我什至没有尝试运行代码,因为我无法运行有错误的代码,无论如何你是说添加类似 import android.view.button 的东西?
  • 您是否已将 Android SDK 添加到您的 IDE 中?如果你使用 eclipse Android 这里有插件..developer.android.com/sdk/installing.html
  • 听起来您缺少一项要求。
  • 是的,我正在使用 Eclipse。
  • 是的,我有 Eclipse 的 SDK 插件。
【解决方案2】:

您的活动名称是HelloWorldActivity,但您在 Intent 中使用了HelloWorld。 替换这段代码,

Intent i = new Intent(HelloWorld.this, OtherActivity.class);

用下面的代码替换上面,

Intent i = new Intent(HelloWorldActivity.this, OtherActivity.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2021-08-04
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多