从上下文调用异步任务时出现空指针异常

时间:2014-05-13 13:08:33

标签: android android-listview android-asynctask

从上下文调用异步任务时,我的异步任务运行良好,但是, 将值设置为textview时获取空指针异常

我没有参考textview ...请帮助我从上下文类调用asynch任务时如何获取参考...

05-14 11:44:45.279: E/AndroidRuntime(18314): FATAL EXCEPTION: main
05-14 11:44:45.279: E/AndroidRuntime(18314): Process: com.example.asynctest, PID: 18314
05-14 11:44:45.279: E/AndroidRuntime(18314): java.lang.NullPointerException
05-14 11:44:45.279: E/AndroidRuntime(18314):    at com.example.asynctest.MainActivity$MyAsync.onPostExecute(MainActivity.java:50)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at com.example.asynctest.MainActivity$MyAsync.onPostExecute(MainActivity.java:1)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at android.os.AsyncTask.finish(AsyncTask.java:632)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at android.os.Looper.loop(Looper.java:136)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at java.lang.reflect.Method.invoke(Method.java:515)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-14 11:44:45.279: E/AndroidRuntime(18314):    at dalvik.system.NativeStart.main(Native Method)

MainClass.java

public class MainActivity extends Activity {

    TextView textView;
    String textctx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView) findViewById(R.id.textView1);
        textView.setText("Main class Text");
        MyContextClass contextClass=new MyContextClass(this);
        contextClass.MyContextMethod();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void callfromcontext(String contexttext) {               

        new MyAsync().execute(contexttext);

    }

    class MyAsync extends AsyncTask<String, String, String>
    {

        @Override
        protected String doInBackground(String... params) {
            String sometext=params[0]; 
            return sometext;
        }

        @Override
        protected void onPostExecute(String result) {

            System.out.println("result"+result);            
            textView.setText(result);

        }


    }

}

MyContextClass.java

public class MyContextClass {

    Context context;
    public MyContextClass(Context ctx) 
    {
        this.context=ctx;           
        MainActivity activity=new MainActivity();
        String contexttext="Context Class Text";
        activity.callfromcontext(contexttext);
    }

    public void MyContextMethod()
    {

    }

}

1 个答案:

答案 0 :(得分:0)

MainActivity activity=new MainActivity();

永远不要使用new实例化活动。对于你想要参加活动的任何事情,他们都不会有好处。

特别是在您的情况下:生命周期方法onCreate()不会为该活动对象运行,TextView引用有null

如果您确实需要发布活动,请使用Intent

如果您只需要获得对活动的引用,请将其作为参数传递,例如

void methodThatNeedsActivity(MainActivity activity) {
  activity.someMethod();
}

// call from MainActivity
otherObject.methodThatNeedsActivity(this);