java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“android.view.view.mainactivity.findviewbyid(int)”

30byixjq  于 2021-07-09  发布在  Java
关注(0)|答案(4)|浏览(413)

我有一个名为mainactivity.java的类,它调用 AsyncTask 班级。最后一节课有 findViewById() 执行时返回以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View <mypackage>.MainActivity.findViewById(int)' on a null object reference

我不明白如何编辑 ImageView 定位于 R.layout.activity_main 在那之后 AsyncTask 完成工作。
主活动.java

public class MainActivity extends Activity {

    public MainActivity() {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Connection().execute();
    }

}

连接.java

public class Connection extends AsyncTask<String, Void, String> {
    public String result;

    //I know, this isn't correct, how can i do?
    public MainActivity MainActivity;

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
                //...

        return "a string";
    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
                    //...

            // Where the error is generated
            ImageView image = (ImageView) MainActivity.findViewById(R.id.image);

            //...

    }
}
wyyhbhjk

wyyhbhjk1#

将imageview作为类的变量

private ImageView image;

在oncreate上初始化

image = (ImageView) findViewById(R.id.image);

public class Connection extends AsyncTask<String, Void, String> {
        public String result;

        //I know, this isn't correct, how can i do?
        public MainActivity MainActivity;

        @Override
        protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
            //...

        return "a string";
    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
                //...

        // Where the error is generated
        //do other stuff with your imageview

        //...

    }
}
fsi0uk1n

fsi0uk1n2#

实际上我在做一个活动和服务应用程序。我也遇到了同样的问题,在bindservice中,我使用了bind\u important标志,而不是使用bind\u auto\u create。
一旦我换了旗子,它就成功地为我工作了。

elcex8rz

elcex8rz3#

您没有指定要传递给的参数 Connection.java AsyncTask .
我的一个学生也有同样的问题。尽管他在使用 Volley Library 为了 HttpConnection 主要问题是:他正在写作 URL 直接没有http协议前缀,即。 String post_url ="api/do_post"; 只需添加 http/https 在帖子url前面: String post_url ="http://api/do_post";

kuarbcqp

kuarbcqp4#

错误是

public MainActivity MainActivity;

从未初始化,因此指向null。要使代码正常工作,最基本的步骤是mainactivity

new Connection(this).execute();

连接

public class Connection extends AsyncTask<String, Void, String> {

    public MainActivity MainActivity;

    public Connection(MainActivity activity) {
        MainActivity = activity;
    }

但是在oncreate中创建任务并传递活动并不是最好的主意。此外,字段名应始终以小写字母开头。
最好的方法是将imageview传递给asynctask。在活动启动之前不要启动任务,并且在活动停止时不要忘记取消任务。

public final class MainActivity extends Activity {

    public MainActivity() {}

    private Connection connection;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (connection == null || connection.getStatus() != AsyncTask.Status.RUNNING) {
            connection = new Connection(imageView);
            connection.execute();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (connection != null && connection.getStatus() == AsyncTask.Status.RUNNING) {
            connection.cancel(true);
        }
    }

}

在connection.java中,将imageview存储为weakreference以避免泄漏。

public final class Connection extends AsyncTask<String, Void, String> {

    private final WeakReference<ImageView> imageViewRef;

    public Connection(ImageView view) {
        imageViewRef = new WeakReference<ImageView>(view);
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
                //...

        return "a string";
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
                //...

        final ImageView imageView = imageViewRef.get();
        // if the Activity is still alive, the ImageView will not be null
        if (imageView != null) {
            // set an image or whatever you need
            image.setImageResource(666);
        }

    }

相关问题