gpt4 book ai didi

安卓 : HttpClient POST : NullPointerException

转载 作者:行者123 更新时间:2023-11-30 04:50:46 25 4
gpt4 key购买 nike

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

无论如何,我正在使用 Eclipse 和 Android SDK 模拟器开发我的第一个 Android 应用。

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

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

我也已将互联网权限添加到我的 list 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

最佳答案

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

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

这当然会导致 NullPointerException 因为在 login() 中你正在做:

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

在那种情况下,txtResult 还没有被初始化,因此它是空的。

编辑:

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

你必须做的,我认为你会明白的是:

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

关于安卓 : HttpClient POST : NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3543211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com