Android : HttpClient POST : NullPointerException
I am new to Android development since my EVO purchase a few months ago.
Anyway, I am developing my first Android app using Eclipse and Android SDK emulator.
I am trying to do a simple HTTP POST. My real code has my login information hard coded in so I replaced it with test information and I changed the url as well to simply POST to google.com.
I am getting a NullPointerException when trying to view the HTTP POST request response.
I have added the internet permissions to my manifest xml file as well.
Here is my code.
editted
Thanks in advance
::updated::
I have initialized the txtResult TextView object and I am getting new NullPointerException.
package com.dougi.TexAgsPremiumWidget;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TexAgsPremiumWidget extends Activity {
LinearLayout layout = new LinearLayout(this);;
TextView txtResult = new TextView(this);
ProgressDialog pDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
login();
layout.addView(txtResult);
setContentView(layout);
pDialog = ProgressDialog.show(this, "Loading", "please wait...", true);
}
public void login()
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.google.com");
try
{
List nameValuePairs = new ArrayList(2);
nameValuePairs.add(new BasicNameValuePair("username", "test"));
nameValuePairs.add(new BasicNameValuePair("password", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
txtResult.setText(HttpHelper.request(response));
}
catch (ClientProtocolException e)
{
Log.i("httpClientProtocolException", e.toString());
}
catch (IOException e)
{
Log.i("httpClientIOException", e.toString());
}
}
}
here is my new logcat error log
08-22 20:57:40.153: ERROR/AndroidRuntime(960): ERROR: thread attach failed
08-22 20:57:40.883: ERROR/AndroidRuntime(966): Uncaught handler: thread main exiting due to uncaught exception
08-22 20:57:40.923: ERROR/AndroidRuntime(966): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dougi.TexAgsPremiumWidget/com.dougi.TexAgsPremiumWidget.TexAgsPremiumWidget}: java.lang.NullPointerException
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.os.Looper.loop(Looper.java:123)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at java.lang.reflect.Method.invoke(Method.java:521)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at dalvik.system.NativeStart.main(Native Method)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): Caused by: java.lang.NullPointerException
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.view.View.(View.java:1777)
08-22 20:57:4开发者_如何学C0.923: ERROR/AndroidRuntime(966): at android.view.ViewGroup.(ViewGroup.java:279)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.widget.LinearLayout.(LinearLayout.java:88)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at com.dougi.TexAgsPremiumWidget.TexAgsPremiumWidget.(TexAgsPremiumWidget.java:25)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at java.lang.Class.newInstanceImpl(Native Method)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at java.lang.Class.newInstance(Class.java:1479)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
08-22 20:57:40.923: ERROR/AndroidRuntime(966): ... 11 more
There could be too many reasons for that, and if you don't tell us which line is the number 60 we are not going to be able to help you. Anyway... so far I see a clear NullpointerException
case: you first call login()
and after that you initialize txtResult
login();
layout = new LinearLayout(this);
txtResult = new TextView(this);
That of course will cause a NullPointerException
because inside login()
you are doing:
txtResult.setText(HttpHelper.request(response));
In that case, txtResult
have not been initialized, thus it's null.
Edit:
Sigh... doing TextView txtResult = new TextView(this);
outside any method will cause another NullPointerException
. That's because the use of this
. You better read little bit more about what it means, but let me give you a simple explanation: this
is a keyword used to reference an instance of the object current object. Every initialization you do that is not inside a method will be called before the constructor is invoked (that's called lazy initialization), thus in that case the object has not been instantiated and this
will be null.
What you have to do, and I thought you were going to get the idea is:
layout = new LinearLayout(this);
txtResult = new TextView(this);
login();
精彩评论