2017-03-06 62 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

我已经通知你了 –

相关问题