解析项目单击侦听器null异常

时间:2015-01-28 12:29:16

标签: java android parse-platform null

  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;
                    }

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

1 个答案:

答案 0 :(得分:1)

在调用ChangeRating之前,您没有将膳食引用设置为任何Meal对象,它显然为空。

应该是

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