我试图使用下面的代码将值插入到mysql数据库中。出于某种原因,每次按下提交按钮时应用程序都会崩溃。我也添加了Internet权限。我无法理解logcat。任何帮助将不胜感激。
MainActivity.java
package com.example.testthis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText et;
Button btn;
String mname;
String query;
String mylink;
Activity context;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.editText1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mname = et.getText().toString();
try {
query = URLEncoder.encode(mname,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mylink = "http://necrecords.16mb.com/donordetails.php?fn="+query;
HttpAsyncTask hat = new HttpAsyncTask();
hat.execute(mylink);
}
});
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return httpRequestResponse(urls[0]);
}
protected void onPreExecute(){
super.onPreExecute();
pd = new ProgressDialog(context);
pd.setTitle("Reporting query");
pd.setMessage("Please wait..");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
if(pd!=null) pd.dismiss();
Toast.makeText(getApplicationContext(),"Successfully Sent!", Toast.LENGTH_LONG).show();
}
}
//For HttpAsync Functions: sending requests and receiving responses
public static String httpRequestResponse(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert InputStream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "InputStream did not work";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat的
04-21 19:34:24.582: I/dalvikvm(5064): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
04-21 19:34:24.582: W/dalvikvm(5064): VFY: unable to resolve virtual method 12215: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
04-21 19:34:24.582: D/dalvikvm(5064): VFY: replacing opcode 0x6f at 0x0000
04-21 19:34:24.582: I/dalvikvm(5064): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
04-21 19:34:24.582: W/dalvikvm(5064): VFY: unable to resolve virtual method 12221: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
04-21 19:34:24.582: D/dalvikvm(5064): VFY: replacing opcode 0x6f at 0x0000
04-21 19:34:24.582: I/dalvikvm(5064): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
04-21 19:34:24.582: W/dalvikvm(5064): VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
04-21 19:34:24.582: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x000e
04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 399: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 421: Landroid/content/res/TypedArray;.getType (I)I
04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 362: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 364: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
04-21 19:34:24.622: D/TextView(5064): Constructor - Got Res id for appearance for textColorPrimaryInverse
04-21 19:34:24.622: W/ResourceType(5064): Skipping entry 0x7f070035 in package table 0 because it is not complex!
04-21 19:34:24.622: D/TextView(5064): Constructor - Got appearance for textColorPrimaryInverse
04-21 19:34:24.622: D/TextView(5064): Constructor -- Got mEditTextBackgroundColor
04-21 19:34:24.702: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 46
04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 49
04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 50
04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 50
04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 50
04-21 19:34:24.722: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 52
04-21 19:34:24.762: D/OpenGLRenderer(5064): Enabling debug mode 0
04-21 19:34:30.742: D/AndroidRuntime(5064): Shutting down VM
04-21 19:34:30.742: W/dalvikvm(5064): threadid=1: thread exiting with uncaught exception (group=0x430ef140)
04-21 19:34:30.742: E/AndroidRuntime(5064): FATAL EXCEPTION: main
04-21 19:34:30.742: E/AndroidRuntime(5064): Process: com.example.testthis, PID: 5064
04-21 19:34:30.742: E/AndroidRuntime(5064): java.lang.NullPointerException
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.AlertDialog.<init>(AlertDialog.java:98)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.example.testthis.MainActivity$HttpAsyncTask.onPreExecute(MainActivity.java:88)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.AsyncTask.execute(AsyncTask.java:535)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.example.testthis.MainActivity$1.onClick(MainActivity.java:67)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.view.View.performClick(View.java:4478)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.view.View$PerformClick.run(View.java:18698)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.Handler.handleCallback(Handler.java:733)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.Looper.loop(Looper.java:149)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.ActivityThread.main(ActivityThread.java:5257)
04-21 19:34:30.742: E/AndroidRuntime(5064): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 19:34:30.742: E/AndroidRuntime(5064): at java.lang.reflect.Method.invoke(Method.java:515)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
04-21 19:34:30.742: E/AndroidRuntime(5064): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
第37行:
Activity context;
然后你永远不会初始化这个变量,在第88行你得到一个空指针异常:
pd = new ProgressDialog(context);
日志告诉您的内容:NullPointerException
。
编辑:只是为Popsta澄清。这是来自logcat的片段,它帮助我弄清楚发生了什么(就在最后):
04-21 19:34:30.742: E/AndroidRuntime(5064): java.lang.NullPointerException
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.AlertDialog.<init>(AlertDialog.java:98)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.example.testthis.MainActivity$HttpAsyncTask.onPreExecute(MainActivity.java:88)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.AsyncTask.execute(AsyncTask.java:535)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.example.testthis.MainActivity$1.onClick(MainActivity.java:67)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.view.View.performClick(View.java:4478)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.view.View$PerformClick.run(View.java:18698)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.Handler.handleCallback(Handler.java:733)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.os.Looper.loop(Looper.java:149)
04-21 19:34:30.742: E/AndroidRuntime(5064): at android.app.ActivityThread.main(ActivityThread.java:5257)
04-21 19:34:30.742: E/AndroidRuntime(5064): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 19:34:30.742: E/AndroidRuntime(5064): at java.lang.reflect.Method.invoke(Method.java:515)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 19:34:30.742: E/AndroidRuntime(5064): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
04-21 19:34:30.742: E/AndroidRuntime(5064): at dalvik.system.NativeStart.main(Native Method)