2013-05-14 100 views
4

我剛開始學習計算機科學和Android開發。我一直在通過一些helloworld演示來嘗試學習。錯誤膨脹類按鈕

因此,我試圖用onClickListener重寫一個程序,該程序有兩個按鈕的按鈕。雖然我沒有任何編譯錯誤,我的計劃是對我強制關閉:

十一月一日至五日:20:33.968:E/AndroidRuntime(3257):了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.multbuttontest/com.example.multbuttontest.MultiButtonActivity}:android.view.InflateException:二進制XML文件行#16:錯誤充氣類按鈕

我的XML文件看起來像這樣(對不起,我吸在格式化):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MultiButtonActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

和代碼:

package com.example.multbuttontest; 

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

public class MultiButtonActivity extends Activity implements View.OnClickListener{ 

Button button1, button2; 
int touchCount1, touchCount2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_multi_button); 

    button1 = (Button) findViewById(R.id.button1); 
    button1.setText("Touch Button 1!"); 
    button1.setOnClickListener(this); 

    button2 = (Button) findViewById(R.id.button2); 
    button2.setText("Touch Button 2!"); 
    button2.setOnClickListener(this); 
} 

public void onClick(View v){ 
    switch(v.getId()){ 
     case R.id.button1: 
      touchCount1++; 
      button1.setText("Touched b1 " + touchCount1 + " times(s)"); 
      break; 
     case R.id.button2: 
      touchCount2++; 
      button2.setText("Touched b2 " + touchCount2 + " times(s)"); 
      break; 
    } 
} 
} 

這是嚴格只是我學習的目的,並且代碼不吸。任何幫助,將不勝感激。

+0

是你的佈局文件嗎?它缺少一個關閉的RelativeLayout標籤。而不應該是

回答

1

這是一些可能會幫助你的代碼。

MainActivity:

public class MainActivity extends Activity 
{ 
    private TextView txt1; 
    private Button btnCount,btnClear; 
    private int num1 = 1; 


@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    txt1 = (TextView) findViewById(R.id.txt1); 

    btnCount = (Button) findViewById(R.id.button1); 
    btnClear = (Button) findViewById(R.id.button2); 


} 

public void sendMessage(View view) 
{ 
    txt1.setText("You have clicked"+ (num1) + "times"); 
    num1++; 

} 

public void clear(View view) 
{ 
    num1 = 1; 
    txt1.setText("You have clicked"+ (num1) + "times"); 
} 




} 

activity_main.xml中:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" 
    android:onClick="sendMessage" /> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_clear" 
    android:onClick="clear" /> 

<TextView   
    android:id="@+id/txt1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:text="@+string/pushed" /> 

輸出:

enter image description here

enter image description here

+0

再次感謝這麼多!我會試試這個。如果我能擁抱你,我會的。 –

8

這不是button,它是Button。 XML標籤指向框架中的java類。 Java是區分大小寫的。

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+0

Hooooly牛我是個白癡。但非常感謝你!仍然需要努力,但我現在非常快樂! –

相關問題