在fragment中显示对话框出现异常

异常类型为:android.view.WindowManager$BadTokenException: Unable to add window

我在fragment中public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {中试图调用对话框静态工具类时出现该异常:当时上下文环境使用

context = getActivity().getApplicationContext();

调用方法为:

 mp = new MyProgress(context);
 mp.start();

静态工具类为:

package com.srit.zcxc.myview;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MyProgress extends View {

	private AlertDialog.Builder builder = null;
	private AlertDialog dialog = null;
	private LinearLayout linear = null;

	public MyProgress(Context context) {
		super(context);
		builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		linear = new LinearLayout(context);
		linear.setOrientation(LinearLayout.VERTICAL);
		linear.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		linear.setBackgroundColor(Color.BLACK);
		linear.setGravity(Gravity.CENTER);
		linear.setPadding(5, 5, 5, 5);
		ProgressBar pb = new ProgressBar(context);
		linear.addView(pb);

		TextView tv = new TextView(context);
		tv.setGravity(Gravity.CENTER);
		tv.setText("取消登录");
		tv.setTextColor(Color.WHITE);
		tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				stop();
			}
		});
		linear.addView(tv);
		builder.setView(linear);
	}

	public void setBackgroundDrawableByresourceId(int resourceId) {
		linear.setBackgroundResource(resourceId);
	}

	public void start() {
		dialog = builder.show();
	}

	public void stop() {
		if (dialog.isShowing()) {
			dialog.dismiss();
		}
	}

	public boolean isShowing() {
		return dialog.isShowing();
	}
}

解决方法为:

            mp = new MyProgress(this.getActivity());
            mp.start();
产生异常的原因:

获取上下文this.getApplicationContext())和 this的区别:

这里的this指的当然就是Acitivity.this , 指的是这个Acitivity的上下文,而this.getApplicationContext()指的则是整个应用

的上下文。

对于AlertDialog来说,是需要依赖一个View,而View是对应于Activity的。

那么为什么会报错呢,这里涉及到一个生命周期的问题了。

对于一个应用Context来说,它的生命周期是整个应用程序的生命周期,而对于Activity来说,当它销毁之后它的生命

周期就结束了。

AlertDialog是属于Acitivity的,当Activity销毁的时候它也必须销毁,所以这里我们指定是Activity的Context。


Fragment调用SQLite,需要先创建一个SQLiteOpenHelper类,并在该类实现数据库的创建和升级。然后在Fragment实例化该类,并调用其getReadableDatabase()或getWritableDatabase()方法获取数据库对象。通过该对象可以执行SQL查询语句,并获取查询结果,最后将结果显示在TextView上。 以下是一个示例代码: ```java public class MyFragment extends Fragment { private TextView textView; private SQLiteDatabase db; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my, container, false); textView = view.findViewById(R.id.text_view); // 实例化 SQLiteOpenHelper MyDatabaseHelper dbHelper = new MyDatabaseHelper(getActivity(), "mydb.db", null, 1); // 获取可读数据库对象 db = dbHelper.getReadableDatabase(); // 查询数据 Cursor cursor = db.rawQuery("select * from mytable", null); StringBuilder builder = new StringBuilder(); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); builder.append("Name: ").append(name).append(", Age: ").append(age).append("\n"); } cursor.close(); // 将结果显示在TextView上 textView.setText(builder.toString()); return view; } @Override public void onDestroy() { super.onDestroy(); // 关闭数据库 db.close(); } } ``` 在该示例,我们首先实例化了一个SQLiteOpenHelper类,然后获取可读数据库对象。接着执行了一条SQL查询语句,并将查询结果构造成一个字符串,最后将结果显示在TextView上。注意,在Fragment的onDestroy()方法需要关闭数据库对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值