【发布时间】:2017-02-13 12:53:17
【问题描述】:
如果我有条件,如何更新我的 UI 按钮以启用/关闭禁用按钮。条件:如果我得到值 = 1.0,则禁用按钮电源,如果我得到值 0,则禁用按钮电源关闭。我希望按钮始终显示新值。
这是我的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_controller, container, false);
mSwitchStatus = (TextView) rootView.findViewById(R.id.statusSwitch);
ImageButton On = (ImageButton)rootView.findViewById(R.id.on);
ImageButton Off = (ImageButton)rootView.findViewById(R.id.off);
callAsynchronousTask();
On.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getActivity().getApplication(), "ON", Toast.LENGTH_SHORT).show();
SendApi sendApi = new SendApi();
sendApi.execute();
}
});
Off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(getActivity().getApplication(), "OFF", Toast.LENGTH_SHORT).show();
SendApi sendApi = new SendApi();
sendApi.execute();
}
});
// Inflate the layout for this fragment
return rootView;
}
重复异步任务
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
GetApi getApi = new GetApi();
getApi.execute();
} catch (Exception e) {
android.util.Log.i("Error", "Error");
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 100);
}
GETApi 的异步任务
public class GetApi extends AsyncTask<Object, Object, Value[]> {
private final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
private final String SWITCHID = "xxxxxxxxxxxxxxxxxxxxxxxxx";
private ImageButton On,Off ;
@Override
protected Value[] doInBackground(Object... params) {
ApiClient apiClient = new ApiClient(API_KEY);
Variable statusSwitch = apiClient.getVariable(SWITCHID);
Value[] variableValues = statusSwitch.getValues();
return variableValues;
}
@Override
protected void onPostExecute(Value[] variableValues) {
String status = Double.toString(variableValues[0].getValue());
mSwitchStatus.setText(status);
if(status.equals("1.0")){
On.setVisibility(View.INVISIBLE);
}
}
}
Fragment_controller.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#1d4851"
tools:context=".activity.ControllerFragment">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/off"
android:src="@drawable/ic_off"
android:background="@null"
android:layout_marginTop="79dp"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/on"
android:layout_alignStart="@+id/on"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/on"
android:background="@null"
android:src="@drawable/ic_on"
android:layout_marginTop="87dp"
android:layout_alignTop="@+id/off"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Water Pump Controller "
android:id="@+id/textView2"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_marginTop="19dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
android:id="@+id/statusSwitch"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_marginTop="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
【问题讨论】:
-
你在这段代码中遇到了什么错误?
-
什么都没有,但是按钮没有更新,如果我改变 status.getText().toString().equals("1.0");我的应用程序崩溃@AnwarKamal
-
将其设为 status.getText().toString().equals("1");
-
或者给我看崩溃报告
-
嗨 @AnwarKamal 我无法制作 status.getText() 因为状态是字符串,这是我调试 E/AndroidRuntime 时的崩溃报告:致命异常:主进程:com.example.medyanto.iotsmartplant, PID: 8159 java.lang.NullPointerException at com.example.medyanto.iotsmartplant.activity.ControllerFragment$GetApi.onPostExecute(ControllerFragment.java:222) at com.example.medyanto.iotsmartplant.activity.ControllerFragment$GetApi.onPostExecute(ControllerFragment. java:198) 在 android.os.AsyncTask.finish(AsyncTask.java:632) 在这个 android 屏幕上:不幸的是,应用程序已经停止
标签: java android api android-asynctask nullpointerexception