0

好的,这发生在我创建一个简单的 ImageButton 时。我看不出我做错了什么......这是我的代码:

(我的 Activity.Xml 文件):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/tapbgh"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center"
        android:text="@string/level01"
        android:textColor="#FFFFFF" />
    <TextView
        android:id="@+id/Score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center"
        android:text="@string/click0"
        android:textColor="#FFFFFF"
        android:textSize="16pt" />
    <Chronometer
        android:id="@+id/Timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center_horizontal"
        android:text="01:00"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/Tap"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.81"
        android:background="@drawable/buttonbg" />
    <Button
        android:id="@+id/Menu"
        android:layout_width="270dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Menu" />

    <ImageButton
        android:id="@+id/Reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/resetbtn"
        android:src="@drawable/resetbtn" />
</LinearLayout>

(我的 .Java 文件):

package my.first.app;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
//Click Events Below This Line
public class Tap_Game_Activity extends Activity {
    private Button tapBtn;
    private Button rstBtn;
    int scr = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button Menu = (Button) findViewById(R.id.Menu);
        registerForContextMenu(Menu);
        tapBtn = (Button) findViewById(R.id.Tap);
        rstBtn = (Button) findViewById(R.id.Reset);
        rstBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView Score = (TextView) findViewById(R.id.Score);
                Score.setText("0");
                scr = 0;
            }
        });
        tapBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                scr = scr + 1;
                TextView Score = (TextView) findViewById(R.id.Score);
                Score.setText(String.valueOf(scr));
            }
        });
    }

// Options Menu

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);

        menu.setHeaderTitle("Menu");
        menu.add(0, view.getId(), 0, "Resume");
        menu.add(0, view.getId(), 0, "Quit");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle()=="Invoke Context Function 1") {
            contextFunction1(item.getItemId());
        }
        else if(item.getTitle()=="Invoke Context Function 2") {
            contextFunction2(item.getItemId());
        }
        else {
            return false;
        }
        return true;
    }

    public void contextFunction1(int id){
        Toast.makeText(this, "function 1 invoked!", Toast.LENGTH_SHORT).show();
    }

    public void contextFunction2(int id){

            new AlertDialog.Builder(this)
                   .setMessage("Are you sure you want to exit?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
                            Tap_Game_Activity.this.finish();
                       }
                   })
                   .setNegativeButton("No", null)
                   .show();
        }

 }

我觉得它可能在这一行:

rstBtn = (Button) findViewById(R.id.Reset);

但我还是不明白,任何帮助都会很棒,谢谢。

4

3 回答 3

2

API 告诉我,我们不能ImageButton转换为Button. 尝试声明rstBtnImageButton并调整演员表。

于 2012-08-11T22:22:49.373 回答
0

rstBtn = (Button) findViewById(R.id.Reset); 您正在转换ImageButtonButtonwhenImageButton继承Button.

http://developer.android.com/reference/android/widget/ImageButton.html

顺便说一句:请选择一个能更好地描述您的问题的标题,并包含您在问题中收到的每条错误消息。

于 2012-08-11T22:25:15.010 回答
0
private ImageButton rstBtn;

....

rstBtn = (ImageButton) findViewById(R.id.Reset);
rstBtn.setOnClickListener(........
于 2012-08-11T22:45:31.620 回答