好吧,我终于发现了我的错误。正如 ePeace 在他的回答中提到的那样。在我的 Asyntask 类中,我创建了一个构造函数,它抛出 nullpointerexception 是侦听器为空。
AsyncListener listener;
public ReportFactory(AsyncListener a)
{
if(a == null)
{
throw new NullPointerException("Listener cant be null");
}
this.setListener(a);
}
public interface AsyncListener {
public void onComplete(String url);
}
public void setListener(AsyncListener listener) {
this.listener = listener;
}
之后,在片段类中,我创建了一个监听器。(在类中有一个对监听器的全局引用,这样你就可以在片段生命周期的其他地方使用它)
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
listener = new AsyncListener()
{
@Override
public void onComplete(String url)
{
URL = url;
webview.loadUrl(URL);
}
};
}
最后,在 onCreate 方法中,我创建了我的 Asyntask 并将侦听器作为参数。
ReportFactory factory = new ReportFactory(listener);
factory.execute(usr, designFile);
希望它对以后可能需要它的你有用。非常感谢您抽出时间 ePeace。