2017-07-18 27 views
1

私はアプリを開発していますが、私は簡単なことでブロックされています。アンドロイドは同じダイアログを2回呼び出します

私の活動では、メールアドレスとアクティベーションを尋ねるダイアログ(AlertDialog.Builder)を表示します。これら2つのフィールドは、Rest APIでチェックされます。

アクティベーションコードが間違っている場合は、アクティビティを(インテントとともに)再開し、再びダイアログを表示します。

私は最初にアクティベーションコードが間違っているとわかりませんが、2回目は正しくダイアログに表示されますが、「送信」をクリックすると、残りのコールは実行されずに戻ります古い「状態」を思い出させるような、常に「無効な資格情報」です。

代わりに、私はアプリを実行し、私は正しい資格情報を入れて、すべてが大丈夫です。

ソースコード:

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" です。 私は

+1

ソースコードなしで何かアドバイスをするのは難しいです。 – eleven

+0

@eleven source code here – hteo

+0

間違った資格情報でアクティビティを再開したいのはなぜですか、なぜ単にダイアログを再度ポップアップしないのですか? – AndroidGeek

答えて

1

プロジェクト内の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);  
    } 
} 

追加androidmanifest.xmlファイルでのご<Application>タグでこの行:

<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);  

を削除することを忘れないでください。

お試しください。お知らせください。

+0

申し訳ありませんが、RestClientManagerは外部ライブラリ(https://github.com/julianfalcionelli/SimpleRESTClientHandler)です。変更することはできません。 – hteo

+0

OK、それを修正せず、アプリケーションクラス – AndroidGeek

+0

で初期化します。 。私のアクティビティでは、私がコールすれば正しいです:RestApplication ra = new RestApplication(); ??アプリを実行すると、restclientmanagerが初期化されていないことがわかります... – hteo

関連する問題