Android:HttpClient POST:NullPointerException

发布于 09-15 16:18 字数 5577 浏览 11 评论 0原文

自从几个月前购买 EVO 以来,我对 Android 开发还是个新手。

不管怎样,我正在使用 Eclipse 和 Android SDK 模拟器开发我的第一个 Android 应用程序。

我正在尝试执行一个简单的 HTTP POST。我的真实代码已硬编码了我的登录信息,因此我将其替换为测试信息,并且还更改了 url 以简单地 POST 到 google.com。

我在尝试查看 HTTP POST 请求响应时收到 NullPointerException。

我也已将互联网权限添加到我的清单 xml 文件中。

这是我的代码。

已编辑

提前致谢

::更新::

我已经初始化了 txtResult TextView 对象,并且收到了新的 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());
        }
    }
}

这是我的新 logcat 错误日志


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:40.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

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:40.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝有风,海豚有海2024-09-22 16:18:34

造成这种情况的原因可能有太多,如果您不告诉我们数字 60 是哪一行,我们将无法为您提供帮助。无论如何......到目前为止,我看到一个明确的 NullpointerException 情况:您首先调用 login() ,然后初始化 txtResult

login();
layout =  new LinearLayout(this);
txtResult = new TextView(this);

这当然会导致 NullPointerException 因为在 login() 中您正在执行以下操作:

txtResult.setText(HttpHelper.request(response));

在这种情况下,txtResult 尚未初始化,因此它为 null。

编辑:

叹息...在任何方法之外执行 TextView txtResult = new TextView(this); 都会导致另一个 NullPointerException。那是因为使用了this。您最好多了解一下它的含义,但让我给您一个简单的解释:this 是一个关键字,用于引用当前对象的实例。您执行的每个不在方法内部的初始化都会在调用构造函数之前调用(这称为延迟初始化),因此在这种情况下,对象尚未实例化,并且 this 将为 null。

你必须做的,我想你会明白的就是:

layout =  new LinearLayout(this);
txtResult = new TextView(this);
login();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文