有FATAL异常:主java.lang.nullPointerException

时间:2015-01-20 20:11:46

标签: android listview android-asynctask

我有FATAL异常:在我的代码中主java.lang.nullPointerException但我不知道为什么因为我的代码似乎很好。

MainActivity.java文件

package com.bitbytestudios.iquiz;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class MainActivity extends Activity implements OnTaskCompleted {

public static String TAG_QUIZNAME = "quizname";
public static String TAG_FILENAME = "filename";

private ListView listview;
private ListViewAdapter adapter;

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

    listview = (ListView) findViewById(R.id.listview);

    setTitle("Select Quiz");

    new DownloadJSON(MainActivity.this, new MainActivity())
            .execute("iquiz_quizzes.json");
}

@Override
public void onTaskCompleted(JSONObject result) {
    ArrayList<Quizzes> quizzesArray = new ArrayList<Quizzes>();

    try {
        // Locate the array name in JSON
        JSONArray jsonarray = result.getJSONArray("quizzes");

        //Log.e("momersaleem", String.valueOf(jsonarray.length()));

        for (int i = 0; i < jsonarray.length(); i++) {

            JSONObject quizObject = jsonarray.getJSONObject(i);

            String quizName = quizObject.getString(TAG_QUIZNAME);
            String fileName = quizObject.getString(TAG_FILENAME);

            quizzesArray.add(new Quizzes(quizName, fileName));
        }
    } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    adapter = new ListViewAdapter(MainActivity.this, quizzesArray);
    listview.setAdapter(adapter);
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
}
}

ListViewAdapter.java文件

package com.bitbytestudios.iquiz;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

    Context context;
    ArrayList<Quizzes> quizzes;

    public ListViewAdapter(Context context, ArrayList<Quizzes> quizzes) {
        this.context = context;
        this.quizzes = quizzes;
    }

    @Override
    public int getCount() {
        return quizzes.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        TextView quizName;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);

        // Get the position
        String quiz = quizzes.get(position).quizName;

        // Locate the TextViews in listview_item.xml
        quizName = (TextView) itemView.findViewById(R.id.quizName);
        quizName.setText(quiz);

        // Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                MainActivity.TAG_FILENAME = quizzes.get(position).fileName;

                Log.e("momersaleem", MainActivity.TAG_FILENAME);
                //Intent intent = new Intent(context, Quiz.class);

                // Start SingleItemView Class
                //context.startActivity(intent);

            }
        });
        return itemView;
    }
}

DownloadJSON.java文件

package com.bitbytestudios.iquiz;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class DownloadJSON extends AsyncTask<String, String, JSONObject> {

    private Context context;
    private OnTaskCompleted listener;
    ProgressDialog pDialog;

    // constructor
    DownloadJSON(Context context, OnTaskCompleted listener) {
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // show progress dialog box
        pDialog = new ProgressDialog(context);
        // pDialog.setTitle("Loading Channels...");
        pDialog.setMessage("Loading Quizzes, Please wait...");
        // pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject jsonobject = JSONLoader.getJSON(params[0]);
        return jsonobject;
    }

    // called after doInBackground finishes
    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        listener.onTaskCompleted(result);
    }

}

LogCat视图

01-21 01:06:55.943: I/System.out(1258): [CDS]close[35047]
01-21 01:06:55.943: I/System.out(1258): close [socket][/0.0.0.0:35047]
01-21 01:06:55.944: I/System.out(1258): close [socket][/0.0.0.0:35047]
01-21 01:06:55.949: D/AndroidRuntime(1258): Shutting down VM
01-21 01:06:55.949: W/dalvikvm(1258): threadid=1: thread exiting with uncaught exception (group=0x40ea7258)
01-21 01:06:55.950: E/AndroidRuntime(1258): FATAL EXCEPTION: main
01-21 01:06:55.950: E/AndroidRuntime(1258): java.lang.NullPointerException
01-21 01:06:55.950: E/AndroidRuntime(1258):     at com.bitbytestudios.iquiz.MainActivity.onTaskCompleted(MainActivity.java:60)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at com.bitbytestudios.iquiz.DownloadJSON.onPostExecute(DownloadJSON.java:45)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at com.bitbytestudios.iquiz.DownloadJSON.onPostExecute(DownloadJSON.java:1)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at android.os.AsyncTask.finish(AsyncTask.java:602)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at android.os.Looper.loop(Looper.java:137)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at android.app.ActivityThread.main(ActivityThread.java:4508)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at java.lang.reflect.Method.invoke(Method.java:511)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-21 01:06:55.950: E/AndroidRuntime(1258):     at dalvik.system.NativeStart.main(Native Method)

listview_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

我在MainActivity.java文件的以下行中出错。

adapter = new ListViewAdapter(MainActivity.this, quizzesArray);
listview.setAdapter(adapter);

请帮我摆脱这个错误。

谢谢,

momersaleem

1 个答案:

答案 0 :(得分:1)

这很可能是因为您将虚假MainActivity传递给了下载程序:

new DownloadJSON(MainActivity.this, new MainActivity())
        .execute("iquiz_quizzes.json");

将您的第二个参数更改为this。您无法在new上致电Activity