我正在尝试做一个简单的网站帖子并收到回复,但我在实施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;
}
}