如何从Android中的Parse.com解析数组

时间:2013-03-06 09:39:35

标签: android arrays nullpointerexception parse-platform

我在Android上的最终项目遇到了一些问题,如果有人可以提供帮助,我会非常感激。

我创建了一个Parse.com数据库,其中包含一个“会话”表,每当我的应用程序中的一个新会话获得startet时,我就会创建一个新行。在这一行中有一列获取整数数组。

我的问题是如何从这个数组中获取单个数组值。

所以这是我的Start-Activity,它将新对象放入我的parse.com表:

package de.lichtenberger.gottschalk.android;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class Liedwahl_Git extends Activity {




    private ArrayList<ParseObject> list;
    private ArrayList<SongData> details;
    private SongDataAdapter adapter;
    private ListView songListView;
    public Button ok;
    private EditText sessionText;
    public Boolean[] ableToPlay = new Boolean[30];

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

        Parse.initialize(this, "XxXxXxX", "xXxXxX"); 
        setupUI();
        new createListTask().execute();
        for(int i = 0; i<=29; i++){
            ableToPlay[i] = false;
        }



    }

    private void setupUI() {
        songListView = (ListView) findViewById(R.id.listeCheck);
        ok = (Button)findViewById(R.id.OkButton);
        ok.setVisibility(View.INVISIBLE);
    }

    private void setupOnClickListeners() {

        sessionText = (EditText)findViewById(R.id.newSession);

        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(sessionText.getText().toString().isEmpty()){

                    Toast.makeText(getApplicationContext(), "Bitte Sessionname angeben!", Toast.LENGTH_SHORT).show();

                }else{


                    ParseObject session = new ParseObject("Sessions");
                    session.put("Name", sessionText.getText().toString());
                    session.put("Online", true);


                    for(int i = 0; i<=29; i++){

                        if(ableToPlay[i]==true){
                            session.addUnique("AbleToPlay", i);
                        }
                    }

                    session.saveInBackground();



                    Intent lagerfeuer = new Intent(Liedwahl_Git.this, Lagerfeuer_Lieder.class);


                    lagerfeuer.putExtra("ParseOb", sessionText.getText().toString());
                    Log.d("Parse", sessionText.getText().toString());
                    startActivity(lagerfeuer);


                }


            }
        });







    }

    private class createListTask extends AsyncTask<Void,Void,Integer>{

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(Liedwahl_Git.this);
            dialog.setMessage("Please wait, while loading!");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();


        }


        @Override
        protected Integer doInBackground(Void... arg0) {

            list = new ArrayList<ParseObject>();
            ParseQuery query = new ParseQuery("SongDatenbank");
            details = new ArrayList<SongData>();
            query.orderByAscending("IDS");

            query.findInBackground(new FindCallback() {
                SongData songDatas;

                @Override
                public void done(List<ParseObject> objects,
                        com.parse.ParseException e) {
                    if (e == null) {
                        Log.d("Parse", "Objektliste empfangen!");
                        list = (ArrayList<ParseObject>) objects;
                        int j = 0;
                        for (int i = 0; i < list.size(); i++) {
                            String artist = list.get(i).getString("artist");
                            String title = list.get(i).getString("title");
                            String duration = list.get(i).getString("duration");
                            String imageUrl = list.get(i).getString("imageUrl");
                            String anzahlObjekte = String.valueOf(j);
                            Log.d("Parse", "Artist: " + artist);
                            Log.d("Parse", "Title: " + title);
                            Log.d("Parse", "Duration: " + duration);
                            Log.d("Parse", "URL: " + imageUrl);
                            Log.d("Parse", "AnzahlObjekte: " + anzahlObjekte);
                            j++;

                            // /////////////////////////////////////////////////////////////////////////////////////////////
                            // /String in parse.com darf nicht länger als 120
                            // Zeichen sein, sonst NullPointerException!!!//
                            // ////////////////////////////////////////////////////////////////////////////////////////////

                            songDatas = new SongData(title, artist, duration,
                                    imageUrl);
                            details.add(songDatas);

                        }

                    } else {
                        Log.d("Parse", "Objektliste empfangen gescheitert!");

                    }
                    adapter = new SongDataAdapter(details, Liedwahl_Git.this);
                    songListView = (ListView)findViewById(R.id.listeCheck);
                    songListView.setAdapter(adapter);

                    songListView.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {

                            Log.d("Parse", "Item"+position+"geklickt");

                            songListView.setItemChecked(position, true);

                            if (ableToPlay[position]==true) {
                                ableToPlay[position]=false;
                                Log.d("Parse", position + "unchecked");
                            }else {
                                ableToPlay[position]=true;
                                Log.d("Parse", position + "checked");
                            }



                        }

                    });

                }


            });

            return 1;
        }


        protected void onPostExecute(Integer result){
            ok.setVisibility(View.VISIBLE);
            setupOnClickListeners();
            dialog.dismiss();

        }




    }


}

