按钮无法解析为类型 - Android开发

时间:2014-02-26 12:51:36

标签: java android xml eclipse debugging

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

add和sub按钮都具有相同的'此行的多个标记 - 按钮无法通过OnCreate方法中的类型'错误解析。

爪哇:

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.
       -->  

3 个答案:

答案 0 :(得分:5)

add.SetOnClickListener

SetOnClickListener的s必须是小写的。

演员:

(button)

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

答案 1 :(得分:0)

add = (button)

Button应该有第一个字符大写。

答案 2 :(得分:0)

进行以下更改

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(小)

:)享受编码...