2017-03-06 39 views
0

View.Visible工作正常以上版本5.0在android但不工作在Kitkat版本。沒有得到實際的問題,我搜了很多,但沒有得到解決方案以下是我的代碼。謝謝你在前進View.Visible工作正常在棉花糖但不工作Kikat

public class mainActivity extends AppCompatActivity implements View.OnClickListener { 
     Button button1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_stb_commands); 

    button1 = (Button)findViewById(R.id.button1); 

    button1.setOnClickListener(this); 

    button1.setVisibility(View.GONE); 

} 
@Override 
public void onClick(View view) { 
    switch (view.getId()){ 
     case R.id.button1: 
      HashMap data = new HashMap(); 
      new mainActivity.AsyncTaskGenre(data).execute(); 
      break; 
     } 
    } 


@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(mainActivity.this); 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

    } 
    @Override 
    protected String doInBackground(Void... params) { 
     String request = "http://"; 
     try { 
      JSONObject json = jsonParser.makeHttpRequest(request, "POST",data); 
      return json.toString(); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     pDialog.dismiss(); 
     Log.d("RESULT ",""+s); 
     if (s != null) { 
      try { 
       JSONObject jsonObject = new JSONObject(s); 

       String error = jsonObject.get("error").toString(); 
       Log.d("RESULT", " " +error); 
       if (error.equals("false")) 
       { 
         button1.setVisibility(View.VISIBLE); 

       } 
       else 
       { 
        Toast.makeText(getApplicationContext(),"not found...!",Toast.LENGTH_SHORT).show(); 

       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
     else{ 
      Toast.makeText(getApplicationContext(),"check connection...!",Toast.LENGTH_LONG).show(); 
     } 
    } 
}    
+0

它應該爲Android的每一個版本而消失,因爲你的代碼告訴你要做那個button1.setVisibility(View.GONE);在onCreate – sadat

+1

錯誤的編碼比較'if(error ==「false」)' – Piyush

回答

0

起初比較你的字符串值正確

代碼整頓

if (error.equals("false")) // Why you set == ? 
      { 
        button1.setVisibility(View.VISIBLE); 

      } 
      else 
      { 
       Toast.makeText(getApplicationContext(),"not found...!",Toast.LENGTH_SHORT).show(); 

      } 

FYI

JSON響應是

{ 「錯誤」:假​​的, 「1」:{ 「ID」:6, 「名」: 「黃芪多糖」}}

String getError=jsonObject .getString("error").toString(); 

現在做你的代碼

if (error.equals("false")) 
      { 
        button1.setVisibility(View.VISIBLE); 

      } 
+1

它是json響應並且可以和marshmellow一起正常工作。 – Parsh

+0

@Parsh使用equals來代替== ==再次測試 –

+0

不工作。 .. :) – Parsh

0

在Java中,你可以使用equels代替==

if (error.equalsIgnoreCase("false")) 
     { 
       button1.setVisibility(View.VISIBLE); 

     } 
     else 
     { 
      Toast.makeText(getApplicationContext(),"not found...!",Toast.LENGTH_SHORT).show(); 

     } 
0

安裝調試器內onPostExec ution()方法,並根據響應處理您的條件。因爲「jsonObject.get(」error「)。toString()」this.SO調試和處理你的響應可能會有問題。setVisiblity(View.VISIBLE)沒有任何問題。 休息你的代碼似乎很好。 試試看,然後回覆。

0

我發現問題有解析問題謝謝你們。

JSONObject jsonObject = new JSONObject(s); 

       String error = jsonObject.get("error").toString(); 
       Log.d("RESULT", " " +error); 
       if (error.equals("false")) 
       { 
        Iterator keys = jsonObject.keys(); 
        JSONObject currentDynamicValue; 
        int i=0; 
        while (keys.hasNext()) { 
         // loop to get the dynamic key 
         String currentDynamicKey = (String) keys.next(); 
         if (!currentDynamicKey.equals("error")) { 
         // get the value of the dynamic key 
         currentDynamicValue = jsonObject.getJSONObject(currentDynamicKey); 
         list.put(i, currentDynamicValue.get("command_id").toString()); 
         i++; 
         final String command_id = currentDynamicValue.get("command_id").toString(); 
         if(command_id.equals("0")) 
         { 
          button1.setVisibility(View.VISIBLE); 


         } 
         else{ 
          Toast.makeText(getApplicationContext(), "Command not Found..!" , Toast.LENGTH_SHORT).show(); 
         } 

        } 
        } 
+0

我已經通知你了 –

相關問題