已编辑。
现在我的应用程序总是在我尝试打开时停止。现在我真的很困惑因为这个。谁可以帮助我?
这是我的MainActivity代码:
public class MainActivity extends Activity {
TextView textview;
JSONObject json = null;
String str = "";
String GetId, GetPin1, GetPin2;
HttpResponse response;
Context context;
ProgressBar progressbar;
Button button;
ToggleButton tgl1, tgl2;
EditText EditText1, EditText2, EditText3;
String DataParseUrl = "http://192.168.0.114/smarthome/inputData.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new GetTextViewData(context).execute();
GetDataFromEditText();
SendDataToServer(GetId, GetPin1, GetPin2);
handler.post(timedTask);
progressbar = (ProgressBar) findViewById(R.id.progressBar1);
textview = (TextView) findViewById(R.id.textView5);
tgl1 = (ToggleButton) findViewById(R.id.toggleButton1);
tgl2 = (ToggleButton) findViewById(R.id.toggleButton2);
EditText1 = (EditText) findViewById(R.id.editText1);
EditText2 = (EditText) findViewById(R.id.editText2);
EditText3 = (EditText) findViewById(R.id.editText3);
// button = (Button) findViewById(R.id.button1);
progressbar.setVisibility(View.GONE);
}
// auto refresh every 5 seconds
Handler handler = new Handler();
Runnable timedTask = new Runnable() {
@Override
public void run() {
new GetTextViewData(context).execute();
handler.postDelayed(timedTask, 7000);
}
};
// class for take value from url
private class GetTextViewData extends AsyncTask<Void, Void, Void> {
public Context context;
public GetTextViewData(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost(
"http://192.168.0.114/smarthome/getdata_1.php");
try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
try {
textview.setText(json.getString("sensor"));
EditText1.setText(json.getString("pin1"));
// EditText1.setVisibility(View.INVISIBLE);
EditText2.setText(json.getString("pin2"));
// EditText2.setVisibility(View.INVISIBLE);
if (EditText1.getText().toString().equalsIgnoreCase("1")) {
// tgl1.setTextOff("Nyala");
tgl1.setChecked(true);
} else if (EditText1.getText().toString().equalsIgnoreCase("0")) {
// tgl1.setTextOn("Mati");
tgl1.setChecked(false);
}
if (EditText2.getText().toString().equalsIgnoreCase("1")) {
// tgl1.setTextOff("Nyala");
tgl2.setChecked(true);
} else if (EditText2.getText().toString().equalsIgnoreCase("0")) {
// tgl1.setTextOn("Mati");
tgl2.setChecked(false);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Hiding progress bar after done loading TextView.
progressbar.setVisibility(View.GONE);
}
}
// save value from android to database
public void GetDataFromEditText() {
// convert from toggle into edittext
tgl1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tgl1.isChecked()) {
EditText1.setText("1");
} else {
EditText1.setText("0");
}
}
});
// convert from toggle into edittext
tgl2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tgl2.isChecked()) {
EditText2.setText("1");
} else {
EditText2.setText("0");
}
}
});
GetId = EditText3.getText().toString();
GetPin1 = EditText1.getText().toString();
GetPin2 = EditText2.getText().toString();
// GetWebsite = editTextWebsite.getText().toString();
}
public void SendDataToServer(final String id, final String pin1,
final String pin2) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String QuickId = id;
String QuickPin1 = pin1;
String QuickPin2 = pin2;
// String QuickWebsite = website;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", QuickId));
nameValuePairs.add(new BasicNameValuePair("name", QuickPin1));
nameValuePairs.add(new BasicNameValuePair("email", QuickPin2));
// nameValuePairs.add(new
// BasicNameValuePair("website",QuickWebsite));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(DataParseUrl);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Submit Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "Data Submit Successfully",
Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(pin1, pin2);
}
// for back as exit
private Boolean exit = false;
@Override
public void onBackPressed() {
if (exit) {
finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}}
这是我的activity_main xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textAlignment="center"
tools:context="com.example.ta.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Silahkan pilih apa yang akan dikontrol"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20dp" >
</TextView>
<RelativeLayout
android:layout_width="290dp"
android:layout_height="match_parent"
android:textAlignment="center" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="22dp"
android:text="Lampu 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"
android:text="ToggleButton"
android:textOff="Mati"
android:textOn="Nyala" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/toggleButton1"
android:layout_marginTop="26dp"
android:text="Lampu 2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignRight="@+id/toggleButton1"
android:text="ToggleButton"
android:textOff="Mati"
android:textOn="Nyala" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/toggleButton2"
android:layout_marginTop="23dp"
android:text="Suhu Ruangan"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="@+id/toggleButton2"
android:layout_alignTop="@+id/textView4"
android:gravity="center"
android:hint="30"
android:textStyle="bold" />
<TextView
android:id="@+id/textView6"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_above="@+id/progressBar1"
android:layout_alignTop="@+id/textView5"
android:layout_toRightOf="@+id/textView5"
android:gravity="center"
android:text="ºC" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="2"
android:inputType="number" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/progressBar1"
android:ems="10"
android:hint="1"
android:inputType="number" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="3"
android:inputType="number" >
<requestFocus />
</EditText>
</RelativeLayout>
这是我的清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ta"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/iot_cloud"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这里是logcat错误:
10-26 15:06:13.167: E/AndroidRuntime(857): FATAL EXCEPTION: main
10-26 15:06:13.167: E/AndroidRuntime(857): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ta/com.example.ta.MainActivity}: java.lang.NullPointerException
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.os.Looper.loop(Looper.java:137)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-26 15:06:13.167: E/AndroidRuntime(857): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 15:06:13.167: E/AndroidRuntime(857): at java.lang.reflect.Method.invoke(Method.java:511)
10-26 15:06:13.167: E/AndroidRuntime(857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-26 15:06:13.167: E/AndroidRuntime(857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-26 15:06:13.167: E/AndroidRuntime(857): at dalvik.system.NativeStart.main(Native Method)
10-26 15:06:13.167: E/AndroidRuntime(857): Caused by: java.lang.NullPointerException
10-26 15:06:13.167: E/AndroidRuntime(857): at com.example.ta.MainActivity.GetDataFromEditText(MainActivity.java:174)
10-26 15:06:13.167: E/AndroidRuntime(857): at com.example.ta.MainActivity.onCreate(MainActivity.java:55)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.Activity.performCreate(Activity.java:5104)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-26 15:06:13.167: E/AndroidRuntime(857): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-26 15:06:13.167: E/AndroidRuntime(857): ... 11 more
抱歉我的英语不好。
答案 0 :(得分:0)
你可以这样做......
toggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked)
// save into databse code
else
// save into databse code
}
});
使用
Switch toggleButton= ((Switch) findViewById(R.id.switch_toggle));