不幸的是(我的应用程序)已停止工作

时间:2012-08-11 22:17:30

标签: java android

好的,这发生在我创建一个简单的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);

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

3 个答案:

答案 0 :(得分:2)

API tells me,我们无法将ImageButton投射到Button。尝试将rstBtn声明为ImageButton并调整演员。

答案 1 :(得分:0)

rstBtn = (Button) findViewById(R.id.Reset);ImageButton Button继承时,您将ImageButton投射到Button

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

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

答案 2 :(得分:0)

private ImageButton rstBtn;

....

rstBtn = (ImageButton) findViewById(R.id.Reset);
rstBtn.setOnClickListener(........