当我按下按钮" btUpdPlata"它放在活动MainActivity
中,并显示列#34; Plata"的数据。 (登录后停止使用应用程序的相应用户的数据库中的数值)
我做了几次测试,但它总是最终导致应用程序崩溃,可能是什么?
plata.php
<?php
include_once("conn.php");
$plata=$_GET['plata'];
$query="SELECT * FROM signup where plata='{$plata}'";
$result=mysqli_query($conn ,$query);
while($row=mysqli_fetch_array($result)){
$output_info = "Plata ".$row['plata'];
echo "platita";
echo $output_info;
}
mysqli_close();
?>
背景plata
public class BackgroundWorkerPlata extends AsyncTask<String,Void,String> {
MainActivity ob;
Context context;
AlertDialog alertDialog;
BackgroundWorkerPlata (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "insert url here";
if(type.equals("plata")) {
try {
String plata = params[5];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("plata","UTF-8")+"="+URLEncoder.encode(plata,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Cargando dinero");
}
@Override
protected void onPostExecute(String result) {
// alertDialog.setMessage(result);
// alertDialog.show();
if (result.contentEquals("platita")) {
ob.PlataET.setText(result.toString().replace(',','\n'));
}
}
@Override
protected void onProgressUpdate (Void...values){
super.onProgressUpdate(values);
}
}
在MainActivity中:
public void cambiarTextoUpdatePlata(View v) {
String plata = PlataET.getText().toString();
String type = "plata";
BackgroundWorkerPlata backgroundWorkerPlata = new BackgroundWorkerPlata(this);
backgroundWorkerPlata.execute(type, plata);
TextView updatePlata = (TextView) findViewById(R.id.PlataET);
updatePlata.setText("Tienes " + plata + " Pesos");
{
}
按钮:
<Button
android:id="@+id/btUpdPlata"
android:onClick="cambiarTextoUpdatePlata"
android:text="Actualizar Plata" />
更新
碰撞:
08-06 03:11:05.690 23352-23352/net.prontocars.prontocars E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.prontocars.prontocars, PID: 23352
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at net.prontocars.prontocars.MainActivity.cambiarTextoUpdatePlata(MainActivity.java:45)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
更新2
修复textview后的新崩溃
08-06 05:03:31.719 12631-12631/net.prontocars.prontocars E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.prontocars.prontocars, PID: 12631
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contentEquals(java.lang.CharSequence)' on a null object reference
at net.prontocars.prontocars.BackgroundWorkerPlata.onPostExecute(BackgroundWorkerPlata.java:99)
at net.prontocars.prontocars.BackgroundWorkerPlata.onPostExecute(BackgroundWorkerPlata.java:43)
at android.os.AsyncTask.finish(AsyncTask.java:667)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
您可以在代码中看到,您在第一行使用了PlataET
并且给出了NullPointerException,我们可以看到您在几行之后创建了该对象,因此创建然后使用它。
试试这个,
public void cambiarTextoUpdatePlata(View v) {
PlataET = (TextView) findViewById(R.id.PlataET);
String plata = PlataET.getText().toString();
String type = "plata";
BackgroundWorkerPlata backgroundWorkerPlata = new BackgroundWorkerPlata(this);
backgroundWorkerPlata.execute(type, plata);
PlataET.setText("Tienes " + plata + " Pesos");
编辑错误2: 您正在使用null对象检查字符串,因此请更改此条件,
if (!TextUtils.isEmpty(result) && result.contentEquals("platita")) {
ob.PlataET.setText(result.toString().replace(',','\n'));
}