1

我的应用程序遇到了另一个问题,但这次我的堆栈跟踪说了一些不同的东西,所以我只是希望有人可以帮助解决这个问题。我只需要有人告诉我出了什么问题以及我能做些什么来解决它。

07-26 13:40:26.983: E/AndroidRuntime(837): FATAL EXCEPTION: main
07-26 13:40:26.983: E/AndroidRuntime(837): java.lang.NullPointerException
07-26 13:40:26.983: E/AndroidRuntime(837):  at com.theproblemsolver.asynctask.onPostExecute(MainActivity.java:72)
07-26 13:40:26.983: E/AndroidRuntime(837):  at com.theproblemsolver.asynctask.onPostExecute(MainActivity.java:1)
07-26 13:40:26.983: E/AndroidRuntime(837):  at android.os.AsyncTask.finish(AsyncTask.java:631)
07-26 13:40:26.983: E/AndroidRuntime(837):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-26 13:40:26.983: E/AndroidRuntime(837):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-26 13:40:26.983: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 13:40:26.983: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:137)
07-26 13:40:26.983: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-26 13:40:26.983: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 13:40:26.983: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:511)
07-26 13:40:26.983: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-26 13:40:26.983: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-26 13:40:26.983: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)
07-26 13:40:31.994: E/Trace(864): error opening trace file: No such file or directory (2)

主要活动

public class MainActivity extends Activity {

    EditText et;
    Button getanswer;

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

        Button getanswer = (Button) findViewById(R.id.button1);
        getanswer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new asynctask().execute();
            Intent i = new Intent(MainActivity.this, ListView.class);
            startActivity(i);
        }

        //protected void launchNewActivity(){
        //startActivity(new Intent(MainActivity.this, ListView.class));


        });
    } 
}

      class asynctask extends AsyncTask<String, Void, JSONObject> {

             private static final String TAG_RESULTS = "ResultsSet";
             private static final String TAG_SUBJECT = "Subject";
             private static final String TAG_NUMANSWERS = "NumAnswers";
             private static final String TAG_QUESTION = "Question";
             private static final String TAG_QUESTION_CONTENT = "Content";
             private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
             private static final String TAG_QUESTION_ANSWERS = "Answers";
            public JSONArray json;

         @Override
         protected JSONObject doInBackground(String... params) {

             JSONParsser jParser = new JSONParsser();

             String URI = "http://example.com/json";

             JSONObject json = jParser.getJSONFromURI(URI);

             return json ;

         }{ }
            @Override
             protected void onPostExecute(JSONObject json) {

                 JSONArray ResultsSet = null ; {
                 try {
                     ResultsSet = json.getJSONArray(TAG_RESULTS);
                } catch (JSONException e) {
                   e.printStackTrace();
                }
            return;
             }}
      }

列表视图活动

public class ListView extends ListActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.listview);



    //protected void doInBackground(String... params) {
    JSONParsser jParser = new JSONParsser();

    String URI = "http://example.com/json";;

    JSONObject json = jParser.getJSONFromURI(URI); 

    //protected void onPostExecute(JSONObject json) {

     final String TAG_RESULTS = "ResultsSet";
     final String TAG_SUBJECT = "Subject";
     final String TAG_NUMANSWERS = "NumAnswers";
     final String TAG_QUESTION = "Question";
     final String TAG_QUESTION_CONTENT = "Content";
     final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
     final String TAG_QUESTION_ANSWERS = "Answers";

            JSONArray ResultsSet = null;

            ArrayList<HashMap<String, String>> questionList = new ArrayList<HashMap<String, String>>();
    try {
       ResultsSet = json.getJSONArray(TAG_RESULTS);

       for(int i = 0; i < ResultsSet.length(); i++){
           JSONObject r = ResultsSet.getJSONObject(i);

           String Subject = r.getString(TAG_SUBJECT);
           String NumAnswers = r.getString(TAG_NUMANSWERS);

           JSONObject Question = r.getJSONObject(TAG_QUESTION);
           String Content = Question.getString(TAG_QUESTION_CONTENT);
           String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER);
           String Answers = Question.getString(TAG_QUESTION_ANSWERS);

           HashMap<String, String> map = new HashMap<String, String>();

           map.put(TAG_SUBJECT, Subject);
           map.put(TAG_NUMANSWERS, NumAnswers);

           questionList.add(map);

       }
    } catch (JSONException e) {
       e.printStackTrace();
       }

    ListAdapter adapter = new SimpleAdapter(this, questionList,
            R.layout.row,
            new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] {
            R.id.Subject, R.id.NumAnswers });

    setListAdapter(adapter);


    android.widget.ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String Subject = view.findViewById(R.id.Subject).getContext().toString();

            Intent i = new Intent(getApplicationContext(), SingleListItem.class);
            i.putExtra(TAG_SUBJECT, Subject);
            startActivity(i);

        }

    });

}

}

单列表项

public class SingleListItem extends Activity {

    TextView subject;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.singlelistitem);

    subject = (TextView) findViewById(R.id.Subject2);

    Intent i = getIntent();
    String Subject = i.getStringExtra("TAG_SUBJECT");
    subject.setText(Subject);

    }
}
4

1 回答 1

0

试试这个:

您的 listview 活动应如下所示:

public class ListView extends ListActivity {

private ProgressDialog pDialog;

JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> questionList;

private static String url = "http://example.com/json";;

final String TAG_RESULTS = "ResultsSet";
final String TAG_SUBJECT = "Subject";
final String TAG_NUMANSWERS = "NumAnswers";
final String TAG_QUESTION = "Question";
final String TAG_QUESTION_CONTENT = "Content";
final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
final String TAG_QUESTION_ANSWERS = "Answers";


JSONArray ResultsSet = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview);

    questionList = new ArrayList<HashMap<String, String>>();

    new LoadAllData().execute();


    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {


        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == 100) {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
}


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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ListView.this);
        pDialog.setMessage("Loading Data. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }


    protected String doInBackground(String... args) {

        try {
                JSONObject json = jParser.getJSONFromUrl(url);
                ResultsSet = json.getJSONArray(TAG_RESULTS);

               for(int i = 0; i < ResultsSet.length(); i++) {
                   JSONObject r = ResultsSet.getJSONObject(i);
                   String Subject = r.getString(TAG_SUBJECT);
                   String NumAnswers = r.getString(TAG_NUMANSWERS);

                   JSONObject Question = r.getJSONObject(TAG_QUESTION);
                   String Content = Question.getString(TAG_QUESTION_CONTENT);
                   String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER);
                   String Answers = Question.getString(TAG_QUESTION_ANSWERS);

                   HashMap<String, String> map = new HashMap<String, String>();

                   map.put(TAG_SUBJECT, Subject);
                   map.put(TAG_NUMANSWERS, NumAnswers);
               }



        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;


    }

    protected void onPostExecute(String file_url) {

        pDialog.dismiss();
        runOnUiThread(new Runnable() {
        public void run() {
            ListAdapter adapter = new SimpleAdapter(this, questionList,
                    R.layout.row,
                    new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] {
                    R.id.Subject, R.id.NumAnswers });

            setListAdapter(adapter);

            }
        });

    }   
}
}

和 MainActivity

public class MainActivity extends Activity {

EditText et;
Button getanswer;

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

    Button getanswer = (Button) findViewById(R.id.button1);
    getanswer.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent i = new Intent(MainActivity.this, ListView.class);
        startActivity(i);
    }


    });
  } 
}

刚刚修复了所有右花括号:)

于 2013-07-27T02:45:40.213 回答