对于dobackground,异步任务不为null,而在执行后执行时为null

时间:2016-09-30 10:35:18

标签: android android-asynctask

您好我得到一个空指针异常,我不知道为什么。

我在后台调用我的数据库,我测试了数组“结果”的大小,但它不是空的。

但是当我在onpostexecute中使用结果时,我得到一个空指针异常:

这是我的异步类:

public class GetVideoInfoFromDataBase extends AsyncTask {

    // Paginated list of results for alarm database scan
    static PaginatedScanList<AlarmDynamoMappingAdapter> results;

    // The DynamoDB object mapper for accessing DynamoDB.
    private final DynamoDBMapper mapper;

    Context context;
    private AsyncInterface asyncInterface;

    public GetVideoInfoFromDataBase(Context context){
        mapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
        this.context = context;
        this.asyncInterface = (AsyncInterface) context;

    }

    @Override
    protected Object doInBackground(Object[] params) {
        System.out.println("The backgrond stuff is working 1");
        DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
        results = mapper.scan(AlarmDynamoMappingAdapter.class, scanExpression);
        return results;
    }

    @Override
    public void onPostExecute(Object obj) {

           asyncInterface.response(results);
    }
}

所以下面的行有空指针。

asyncInterface.response(results);

这是我的异步接口:

public interface AsyncInterface {
    void response(PaginatedScanList<AlarmDynamoMappingAdapter> output);
}

我这是调用异步任务的主类,如下所示: 公共类MainAlarmsFragment extends Fragment实现了AsyncInterface {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main_alarms, container, false);
        new GetVideoInfoFromDataBase(context).execute();
    return view;
}

  @Override
    public void response(PaginatedScanList<AlarmDynamoMappingAdapter> output) {

    }

提前感谢您的帮助

3 个答案:

答案 0 :(得分:1)

尝试用下面的

替换你的onPostExecute行
("#amount").bind("keyup keydown", function() {
    var amount = parseFloat($(this).val());
    if (amount) {
        if (amount < 500 || amount > 800) {
            $("span.paymentalert").html("Your payment must be between £500 and £800");
        } else {
            $("span.paymentalert").html("");
        }
    } else {
        $("span.paymentalert").html("Your payment must be a number");
    }
});

可能是你需要输入对象obj ..

答案 1 :(得分:1)

public GetVideoInfoFromDataBase(Context context,AsyncInterface asyncInterface){
        mapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
        this.context = context;
        this.asyncInterface = asyncInterface;

    }

替换Contructor

答案 2 :(得分:0)

更改此

    public GetVideoInfoFromDataBase(Context context){
        mapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
        this.context = context;
        this.asyncInterface = (AsyncInterface) context;

    }

到这个

    public GetVideoInfoFromDataBase(Context context,AsyncInterface asyncInterface){
        mapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
        this.context = context;
        this.asyncInterface = asyncInterface;

    }

UPDATE:现在执行asynctask就好了 新的GetVideoInfoFromDataBase(this,this).execute(); 如果您处于活动状态且您的活动正在实施 AsyncInterface 界面,则必须传递上下文 AsyncInterface 对象引用。