我不知道是否可以從非活動類(不是活動本身的類)啓動敬酒。我想在我的輸入過濾器類中使用我的敬酒,所以我首先嚐試將整個活動傳遞給其他類,但嚴重失敗。所以我決定將上下文本身傳遞給非活動類,在這種情況下它是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;
}
}
EnterInformation活動
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());
如果有一個崩潰,請從logcat中張貼堆棧跟蹤,而且應該有沒問題,可以從任何類中調用Toast函數,儘管您可能需要在UI線程中。 – 2013-02-28 02:41:29
I second @GabeSechan,請發佈堆棧跟蹤。 – 2013-02-28 02:54:21