View.Visible
在Android中正好运行上述版本5.0,但不能在Kitkat版本上运行。没有得到实际问题,我搜索了很多,但没有得到解决方案是我的代码。提前谢谢
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 :(得分:1)
首先正确比较您的字符串值
Code Rectify
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 :(得分:0)
在java中,您可以使用equels
代替==
if (error.equalsIgnoreCase("false"))
{
button1.setVisibility(View.VISIBLE);
}
else
{
Toast.makeText(getApplicationContext(),"not found...!",Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
在onPostExecution()方法中附加调试器,并根据响应处理您的条件。因为&#34; jsonObject.get(&#34;错误&#34;)。toString()&#34; this.SO调试并且你可以回答你的反应.SetVisiblity没有任何问题( View.VISIBLE)。 休息你的代码似乎没问题。 试一试然后回复。
答案 3 :(得分: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();
}
}
}