0

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

4 回答 4

3

2个错误:

  • 你在课堂上声明你的textView本地+。只需在您的班级中声明即可。
  • textView在调用之前初始化你的setContentView

试试这个:

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;
    }


}
于 2012-07-18T16:13:32.007 回答
1

把这一行:

tv = (TextView)findViewById(R.id.TextView01);

在这一行之后:

setContentView(R.layout.activity_main);
于 2012-07-18T16:07:12.683 回答
1

您有一个局部 tv变量,其中包含您的TextView. 由于您没有将引用放入您的tv字段中,因此它将为空,因此NullPointerException.

您可以通过简单地删除局部变量的声明来解决此问题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();
    }

您还必须移动

setContentView(R.layout.activity_main);

操作 s 之前的行,View否则 android 框架会抱怨。

如果感到困惑,您可以在此处阅读有关变量范围的信息:Java 变量范围

于 2012-07-18T16:10:57.817 回答
0

tv 您在本地声明另一个。
所以结果findViewById存储在局部变量tv中,而不是保留的字段 中。从您的方法中 删除。 正如@Dheeraj 和@you786 在我之前指出的那样,您还应该 在尝试获取. 在那之后,它应该工作。tvnull
TextView tv;onCreate
setContentView(...)TextView

于 2012-07-18T16:07:50.627 回答