我正在尝试从类中显示Toast消息,该类是customExceptionHandler。但我无法做到这一点。
我见过像Displaying a Toast message from the Application class这样的类似问题但是,它仍然没有解决我的问题。这里有什么我想念的。
我可以在Logcat中看到Log语句,但toast没有显示
public class MyApplication extends Application {
private Thread.UncaughtExceptionHandler defaultUEH;
private Thread.UncaughtExceptionHandler unCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
Log.e("Inside Run", "******************** Inside uncaughtException ***************" + ex.getMessage());
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
}
};
public MyApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(unCaughtExceptionHandler);
}
}
我还尝试了另外一件事,我知道静态方法和变量不是最佳解决方案,但是我在一个util类中创建了一个静态方法,在静态变量中维护了一个活动上下文并用它来表示一个toast也没用?
喜欢
Util.showToast(ex.getMessage());
答案 0 :(得分:0)
Android Manifest文件,声明如下。
<application android:name="com.example.MyApplication">
</application>
然后写下课程:
public class MyApplication extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
}
现在,无处不在调用MyApplication.getAppContext()
来静态获取应用程序上下文。
答案 1 :(得分:0)
这对我有用
/**
* createConstants Type
*/
type createConstantsType =
<T extends string, U extends string>(namespace: T, prefix: U | null) =>
<V extends string>(...constants: V[]) => Record<V, string>;
/**
* function for creating namespaced constants
* @param {String} namespace the namespace for the constant to create
* @param {String} prefix the prefix for the constant to create
* @returns {Object} namespaced constants for a module/feature
*
* // Common approach
* export const NAMESPACE = 'manual';
* export const SIDEBAR_TOGGLE = `${NAMESPACE}/SIDEBAR_TOGGLE`;
* export const SIDEBAR_OPEN = `${NAMESPACE}/SIDEBAR_OPEN`;
* export const SIDEBAR_CLOSE = `${NAMESPACE}/SIDEBAR_CLOSE`;
*
* // Usage of this utility
* export const NAMESPACE = 'manual';
* export const SIDEBAR = createConstants(NAMESPACE, 'sidebar')('TOGGLE', 'OPEN', 'CLOSE');
*
* // which will generate:
* SIDEBAR = {
* TOGGLE: 'manual/SIDEBAR_TOGGLE',
* OPEN: 'manual/SIDEBAR_OPEN',
* CLOSE: 'manual/SIDEBAR_CLOSE',
* }
*
*/
export const createConstants: createConstantsType =
<T extends string, U extends string>(namespace: T, prefix: U | null = null) =>
<V extends string>(...constants: V[]): Record<V, string> => (
constants.reduce((result: Record<V, string>, constant: string): Record<V, string> => ({
[constant.toUpperCase()]:
`${namespace}/${(prefix) ? `${prefix.toUpperCase()}_` : ''}${constant.toUpperCase()}`,
...result,
}), {} as Record<V, string>)
);
答案 2 :(得分:-1)
如果你想在你的应用程序calss中显示Toast而不是必须有一个上下文来显示Toast,那么你可以在构造函数中传递Context或获取活动实例
我没有尝试过这段代码,但你应该试试这个:
private static ApplicationContext instance;
/**
* Your Constructor
*/
public MyApplication() {
instance = this;
}
/**
* Gets the application context.
* @return the application context
*/
public static Context getContext() {
if (instance == null) {
instance = new ApplicationContext();
}
return instance;
}
public static void showToast(String data) {
Toast.makeText(getContext(), data,
Toast.LENGTH_SHORT).show();
}