【问题标题】:Button cannot be resolved to a type - Android developmentButton 无法解析为类型 - Android 开发
【发布时间】:2014-02-26 12:51:36
【问题描述】:

我是 android 开发新手,遇到了一些调试问题。我正在从一个名为“TheNewBoston”的 youtube 频道学习并在第 10 课中遇到问题。我没有使用与教程中的人相同的 src 包,因为我找不到它所以我想知道这是否造成了问题。

添加和子按钮在 OnCreate 方法中都有相同的“此行有多个标记 - 按钮无法按类型解析”错误。

Java:

package com.example.thenewboston.sam;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.thenewboston.sam.util.SystemUiHider;
public class FullscreenActivity extends Activity {
/**
 * Whether or not the system UI should be auto-hidden after
 * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
 */

int counter;
Button add, sub;
TextView Display;
private static final boolean AUTO_HIDE = true;
/**
 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
 * user interaction before hiding the system UI.
 */
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

/**
 * If set, will toggle the system UI visibility upon interaction. Otherwise,
 * will show the system UI visibility upon interaction.
 */
private static final boolean TOGGLE_ON_CLICK = true;

/**
 * The flags to pass to {@link SystemUiHider#getInstance}.
 */
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

/**
 * The instance of the {@link SystemUiHider} for this activity.
 */
private SystemUiHider mSystemUiHider;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);
    counter = 0;
    add = (button) findViewById(R.id.bAdd);
    sub = (button) findViewbyId(R.id.bSub);
    Display = (TextView) findViewById(R.id.tvDisplay);
    add.SetOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            Display.setText("your total is " + counter);


        }
    });
sub.SetOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
        }
    });

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView,
            HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
        .setOnVisibilityChangeListener(new               SystemUiHider.OnVisibilityChangeListener() {
                // Cached values.
                int mControlsHeight;
                int mShortAnimTime;

XML:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    >

    <!--
     The primary full-screen view. This can be replaced with whatever view
     is needed to present your content, e.g. VideoView, SurfaceView,
     TextureView, etc.
    -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45sp"
android:textStyle="bold"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"


      />

  <Button
    android:layout_width="250sp"
    android:layout_height="wrap_content"
    android:text="Add one" 
    android:layout_gravity="center"
    android:textSize="20sp"
    android:id="@+id/bAdd"
    ></Button>
  <Button
    android:layout_width="250sp"
    android:layout_height="wrap_content"
    android:text="Subtract one" 
    android:layout_gravity="center"
    android:textSize="20sp"
    android:id="@+id/bSub"
    ></Button>

       </LinearLayout>
       <!--
       This FrameLayout insets its children based on system windows using
       android:fitsSystemWindows.
       -->  

【问题讨论】:

  • Java 中的类名区分大小写。所以,add = (button) findViewById(R.id.bAdd); 真的应该是add = (Button) findViewById(R.id.bAdd);

标签: java android xml eclipse debugging


【解决方案1】:
add.SetOnClickListener

SetOnClickListener 的s 必须为小写。

还有演员:

(button)

是错误的,如评论框中所述。应该是Button

【讨论】:

    【解决方案2】:
    add = (button)
    

    按钮的第一个字符应为大写。

    【讨论】:

      【解决方案3】:

      进行以下更改

      add = (Button) findViewById(R.id.bAdd);
      sub = (Button) findViewbyId(R.id.bSub);
      

      而不是

      add = (button) findViewById(R.id.bAdd);
      sub = (button) findViewbyId(R.id.bSub);
      

      而且在 SetOnClickListener 中 S 必须是 s( Small )

      :) 享受编码...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        • 2011-07-14
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多