我有以下情况:
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类显示错误时:
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()
作为锚定弹出窗口并对其起作用的视图,但是在这种特殊情况下,调用工具提示类的活动没有操作栏(这是唯一的,其他所有活动都具有自定义操作栏)。
我在这里问是否有一种方法可以使用虚拟的,以编程方式创建的视图在这种情况下使用。
或者也许我做错了,你有一个很好的建议:)
提前致谢
最佳答案
经过一番尝试,我终于找到了解决方案:我选择使用两种不同的方法,一种用于带动作栏的活动,一种用于不带动作栏的活动:
public void showNow() { //THIS IS FOR ACTIVITY WITH ACTION BAR
pn.showAtLocation((Activity)con.getActionBar().getCustomView(), Gravity.NO_GRAVITY, 0, 180);
}
public void showNowNoActBar() { //THIS IS FOR ACTIVITY WITHOUT ACTION BAR
pn.showAtLocation(con.findViewById(R.id.dummy_hidden_view), Gravity.NO_GRAVITY, 0, 180);
}
显然
R.id.dummy_hidden_view
是在活动的XML布局中定义的,而没有这样的自定义操作栏:<TextView
android:id="@+id/dummy_hidden_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="gone" />
我认为这两种方法都只能用一种方法解决,但到目前为止,我可以依靠我的“双重方法”解决方案来满足需要。希望它可以帮助别人。仍然存在这样一个事实,我将对如何处理此问题提出任何更好的建议。