按下按钮时应用程序崩溃

时间:2015-10-30 15:58:44

标签: java android

按下“Lets's Play”按钮后,我的应用程序崩溃了。 我刚刚开始开发..它甚至不是产生错误的文本,我只尝试做tv1.setText(“test”);它也崩溃了。

这是我的代码:

    package com.example.adam.casinoadam;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

    ImageView iv1;
    ImageView iv2;
    ImageView iv3;
    TextView tv1;
    Button btn1;
    int x,y,z;
    int coins=15;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        tv1 = (TextView) findViewById(R.id.tv1);
        btn1 = (Button) findViewById(R.id.btn1);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    public void Start(View view) {
        if(coins==0) {
            Toast.makeText(this, "Sorry. Good Bye!", Toast.LENGTH_LONG).show();
            finish();
        }

        coins=coins-1;
        tv1.setText("Money: "+coins+" Coins");


        x=1+(int)((5-1+1)*Math.random());
        y=1+(int)((5-1+1)*Math.random());
        z=1+(int)((5-1+1)*Math.random());


        if(x==1)iv1.setImageResource(R.drawable.cherry);
        if(x==2)iv1.setImageResource(R.drawable.tiltan);
        if(x==3)iv1.setImageResource(R.drawable.bell);


        if(y==1)iv2.setImageResource(R.drawable.cherry);
        if(y==1)iv2.setImageResource(R.drawable.tiltan);
        if(y==1)iv2.setImageResource(R.drawable.bell);


        if(z==1)iv3.setImageResource(R.drawable.cherry);
        if(z==1)iv3.setImageResource(R.drawable.tiltan);
        if(z==1)iv3.setImageResource(R.drawable.bell);


        if(x==y && y==z && z==x){
            Toast.makeText(this,"You won !",Toast.LENGTH_LONG).show();
            coins=coins+10;
}
}
}

6 个答案:

答案 0 :(得分:0)

用以下代码替换你的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv1 = (ImageView) findViewById(R.id.iv1);
    iv2 = (ImageView) findViewById(R.id.iv2);
    iv3 = (ImageView) findViewById(R.id.iv3);
    tv1 = (TextView) findViewById(R.id.tv1);
    btn1 = (Button) findViewById(R.id.btn1);1
}
  如果您提供的布局中存在

findViewById(),则会返回该视图   在setContentView()中,否则返回null,那就是   发生在你身上

     

如果你setContentView(R.layout.activity_main);   然后调用findViewById(R.id.iv1);它将返回一个View   是您的布局,但如果您之前致电findViewById(R.id.iv1);   setContentView(R.layout.activity_main);此后它将返回null   您的活动没有附加视图。

答案 1 :(得分:0)

在onCreate方法中调用setContentView之前,无法初始化视图对象。如果初始化视图对象,它将获得空值,因为活动不会获取它的布局文件。

iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
tv1 = (TextView) findViewById(R.id.tv1);
btn1 = (Button) findViewById(R.id.btn1);

这5行必须在onCreate方法的这两行之后:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

所以,你的代码必须是:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv1 = (ImageView) findViewById(R.id.iv1);
    iv2 = (ImageView) findViewById(R.id.iv2);
    iv3 = (ImageView) findViewById(R.id.iv3);
    tv1 = (TextView) findViewById(R.id.tv1);
    btn1 = (Button) findViewById(R.id.btn1);
}

答案 2 :(得分:0)

像这样修改你的代码:

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

        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        tv1 = (TextView) findViewById(R.id.tv1);
        btn1 = (Button) findViewById(R.id.btn1);
    }

findViewById 必须在 setContentView

之后

答案 3 :(得分:0)

您必须在setContentView(R.layout.activity_main);

之后定义视图

答案 4 :(得分:0)

使用此onCreate()替换您的代码首先,您必须setContentView(R.layout.activity_main)投标Button以及所有内容。

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

所以最后你的代码看起来像这样。

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

    iv1 = (ImageView) findViewById(R.id.iv1);
    iv2 = (ImageView) findViewById(R.id.iv2);
    iv3 = (ImageView) findViewById(R.id.iv3);
    tv1 = (TextView) findViewById(R.id.tv1);
    btn1 = (Button) findViewById(R.id.btn1);
}

答案 5 :(得分:0)

您必须尝试这样做,因为在contentview定义布局之前,您无法在程序中使用(findviewbyid)函数

  @Override

 protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv1 = (ImageView) findViewById(R.id.iv1);
    iv2 = (ImageView) findViewById(R.id.iv2);
    iv3 = (ImageView) findViewById(R.id.iv3);
    tv1 = (TextView) findViewById(R.id.tv1);
    btn1 = (Button) findViewById(R.id.btn1);

}