在第三方应用中,当我点击Button "充值100元",第三方应用中直接给我onDestroy Log. 导致textview_money 显示不出100元, 还是0 元不变, 后面的TOAST也都不能显示, 还显示一下信息:
2020-09-07 07:48:46.493 11197-11209/com.example.alipaythirdpartyclient E/JavaBinder:*** Uncaught remote exception! (Exceptions are not yet supported across processes.)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()。
感谢,感谢。大伙教一下,无经验菜鸟一枚。
//支付成功,我们就去修改UI上的内容 //实际上是去修改数据库,其实,支付宝是通过回调的URL地址,通知我们的服务器 @Override public void onPaySuccess() throws RemoteException { Log.d(TAG, "onPaySuccess..."); //Toast.makeText(MainActivity.this, "充值成功", Toast.LENGTH_SHORT).show(); moneyTv.setText("100"); } @Override public void onPayFailed(int errorCode, String msg) throws RemoteException { Log.d(TAG, "errorCode is " + errorCode + ". errorMsg" + msg); Toast.makeText(MainActivity.this, "充值失败", Toast.LENGTH_SHORT).show(); }你的Log是有的吧,但是你更新UI是在主线程吗?这个要在主线程呀,否则就报错了。
你可以打一下线程的名称/ID,看看就知道了。
第三方支付的代码如下:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private AliPayService aliPayConnection; private boolean isBind; private Button buyBt; private TextView moneyTv; private ThirdPartyPayAction mThirdPartyPayAction; @Override protected void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate..."); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); doBindAliPayService(); initView(); setListener(); } private void setListener() { buyBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //进行充值 try { if (mThirdPartyPayAction != null) { mThirdPartyPayAction.requestPay("充值100元", 100.00f, new PayCallBack()); } } catch (RemoteException e) { e.printStackTrace(); } } }); } public class PayCallBack extends ThirdPartyPayResult.Stub { //支付成功,我们就去修改UI上的内容 //实际上是去修改数据库,其实,支付宝是通过回调的URL地址,通知我们的服务器 @Override public void onPaySuccess() throws RemoteException { Log.d(TAG, "onPaySuccess..."); //Toast.makeText(MainActivity.this, "充值成功", Toast.LENGTH_SHORT).show(); moneyTv.setText("100"); } @Override public void onPayFailed(int errorCode, String msg) throws RemoteException { Log.d(TAG, "errorCode is " + errorCode + ". errorMsg" + msg); Toast.makeText(MainActivity.this, "充值失败", Toast.LENGTH_SHORT).show(); } } private void doBindAliPayService() { Log.d(TAG, "doBindAliPayService"); Intent intent = new Intent(); intent.setAction("com.example.ALIPAY_THIRD_PARTY_PAY"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setPackage("com.example.alipay"); aliPayConnection = new AliPayService(); isBind = bindService(intent, aliPayConnection, BIND_AUTO_CREATE); } private void initView() { moneyTv = findViewById(R.id.money_tv); buyBt = findViewById(R.id.buy_bt); } private class AliPayService implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "onServiceConnected..." + service); mThirdPartyPayAction = ThirdPartyPayAction.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected..."); aliPayConnection = null; } } @Override protected void onDestroy() { super.onDestroy(); if (isBind && aliPayConnection != null) { Log.d(TAG, "onDestroy..."); unbindService(aliPayConnection); aliPayConnection = null; isBind = false; } } }跟老师的视频对比了一下,点击“充值100元”按钮的时候不应该就onDestroy(), 应该还是在onServiceConnected.... 求解。
那句 话已经告诉你了呀 has not called Looper.prepare()。
那你就上代码呗,这么多人阅读却没有人给你解答哦。
你不提供代码,别人怎么分析呢?
后右已经提示了:Log、代码、现象描述,可以图文结合。所以呀,还是要详细一点别人才快帮助你解决问题。
都查看过了,把onPaySuccess()里的TOAST去掉就没有上面的错误信息了,onPaySuccess()里的Log能打出来了, 但是就是Log下面一行的moneyTv.setText("100")一直不能在TEXTVIEW中显示100出来,怎么回事啊?