1
我有以下情況:如何創建虛擬視圖錨彈出窗口的Android
private void startConnection() {
HashMap<String,String> hm = new HashMap<String,String>();
hm.put("email","[email protected]");
String URL = "192.168.1.2/test.php";
Db_connection db = new Db_connection(hm,URL,context);
db.startTest();
}
public class Db_connection {
private HashMap<String, String> params;
private String URL;
private ConnectionController CC;
private ToolTip tt;
private Context con;
private ProgressDialog dialog;
public Db_connection(HashMap<String,String> params,String URL,Context con) {
this.params = params;
this.URL = URL;
this.con = con;
this.dialog = new ProgressDialog(con);
this.dialog.setCancelable(true);
this.CC = ConnectionController.getInstance();
}
public void startTest() {
JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (dialog.isShowing()) { dialog.dismiss(); }
if (response != null && response.has("result")) {
try {
if (!response.getString("result").matches("error")) {
Toast.makeText(con, "Response: "+response.getString("result"), Toast.LENGTH_LONG).show();
}
else { Toast.makeText(con, response.getString("errmsg"), Toast.LENGTH_LONG).show(); return; }
VolleyLog.v("Response:%n %s", response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
ToolTip tt = new ToolTip(con, error.getMessage());
tt.showNow();
Log.e("Error: ", error.getMessage());
}
});
req.setRetryPolicy(rp);
CC.addToRequestQueue(req);
}
}
public class ToolTip {
private Context con;
private PopupWindow pn;
private View layout;
private ImageView cancel;
private TextView mess;
public ToolTip(Context con,String htmlMsg) {
this.con = con;
LayoutInflater inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.tooltip,null);
pn = new PopupWindow(layout,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,false);
pn.setAnimationStyle(R.style.AnimationToolTip);
cancel = (ImageView)layout.findViewById(R.id.tooltip_imageClose);
mess = (TextView)layout.findViewById(R.id.tooltip_mainText);
mess.setText(Html.fromHtml(htmlMsg));
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pn.dismiss();
}
});
}
public void showNow() {
pn.showAtLocation(/*HERE IS WHERE I DON'T KNOW WHAT TO PASS AS VIEW*/((Activity) con).getActionBar().getCustomView(), Gravity.NO_GRAVITY, 0, 180);
}
}
一切正常,但是當凌空請求拋出一個錯誤,並使用工具提示類顯示錯誤:
ToolTip tt = new ToolTip(con, error.getMessage());
tt.showNow();
我得到這個異常:
11-21 15:08:06.390: E/AndroidRuntime(22448): Process: com.tooltip.test, PID: 22448
11-21 15:08:06.390: E/AndroidRuntime(22448): java.lang.ClassCastException: com.tooltip.test.ConnectionController cannot be cast to android.app.Activity
11-21 15:08:06.390: E/AndroidRuntime(22448): at com.tooltip.test.ToolTip.showNow(ToolTip.java:46)
11-21 15:08:06.390: E/AndroidRuntime(22448): at com.tooltip.test.Db_connection$2.onErrorResponse(Db_connection.java:100)
11-21 15:08:06.390: E/AndroidRuntime(22448): at com.android.volley.Request.deliverError(Request.java:577)
11-21 15:08:06.390: E/AndroidRuntime(22448): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
11-21 15:08:06.390: E/AndroidRuntime(22448): at android.os.Handler.handleCallback(Handler.java:733)
11-21 15:08:06.390: E/AndroidRuntime(22448): at android.os.Handler.dispatchMessage(Handler.java:95)
11-21 15:08:06.390: E/AndroidRuntime(22448): at android.os.Looper.loop(Looper.java:136)
11-21 15:08:06.390: E/AndroidRuntime(22448): at android.app.ActivityThread.main(ActivityThread.java:5139)
11-21 15:08:06.390: E/AndroidRuntime(22448): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 15:08:06.390: E/AndroidRuntime(22448): at java.lang.reflect.Method.invoke(Method.java:515)
11-21 15:08:06.390: E/AndroidRuntime(22448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
11-21 15:08:06.390: E/AndroidRuntime(22448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
11-21 15:08:06.390: E/AndroidRuntime(22448): at dalvik.system.NativeStart.main(Native Method)
通常我通過這樣的:
((Activity) con).getActionBar().getCustomView()
的圖,該錨彈出窗口和它的作品,但在這種特殊情況下,活動呼叫提示類,沒有一個動作條(它是唯一一個,所有其他的活動確實有自定義動作條)。
我在這裏問是否有一種方法使用虛擬,程序創建的視圖在這種情況下使用。
也許我做錯了,你有一個很好的建議:)
在此先感謝