2014-02-26 69 views
2

我是新來的android開發,我有一些調試問題。我從一個名爲「TheNewBoston」的YouTube頻道學習,並在第10課遇到了問題。我沒有使用與教程中的人一樣的src包,因爲我找不到它,所以我想知道這是否是這個造成了這個問題。按鈕無法解析爲一種類型 - Android開發

添加和子按鈕都具有相同的'多行標記在此行 - 按鈕不能通過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. 
     --> 
+0

類Java中的名稱區分大小寫。所以,'add =(button)findViewById(R.id.bAdd);'應該是'add =(Button)findViewById(R.id.bAdd);' – ozbek

回答

5
add.SetOnClickListener 

SetOnClickListener的s必須是小寫。

而且演員:

(button) 

是錯誤的,因爲在評論欄中註明。它應該是Button

+1

super spotting。 – Raghunandan

0
add = (button) 

按鈕應該有第一個字符大寫。

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

:)享受編碼...