using parse , testing on emulator and getting null pointer exception ,


我认为该餐未与适配器连接,这就是为什么我无法访问它并且它返回null的原因?任何人都可以通过代码帮助我,并进行更正或告知问题所在。谢谢

public class MealListActivity extends ListActivity {

    private ParseQueryAdapter<Meal> mainAdapter;
    private FavoriteMealAdapter favoritesAdapter;
    private Meal meal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getListView().setClickable(true);

        mainAdapter = new ParseQueryAdapter<Meal>(this, Meal.class);
        mainAdapter.setTextKey("title");
        mainAdapter.setImageKey("photo");

        getListView().setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {

                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent,
                            View view, int position, long id) {
                        mainAdapter.getItemId(position);
                        ChangeRating(meal);
                        return false;
                    }

                });

        // Subclass of ParseQueryAdapter
        favoritesAdapter = new FavoriteMealAdapter(this);

        // Default view is all meals
        setListAdapter(mainAdapter);

    }

    protected void ChangeRating(Meal meal) {
        String x = meal.getRating();
        if(x.isEmpty()){
            meal.setRating("5");
            updateMealList();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_meal_list, menu);
        return true;
    }

    private void updateMealList() {
        mainAdapter.loadObjects();
        setListAdapter(mainAdapter);
    }


我试图在解析中在Meal类中设置一餐的等级,就像在代码中一样,检查等级单元是否为空,然后在该单元格中插入等级,但logcat显示if(x = meal .. .... isEmpty)空指针异常

    01-28 12:21:30.810: E/AndroidRuntime(1470):     at com.parse.mealspotting.MealListActivity.ChangeRating(MealListActivity.java:52)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at com.parse.mealspotting.MealListActivity$1.onItemLongClick(MealListActivity.java:37)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at android.widget.AbsListView.performLongPress(AbsListView.java:2580)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2530)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at android.os.Handler.handleCallback(Handler.java:605)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at android.os.Looper.loop(Looper.java:137)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at java.lang.reflect.Method.invokeNative(Native Method)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at java.lang.reflect.Method.invoke(Method.java:511)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-28 12:21:30.810: E/AndroidRuntime(1470):     at dalvik.system.NativeStart.main(Native Method)


编辑

   @Override
                    public boolean onItemLongClick(AdapterView<?> parent,
                            View view, int position, long id) {
                        // mainAdapter.getItem(position);
                        meal = mainAdapter.getItem(position);
                        if (meal.getRating().isEmpty()) {
                            meal.setRating("5");
                            updateMealList();
                        }
                        return false;
                    }


我尝试过,但仍然遇到同样的问题。

最佳答案

在调用ChangeRating之前,您尚未将膳食参考设置为任何Meal对象,并且显然为null。

应该是

meal=this.getListAdapter().getItem(position);

10-05 23:36