0

我不知道您是否可以从非活动类(不是活动本身的类)启动祝酒词。我想在我的输入过滤器类中使用我的 toast,所以我首先尝试将整个活动传递给另一个类,但严重失败。所以我决定将上下文本身传递给非活动类,在本例中是 InputFilterMinMax。从那里我使用该上下文尝试将 Toast 显示到当前活动。我的问题是,这是否可能,如果是这样,为什么我的 toast 尝试启动后我的应用程序会崩溃。谢谢。

package com.TechDigy.parabolicmotion;

import android.app.Activity;
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.Toast;

public class InputFilterMinMax implements InputFilter {

    private int min, max;
    Context context;

    public InputFilterMinMax(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterMinMax(String min, String max) {
        this.min = Integer.parseInt(min);
        this.max = Integer.parseInt(max);
    }

    public InputFilterMinMax(Context context2) {
        // TODO Auto-generated constructor stub
        context = context2;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {   
        try {
            int input = Integer.parseInt(dest.toString() + source.toString());
            if (!isInRange(min, max, input))
                Toast.makeText(context, "Max Angle 90 Degrees", Toast.LENGTH_LONG).show();
            if (isInRange(min, max, input))
                return null;
        } catch (NumberFormatException nfe) { }     
        return "";
    }

    private boolean isInRange(int a, int b, int c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}

输入信息活动

package com.TechDigy.parabolicmotion;

    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.text.InputFilter;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

public class EnterInformation extends Activity { 

    //Name the String which will be used to identify the file name of the SharedPreference
    public static final String REFS = "checkedornot";
    //final String[] names = new String[]{"DegreeLaunchInitialVelocity","DegreeLaunchInitialYVelocity","DegreeLaunchInitialXVelocity","InitialYVelocityInitialXVelocity"};
    boolean DegreeLaunchInitialVelocity,DegreeLaunchInitialYVelocity,DegreeLaunchInitialXVelocity,InitialYVelocityInitialXVelocity;
    boolean testout = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
        //Reference the Edit Texts
        final EditText InitialVelocity = (EditText) findViewById(R.id.eTInitialVelcity);
        final EditText DegreeOfLaunch = (EditText) findViewById(R.id.eTDegreeOfLaunch);
        final EditText InitialYVelocity = (EditText) findViewById(R.id.eTInitialYVelocity);
        EditText TimeOfFlight = (EditText) findViewById(R.id.eTTimeOfFlight);
        EditText DistanceXDirection = (EditText) findViewById(R.id.eTDistanceTraveled);
        EditText PeakHeight = (EditText) findViewById(R.id.eTPeakHeight);
        final EditText InitialXVelocity = (EditText) findViewById(R.id.eTInitialXVelcity);
    //  if(DegreeOfLaunch.getText().toString().length() >2){
        //  Toast.makeText(getApplicationContext(), "Max Angle 90 Degrees", Toast.LENGTH_LONG).show();
    //  }
        //Pass the context
        InputFilterMinMax context = new InputFilterMinMax(getApplicationContext());
4

2 回答 2

0

请尝试换getApplicationContext()

InputFilterMinMax context = new InputFilterMinMax(getApplicationContext());

EnterInformation.this

所以,这条线看起来像,

InputFilterMinMax context = new InputFilterMinMax(EnterInformation.this);
于 2013-02-28T02:51:33.507 回答
0

我遇到了同样的问题,但我解决了它。!在非活动类中,您只需宣布一个“公共静态字符串”。然后在您的 MainActivity 或其他活动中,您可以直接使用 Toast。

就我而言,我声明了一个非活动类 NoteDB。所以我 public static String S在课堂上声明。(您可以在课堂上更改 S 值。然后在我的 MainActivity 中,我宣布

Toast(MainActivity.this, NoteDB.S ,TOAST.SHORT_LENTGH).show();

它运作良好。

于 2015-04-16T15:35:19.413 回答