这里我试图接收该数组的活动:

package de.lichtenberger.gottschalk.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;



public class Lagerfeuer_Lieder extends Activity {

    private ImageButton navLyrics;
    private ImageButton navTune;
    private ImageButton navMusic;
    private ImageButton navChords;
    private ImageView guitar;
    private Button test;
    private String sessionName;
    private ParseObject songList;
    private String ableToPlay;

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

        Parse.initialize(this, "hGxasGU6e0WQAOh5JIOGDfvFBKrYyBJKXIzxBfAG", "WsOPsXerpsFjsjekKKbZnnjAHvXy5PQHVQEB8Cqu");


        setupUI();
        setupOnClickListeners();

        getSongList();

    }

    private void getSongList() {
        sessionName = getIntent().getExtras().getString("ParseOb");

        ParseQuery pq = new ParseQuery("Sessions");
        pq.whereEqualTo("Name", sessionName);

        pq.getFirstInBackground(new GetCallback() {

            @Override
            public void done(ParseObject object, com.parse.ParseException e) {

                songList = object;
                ableToPlay = songList.getString("AbleToPlay");
            }
        });

        Log.d("Parse", songList.get("AbleToPlay").toString());



    }

    private void setupOnClickListeners() {

        navLyrics.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToLyrics = new Intent(Lagerfeuer_Lieder.this, Lyrics.class);
                navToLyrics.putExtra("BackButton", R.string.main);
                startActivity(navToLyrics);



            }
        });

        navTune.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToTune = new Intent(Lagerfeuer_Lieder.this, Tune.class);
                startActivity(navToTune);



            }
        });

        navMusic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToMusic = new Intent(Lagerfeuer_Lieder.this, Music.class);
                startActivity(navToMusic);



            }
        });

        navChords.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToChords = new Intent(Lagerfeuer_Lieder.this, Chords.class);
                startActivity(navToChords);



            }
        });



    }

    private void setupUI() {

        navLyrics = (ImageButton)findViewById(R.id.navlyrics);
        navTune = (ImageButton)findViewById(R.id.navtune);
        navMusic = (ImageButton)findViewById(R.id.navmusic);
        navChords = (ImageButton)findViewById(R.id.navchords);
        guitar = (ImageView)findViewById(R.id.guitar);

        Animation animation = new TranslateAnimation(0, 500,0, 0);
        animation.setDuration(1000);
        animation.setFillAfter(true);
        guitar.startAnimation(animation);
        guitar.setVisibility(0);
        Log.d("Parse", getIntent().getExtras().getString("ParseOb"));


    }

    private void animation() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        String objID = getIntent().getExtras().getString("ParseOb");
        Log.d("Parse", objID);
        ParseQuery pq = new ParseQuery("Sessions");
        pq.getInBackground(objID, new GetCallback() {

            @Override
            public void done(ParseObject arg0, com.parse.ParseException arg1) {
                ParseObject toDest = arg0;
                try {
                    toDest.delete();
                } catch (com.parse.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            });

    }

}

在这里你可以看到eclipse实际上得到了具有数组值的Object:

Eclipse

希望有人知道如何解决这个问题。 先谢谢你!

1 个答案:

答案 0 :(得分:1)

问题出在下面的代码中

pq.getFirstInBackground(new GetCallback() {

            @Override
            public void done(ParseObject object, com.parse.ParseException e) {

                songList = object;
                ableToPlay = songList.getString("AbleToPlay");
            }
        });

请检查/调试使用getFirstInBackground方法调用接口GatcallBack的方法done。因此,如果将ParseObject作为null返回,那么获取NullPointerException的可能性就会增加。

这里的调试可以帮到你很多