Android无法从静态上下文中引用

时间:2015-01-23 17:25:22

标签: java android static-methods

我是Android的初学者,这是我第一个创建一个名为GeoQuiz的应用程序,它基本上有几个问题,用户必须单击true / false或用作弊按钮请求答案。我是在第138行得到与非静态和静态上下文相关的这一行错误,我做错了什么?我正在尝试每次单击按钮作弊时,它会打开另一个窗口,该窗口位于不同的.xml文件和另一个java类中

package com.example.fusion.geoquiz;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.TextView;
import android.util.Log;
import android.content.Intent;
import android.content.Intent;
import java.util.*;
import static com.example.fusion.geoquiz.R.string.correct_toast;

public class QuizActivity extends ActionBarActivity {
    private Button mTrueButton;
    private Button mFalseButton;
    private ImageButton mNextButton;
    private ImageButton mPrevButton;
    private ProgressBar mProgress;
    private Button mCheatButton;
    private int mProgressStatus = 0;
    private TextView mQuestionTextView;
    private static final String TAG = "QuizActivity";
    private static final String KEY_INDEX = "index";
    private int checker = 0;

    private TrueFalse[] mQuestionBank = new TrueFalse[] {
            new TrueFalse(R.string.question_oceans, true),
            new TrueFalse(R.string.question_mideast, false),
            new TrueFalse(R.string.question_africa, false),
            new TrueFalse(R.string.question_americas, true),
            new TrueFalse(R.string.question_asia, true),
    };
    private int mCurrentIndex = 0;
    private void disablePrev(){
        if(mCurrentIndex== 0){
            mPrevButton = (ImageButton) findViewById(R.id.prev_button);
            mPrevButton.setEnabled(false);
        }
        else{
            mPrevButton = (ImageButton) findViewById(R.id.prev_button);
            mPrevButton.setEnabled(true);
        }
    }
    private void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getQuestion();
        mQuestionTextView.setText(question);
    }
    private void checkAnswer(boolean userPressedTrue) {
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
        int messageResId = 0;
        if (userPressedTrue == answerIsTrue) {
            messageResId = R.string.correct_toast;
            checker++;

            if(checker >0) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
                //mProgressStatus=checker;
                //mProgress.setProgress(mProgressStatus);
                disablePrev();
            }
        } else {
            messageResId = R.string.incorrect_toast;
            checker = 0;
        }
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        disablePrev();
        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
        updateQuestion();
        mTrueButton = (Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                checkAnswer(false);
            }
        });

        mFalseButton = (Button)findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
               checkAnswer(true);
            }
        });
            mNextButton = (ImageButton) findViewById(R.id.next_button);
            mNextButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(checker==1) {
                        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                        updateQuestion();
                        disablePrev();
                    }
                }
            });

            mPrevButton = (ImageButton) findViewById(R.id.prev_button);
            disablePrev();
        mPrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentIndex != 0){
                    mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
                    updateQuestion();
                    disablePrev();
                 }
            }
        });
        mQuestionTextView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(checker==1) {
                    mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                    updateQuestion();
                    disablePrev();
                }
            }
        });
        if (savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
        }
        mCheatButton = (Button)findViewById(R.id.cheat_button);
        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(QuizActivity.this, cheatActivity.startActivity(intent));
            }
        });
        disablePrev();
        updateQuestion();
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "onSaveInstanceState");
        savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
    }
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }
    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause() called");
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() called");
    }
    @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_quiz, 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);
    }
}

这是作弊类和cheatButton

的类
package com.example.fusion.geoquiz;

import android.app.Activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import android.widget.TextView;
import android.util.Log;
/**
 * Created by fusion on 1/21/2015.
 */
public class cheatActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
    }
}

2 个答案:

答案 0 :(得分:0)

不确定您在那里做了什么,但是您需要引用其他活动类。只需调用startActivity即可,因为它将从QuizActivity.this隐式调用。

Intent intent = new Intent(QuizActivity.this, cheatActivity.class);
startActivity(intent);

为了避免将来出现错误,请选择一个您将使用ActivityActionBarActivity的活动类。后者来自支持包。

答案 1 :(得分:0)

替换

Intent intent = new Intent(QuizActivity.this, cheatActivity.startActivity(intent));

Intent intent = new Intent(QuizActivity.this, cheatActivity.class);
startActivity(intent);

有关详细信息,请参阅docs