天天看點

Android學習筆記(一):AndroidStudio無法從靜态上下文中引入非靜态方法setGravity()和show()無法從靜态上下文中引入非靜态方法

無法從靜态上下文中引入非靜态方法

筆者在學習《Android程式設計權威指南第三版》的過程中,在第一章挑戰練習“定制toast消息”遇到了問題,筆者由于缺乏java基礎,遲遲不能解決。

原代碼

package com.bignerdranch.android.geoquiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton;
    private Button mFalseButton;

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

        mTrueButton = (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(QuizActivity.this,
                        R.string.correct_toast,
                        Toast.LENGTH_SHORT).show();
            }
        });

        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(QuizActivity.this,
                        R.string.incorrect_toast,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

           

這是第一章已經給出的關于toast部分的代碼,現在要求更改代碼使toast消息從螢幕頂部彈出而不是底部彈出,這需要用到setGravity.TOP指令。我嘗試寫出了如下代碼:

報錯的代碼

package com.bignerdranch.android.geoquiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton;
    private Button mFalseButton;

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

        mTrueButton = findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(QuizActivity.this,
                        R.string.correct_toast,
                        Toast.LENGTH_SHORT);
                Toast.setGravity(Gravity.TOP,0,0);//該句報錯
                Toast.show();//該句報錯

            }
        });

        mFalseButton = findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(QuizActivity.this,
                        R.string.incorrect_toast,
                        Toast.LENGTH_SHORT);
                Toast.setGravity(Gravity.TOP,0,0);//該句報錯
                Toast.show();//該句報錯

            }
        });
    }
}
           

編譯問題截圖

Android學習筆記(一):AndroidStudio無法從靜态上下文中引入非靜态方法setGravity()和show()無法從靜态上下文中引入非靜态方法

問題分析

在AppCompaActivity下onCreate的靜态類環境中錯誤的引用了非靜态方法。正确的做法,應該通過Toast toast =Toast.***的方法來實作。

解決問題後的代碼

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton;
    private Button mFalseButton;

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

        mTrueButton = findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast toast1 =Toast.makeText(QuizActivity.this,
                        R.string.correct_toast,
                        Toast.LENGTH_SHORT);
                toast1.setGravity(Gravity.TOP,0,0);
                toast1.show();


            }
        });

        mFalseButton = findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast toast2 =Toast.makeText(QuizActivity.this,
                        R.string.incorrect_toast,
                        Toast.LENGTH_SHORT);
                toast2.setGravity(Gravity.TOP,0,0);
                toast2.show();

            }
        });
    }
}
           

繼續閱讀