lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int p,long arg3) {
showPopUp(users[p],ids[p]);
}
});
public void showPopUp(String username,final String toid){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Challenge");
builder.setMessage("Do You Want to Challenge "+username+"..??");
builder.setNegativeButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String npath = cpath+toid;
HttpGet get = new HttpGet(npath);
Log.d("Challenge path" ,npath+"");
try {
HttpResponse response = client.execute(get);
Log.d("execution result " ,response.getStatusLine().getStatusCode()+"");
if(response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONArray jarray = new JSONArray(data);
JSONObject jobj = new JSONObject();
jobj = jarray.getJSONObject(0);
String error = jobj.getString("errormsg");
Log.d("challenge response", data);
if(error.equalsIgnoreCase("false"))
{
Toast.makeText(getApplicationContext(), "Challenge Has Started. Play Your Turn", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), DicesTwoColumn.class);
intent.putExtra("fromid", LoginScreen.uid);
intent.putExtra("toid", toid);
String sql = "INSERT INTO tbl_localgame (from_id,to_id,status) VALUES ('"+LoginScreen.uid+"','"+toid+"','active')";
db.execSQL(sql);
startActivity(intent);
}
else
Toast.makeText(getApplicationContext(), "You already sent a challenge to this user", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
builder.setPositiveButton("No", null);
builder.show();
}
这是 logcat 结果
05-06 10:39:57.622: D/Challenge path(875): http://www.base29.com/base3/Yatzy/challengeuser.php?fromid=7&toid=1
05-06 10:39:57.671: D/AndroidRuntime(875): Shutting down VM
05-06 10:39:57.671: W/dalvikvm(875): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-06 10:39:57.802: E/AndroidRuntime(875): FATAL EXCEPTION: main
05-06 10:39:57.802: E/AndroidRuntime(875): android.os.NetworkOnMainThreadException
05-06 10:39:57.802: E/AndroidRuntime(875): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-06 10:39:57.802: E/AndroidRuntime(875): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
05-06 10:39:57.802: E/AndroidRuntime(875): at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
05-06 10:39:57.802: E/AndroidRuntime(875): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
05-06 10:39:57.802: E/AndroidRuntime(875): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
05-06 10:39:57.802: E/AndroidRuntime(875): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.io.SocketInputBuffer.isStale(SocketInputBuffer.java:109)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.AbstractHttpClientConnection.isStale(AbstractHttpClientConnection.java:205)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.conn.AbstractClientConnAdapter.isStale(AbstractClientConnAdapter.java:185)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:336)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-06 10:39:57.802: E/AndroidRuntime(875): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-06 10:39:57.802: E/AndroidRuntime(875): at com.base29.flipit.GamePlayersList$2.onClick(GamePlayersList.java:86)
05-06 10:39:57.802: E/AndroidRuntime(875): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-06 10:39:57.802: E/AndroidRuntime(875): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 10:39:57.802: E/AndroidRuntime(875): at android.os.Looper.loop(Looper.java:137)
05-06 10:39:57.802: E/AndroidRuntime(875): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-06 10:39:57.802: E/AndroidRuntime(875): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:39:57.802: E/AndroidRuntime(875): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 10:39:57.802: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-06 10:39:57.802: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-06 10:39:57.802: E/AndroidRuntime(875): at dalvik.system.NativeStart.main(Native Method)
它不会进入 try{ } 正文。在 logcat 上它显示Log.d("Challenge path" ,npath+"");
但出现强制关闭错误。适用于姜饼模拟器,但现在适用于冰淇淋三明治