我有一个问题要完成我的Android应用程序。首先,我会快速解释我在做什么。
我的应用程序就像一个投票系统,它只有2个按钮(是/否)。最初在单击按钮时创建逻辑,用是或否填充变量。
但是我试图传递按钮的直接值,而不是填写变量。此时,当我单击“是”按钮时,当您尝试解析时,应用程序会爆炸。
有人能帮助我吗?非常感谢你。下面是代码(java和xml接口)
MainActivity.java
package com.clubee.vote;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int loader = R.drawable.loader;
ImageView image = (ImageView) findViewById(R.id.imgVotacao);
String image_url = "http://clubee.com.br/imgs/vote_imgs/Edicao.png";
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, loader, image);
Button btnVotoSim = (Button) findViewById(R.id.btnSim);
btnVotoSim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new InsereVoto().execute();
}
});
}
class InsereVoto extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Inserindo Voto..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String tipoVoto = "sim";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tipoVoto", tipoVoto));
JSONObject json = jsonParser.makeHttpRequest(url_Insere_voto, "POST", params);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Intent i = new Intent(getApplicationContext(), Splashscreen.class);
startActivity(i);
finish();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText tipoVoto;
private static String url_Insere_voto = "http://dev.clubee.com.br/insereVoto.php";
private static final String TAG_SUCCESS = "success";
}
和activity_main.xml
<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:background="#fffcfffa"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/bkg_app" />
<TextView
android:id="@+id/infoVotacao"
style="@style/CodeFont"
android:layout_below="@id/imgLogo"
android:text="@string/infoVotacao"/>
<ImageView
android:id="@+id/imgVotacao"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="@+id/infoVotacao"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="bottom">
<Button
android:id="@+id/btnSim"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:text="SIM"
android:textColor="#FFFFFF"
android:layout_weight="1.0"
android:background="@drawable/votebtngreen_btn_default_holo_light"/>
<Button
android:id="@+id/btnNao"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:text="NÃO"
android:textColor="#FFFFFF"
android:layout_weight="1.0"
android:background="@drawable/votebtnred_btn_default_holo_light"/>
</LinearLayout>
logcat的:
02-14 13:16:36.995 9799-10217/com.clubee.vote E/JSON Parser﹕ Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
02-14 13:16:36.995 9799-10217/com.clubee.vote W/dalvikvm﹕ threadid=13: thread exiting with uncaught exception (group=0x415bdd88)
02-14 13:16:37.005 9799-10217/com.clubee.vote E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 Process: com.clubee.vote, PID: 9799 java.lang.RuntimeException: An error occured while executing doInBackground()