gpt4 book ai didi

android两次调用相同的对话框

转载 作者:行者123 更新时间:2023-11-30 00:26:58 25 4
gpt4 key购买 nike

我正在开发一个应用程序,但我遇到了一件简单的事情。

在我的 Activity 中,我显示了一个询问邮件地址和激活的对话框 (AlertDialog.Builder)。这两个字段使用 Rest API 检查。

如果激活码错误,我将重新启动 Activity (使用 Intent)并再次显示对话框。

我不明白为什么,如果我第一次输入错误的激活码,第二次会正确出现对话框,但是当我单击“提交”时,应用程序不会运行 Rest 调用并始终返回“无效凭据”,就像它会提醒旧的“状态”一样。

相反,如果我运行该应用程序并输入正确的凭据,则一切正常。

有什么想法吗?

源代码:

 public class PinActivity extends Activity {

String mail;
String verification;
JSONObject responseServer;
BluetoothSocket bsocket;
ConnectedThreadBT cdbt;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
EditText mail_add;
EditText verification_code;

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_check);
setup();

dialogActivation();

}

@Override
public void onDestroy() {
super.onDestroy();
}

private void setup(){
RestClientManager.initialize(getApplicationContext()).enableDebugLog(true);
bsocket = BluetoothApplication.getBSocket();
//salvo codice attivazione sul pacchetto
cdbt=new ConnectedThreadBT(bsocket,mHandler, "PinActivity");
cdbt.start();



}


private void dialogActivation(){


android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_dialog_verification, null);
mail_add = (EditText) view.findViewById(R.id.mailAddress);
verification_code = (EditText) view.findViewById(R.id.verification_code);

builder.setView(view).
setPositiveButton(getApplicationContext().getResources().getString(R.string.submit), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {


//prendo e salvo credenziali
mail = mail_add.getText().toString();

verification = verification_code.getText().toString();
//invio dati al server
activatePPS();


}

});


builder.setCancelable(false);
builder.show();


}


private void activatePPS(){

dialogCheck();

String url = "....";

RestClientManager.getInstance().makeJsonRequest(Request.Method.POST, url, new RequestHandler<>(new RequestCallbacks<JSONObject, Error>()
{
@Override
public void onRequestSuccess(JSONObject response)
{

responseServer = response;

int reply_code = 0;
try {
reply_code = response.getInt("reply_code");
checkReplyCode(reply_code);

} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}

@Override
public void onRequestError(Error error)
{

}




}, paramsList()));


}

private void dialogCheck(){



android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_dialog_load_check, null);
builder.setView(view);
builder.setCancelable(false);
builder.show();




}

private void checkReplyCode(int reply_code) throws JSONException, IOException {



switch(reply_code){

case 0:

successActivation();

break;
case 1001:
//credenziali invalide
Toast.makeText(getApplicationContext(), getResources().getString(R.string.wrong_credentials), Toast.LENGTH_LONG).show();

Intent intent = new Intent(PinActivity.this, PinActivity.class);
startActivity(intent);


break;


}


}

private void successActivation() throws JSONException {


String access_token = responseServer.get("access_token").toString();
String nickname = responseServer.get(".....



new Handler().postDelayed(new Runnable() {
@Override
public void run() {

int value = sharedPref.getInt("step_conf",0);
if(value==0){
Intent intent = new Intent(getApplicationContext(), MethodCurveActivity.class);
intent.putExtra("style", 0);
startActivity(intent);
}
else{
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);

}

}
},3000);



}

private ArrayMap<String, String> paramsList(){


ArrayMap<String, String> parameters=new ArrayMap<>();
parameters.put("user_mail", mail);
parameters.put(.....

return parameters;
}

private void resetMobileDevice(){


String url = "....";



RestClientManager.getInstance().makeJsonRequest(Request.Method.POST, url, new RequestHandler<>(new RequestCallbacks<JSONObject, Error>()
{
@Override
public void onRequestSuccess(JSONObject response)
{

System.out.println("Risposta:"+response);
responseServer = response;

int reply_code = 0;
try {
reply_code = response.getInt("reply_code");
} catch (JSONException e) {
e.printStackTrace();
}
try {
checkReplyCode(reply_code);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}

@Override
public void onRequestError(Error error)
{

}

}, paramsList()));

}

private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {



}
}
};

}

重要的一点是在错误之后的“案例 1001”中。我已经尝试了 finish() 和所有删除 Activity 旧实例的方法...

最佳答案

在您的项目中创建 Application 类,并在其 onCreate 方法中初始化 RestClientManager,如下所示:

public class MyApp extends Application {
private final static String LOG_TAG = Application.class.getSimpleName();

@Override
public void onCreate() {
Log.d(LOG_TAG, "Application.onCreate - Initializing application...");
super.onCreate();
initializeApplication();
Log.d(LOG_TAG, "Application.onCreate - Application initialized OK");
}

private void initializeApplication() {
RestClientManager.initialize(getApplicationContext()).enableDebugLog(true);
}
}

在您的 <Application> 中添加这一行在 androidmanifest.xml 中标记文件:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".App"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

并确保你的单例结构应该是这样的:

    private static RestClientManager instance;

static void initInstance()
{
if (instance == null)
{
// Create the instance
instance = new RestClientManager();
}
}

public static RestClientManager getInstance()
{
// Return the instance
return instance;
}

记得去掉

RestClientManager.initialize(getApplicationContext()).enableDebugLog(true);    

来自您的主要 Activity 。

请试一试,然后告诉我。

关于android两次调用相同的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45171227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com