我的应用程序一直在说" Project1一直在停止"当我尝试在模拟器中运行它。无法弄清楚原因

时间:2017-03-20 14:44:52

标签: android

这是我的代码,它只是在我运行它时一直停止,我无法弄清楚原因。这是我在主要活动中使用的代码:

package com.example.danacarlin.project1new;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class Activity1 extends AppCompatActivity {

private ImageView c_img, m_img, e_img;

int count = 0;
ImageView imgView = (ImageView) findViewById(R.id.imageView4);
TextView result_tv;
TextView count_tv;


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


    c_img = (ImageView) findViewById(R.id.imageView);
    m_img = (ImageView) findViewById(R.id.imageView2);
    e_img = (ImageView) findViewById(R.id.imageView3);

    result_tv = (TextView) findViewById(R.id.textResult);
    count_tv = (TextView)findViewById(R.id.textCount);


    MyOnClickListener myOnClickListener = new MyOnClickListener();
    c_img.setOnClickListener(myOnClickListener);
    m_img.setOnClickListener(myOnClickListener);
    e_img.setOnClickListener(myOnClickListener);
    }
}

这是MyOnClickListener类。一切似乎都可以正常编译,但是当我去运行它时它就不会运行。

package com.example.danacarlin.project1new;

import android.view.View;

/**
 * Created by DanaCarlin on 2017-03-20.
 */

class MyOnClickListener extends Activity1 implements   View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    int rand = (int) (Math.random() * 3 + 1); // get a random number



    //form 1 to 3

    int count=0;
    count++;
    switch (rand) {
        /*
* rand = 1 means computer is cat, * 2 represents mouse,
* 3 represents elephant
*/
        case 1:
            imgView.setImageResource(R.mipmap.cat); //computer
            switch (v.getId()) {
                case R.id.imageView: //player choose Rock
                    result_tv.setText("Result: " + "Tied!");
                    count_tv.setText("Round: " + count);
                    break;
                case R.id.imageView2: //player choose Paper
                    result_tv.setText("Result: " + "Win!");
                    count_tv.setText("Round: " + count);
                    break;
                case R.id.imageView3: //player choose Scissors
                    result_tv.setText("Result: " + "Lose!");
                    count_tv.setText("Round: " + count);
                    break;
            }
            break;

        case 2:
            imgView.setImageResource(R.mipmap.mouse); //computer choose   mouse

            switch (v.getId()) {
                case R.id.imageView:
                    result_tv.setText("Result: " + "Lose!");
                    count_tv.setText("Round: " + count);
                    break;
                case R.id.imageView2:
                    result_tv.setText("Result: " + "Tie!");
                    count_tv.setText("Round: " + count);
                    break;
                case R.id.imageView3:

                    result_tv.setText("Result: " + "Win!");
                    count_tv.setText("Round: " + count);
                    break;
            }
            break;

        case 3:
            imgView.setImageResource(R.mipmap.elephant); //computer
            switch (v.getId()) {
                case R.id.imageView:
                    result_tv.setText("Result: " + "Win!");
                    count_tv.setText("Round: " + count);
                    break;
                case R.id.imageView2:
                    result_tv.setText("Result: " + "Lose!");
                    count_tv.setText("Round: " + count);
                    break;
                case R.id.imageView3:
                    result_tv.setText("Result: " + "Tie!");
                    count_tv.setText("Round: " + count);
                    break;
            }
            break;
    }
}
}

我更改为imageView 1,2,3的图像按钮,但将imageView4保留为imageView。但现在它将一直运行,直到应用程序启动,进入屏幕,当我按下按钮时它会崩溃。这是我的onclick代码。

package project1.csis.pace.edu.project1;

导入android.view.View;

/ **  *由DanaCarlin于2017-03-26创建。  * /

class MyOnClickListener extends MainActivity implements  View.OnClickListener {
    @Override
    public void onClick(View v) {
  form 1 to 3
        choose Rock
        count++;// switch (rand) { /**

        //case 1:
        imgView.setImageResource(R.mipmap.cat1); //computer
        switch (v.getId()) {
            case R.id.cat: //player choose Rock
                result_tv.setText("Result: " + "Tied!");
                count_tv.setText("Round: " + count);
                break;
            case R.id.mouse: //player choose Paper
                result_tv.setText("Result: " + "Win!");
                count_tv.setText("Round: " + count);
                break;
            case R.id.elephant: //player choose Scissors
                result_tv.setText("Result: " + "Lose!");
                count_tv.setText("Round: " + count);
                break;
        }

        //case 2:
        imgView.setImageResource(R.mipmap.mouse1); //computer
        //choose Paper
        switch (v.getId()) {
            case R.id.cat:
                result_tv.setText("Result: " + "Lose!");
                count_tv.setText("Round: " + count);
                break;
            case R.id.mouse:
                result_tv.setText("Result: " + "Tie!");
                count_tv.setText("Round: " + count);
                break;
            case R.id.elephant:
                result_tv.setText("Result: " + "Win!");
                count_tv.setText("Round: " + count);
                break;
        }
        //case 3:
        imgView.setImageResource(R.mipmap.elephant1); //computer
        switch (v.getId()) {
            case R.id.cat:
                result_tv.setText("Result: " + "Win!");
                count_tv.setText("Round: " + count);
                break;
            case R.id.mouse:
                result_tv.setText("Result: " + "Lose!");
                count_tv.setText("Round: " + count);
                break;
            case R.id.elephant:
                result_tv.setText("Result: " + "Tie!");
                count_tv.setText("Round: " + count);
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

替换

super.onCreate(savedInstanceState)

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
    ],

    'api' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\VerifyParametersMiddleware::class,
        'throttle:60,1',
        'bindings',
    ],
];