gpt4 book ai didi

java - 使用 AsyncTask 出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 17:30:28 25 4
gpt4 key购买 nike

我正在尝试向网站发送一个简单的帖子并收到响应,但我在实现 AsyncTask 来提供所需的辅助流时遇到问题。目前我收到一个java.lang.NullPointerException。你知道是什么原因造成的吗?

日志猫:

07-18 11:34:14.829: E/AndroidRuntime(8354): FATAL EXCEPTION: main
07-18 11:34:14.829: E/AndroidRuntime(8354): java.lang.NullPointerException
07-18 11:34:14.829: E/AndroidRuntime(8354): at ca.waddlesoft.smpclient_android.MainActivity$SmpProcessor.onPostExecute(MainActivity.java:50)
07-18 11:34:14.829: E/AndroidRuntime(8354): at ca.waddlesoft.smpclient_android.MainActivity$SmpProcessor.onPostExecute(MainActivity.java:1)
07-18 11:34:14.829: E/AndroidRuntime(8354): at android.os.AsyncTask.finish(AsyncTask.java:602)
07-18 11:34:14.829: E/AndroidRuntime(8354): at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-18 11:34:14.829: E/AndroidRuntime(8354): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-18 11:34:14.829: E/AndroidRuntime(8354): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 11:34:14.829: E/AndroidRuntime(8354): at android.os.Looper.loop(Looper.java:137)
07-18 11:34:14.829: E/AndroidRuntime(8354): at android.app.ActivityThread.main(ActivityThread.java:4514)
07-18 11:34:14.829: E/AndroidRuntime(8354): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:34:14.829: E/AndroidRuntime(8354): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 11:34:14.829: E/AndroidRuntime(8354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
07-18 11:34:14.829: E/AndroidRuntime(8354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
07-18 11:34:14.829: E/AndroidRuntime(8354): at dalvik.system.NativeStart.main(Native Method)

我的代码:

package ca.waddlesoft.smpclient_android;

import java.net.MalformedURLException;
import java.net.URL;

import ca.waddlesoft.smpclient_android.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv;
tv = (TextView)findViewById(R.id.TextView01);
setContentView(R.layout.activity_main);
SmpProcessor task = new SmpProcessor();
task.execute();
}

private class SmpProcessor extends AsyncTask <Void,Void,String> {
@Override
protected String doInBackground(Void... voids){
URL url = null;
try {
url = new URL("http://secure.g4apps.com");
String app="A1";
String data = "E1,E1,AT333AT333,9055627513,2012-02-02 02:02:02,2012-02-02 02:02:02,-454545.343434,434343.232323,A334A334A334A443X,1000000,3434";
byte [] databyte=data.getBytes();
String out = SmpClass.smpCall(url,app,data);
// String out = new String(smpresponse);
return out;
} catch (MalformedURLException e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(String result) {
tv.setText(result);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

最佳答案

2个错误:

  • 您在本地+在类中声明您的textView。只需在其中声明即可你的类(class)。
  • 在调用 setContentView 之前初始化 textView

试试这个:

package ca.waddlesoft.smpclient_android;

import java.net.MalformedURLException;
import java.net.URL;

import ca.waddlesoft.smpclient_android.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.TextView01);

SmpProcessor task = new SmpProcessor();
task.execute();
}

private class SmpProcessor extends AsyncTask <Void,Void,String> {

@Override
protected String doInBackground(Void... voids){
URL url = null;
try {
url = new URL("http://secure.g4apps.com");
String app="A1";
String data = "E1,E1,AT333AT333,9055627513,2012-02-02 02:02:02,2012-02-02 02:02:02,-454545.343434,434343.232323,A334A334A334A443X,1000000,3434";
// byte [] databyte=data.getBytes();
String out = SmpClass.smpCall(url,app,data);
// String out = new String(smpresponse);
return out;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
tv.setText(result);
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}

关于java - 使用 AsyncTask 出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11545621/

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