【问题标题】:Nullpointerexception: attempt to invoke interface methodNullpointerexception:尝试调用接口方法
【发布时间】:2016-01-17 11:33:59
【问题描述】:

我有一个列表视图,当在列表项上滑动时会给出包含按钮的菜单(我使用了swipe menu ListView 库来实现它)。现在,当滑动列表项并在其中单击一个按钮时,我希望将列表项添加到存档中并显示在另一个活动中(为此,我正在使用 sharedpreference)。添加到 sharedpreference 并显示到其他活动中可以正常工作。但是,当在显示档案列表的档案活动中,我想在按下滑动菜单中的按钮时删除列表项。在这里应用程序崩溃了

显示添加档案的我的活动

 public class ProActivity extends Activity
  {

 SwipeMenuListView archiveseList;
     ArchivePreferences archvprefernces;
     List<CodeList> archives;
     SharedPreference shrdprefernces;
     FinalAdapter fnladpter;
     List <CodeList> codelist = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pro_layout);

    archvprefernces = new ArchivePreferences();
    shrdprefernces = new SharedPreference();

    archives = archvprefernces.getArchives(ProActivity.this);
codelist = new ArrayList<CodeList>();

    if (archives == null) {
        showAlert(getResources().getString(R.string.nofav),
                  getResources().getString(R.string.nofavmsg));
    } else {

        if (archives.size() == 0) {
            showAlert(
                getResources().getString(R.string.nofav),
                getResources().getString(R.string.nofavmsg));
        }

        archiveseList = (SwipeMenuListView) findViewById(R.id.pro_layoutListView);
        if (archives != null) {

            fnladpter = new FinalAdapter(ProActivity.this, archives);
            archiveseList.setAdapter(fnladpter);


            SwipeMenuCreator creator = new SwipeMenuCreator() {

                @Override
                public void create(SwipeMenu menu) {
                    // create "open" item
                    SwipeMenuItem openItem = new SwipeMenuItem(ProActivity.this);
                    // set item background
                    openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                                                                       0xCE)));
                    // set item width
                    openItem.setWidth(dp2px(90));
                    // set item title
                    openItem.setTitle("Open");
                    // set item title fontsize
                    openItem.setTitleSize(18);
                    // set item title font color
                    openItem.setTitleColor(Color.WHITE);
                    // add to menu
                    menu.addMenuItem(openItem);

                    // create "delete" item
                    SwipeMenuItem favoriteItem = new SwipeMenuItem(ProActivity.this);
                    // set item background
                    favoriteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                                                                           0x3F, 0x25)));
                    // set item width
                    favoriteItem.setWidth(dp2px(90));
                    // set a icon
                    favoriteItem.setIcon(R.drawable.unfavorite);

                    // add to menu
                    menu.addMenuItem(favoriteItem);

                    SwipeMenuItem info = new SwipeMenuItem(ProActivity.this);

                    info.setBackground(new ColorDrawable(Color.BLUE));
                    info.setTitle(R.id.beg_list_itemTextView);
                    //set item width
                    info.setWidth(dp2px(90));
                    // set a icon
                    info.setIcon(R.drawable.ic_launcher);
                    // add to menu
                    menu.addMenuItem(info);

                }
            };
            // set creator
            archiveseList.setMenuCreator(creator);

            archiveseList.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                        //ApplicationInfo item = mAppList.get(position);
                        switch (index) {
                            case 0:
                                // open

                                break;
                            case 1:
                                // favorite


                                break;

                            case 2 :



                                ImageView alphimg = (ImageView) findViewById(R.id.beg_list_itemImageView);
                                String tag = alphimg.getTag().toString();
                                if (checkArchivedItem(codelist.get(position))) {

                                    archvprefernces.removeArchive(ProActivity.this, codelist.get(position));
                                    alphimg.setTag("noarchive");
                                    codelist.add(codelist.get(position));
                                    fnladpter.remove(archives.get(position));
                                    fnladpter.notifyDataSetChanged();
                        Toast.makeText(ProActivity.this, getString(R.string.archive_removed), Toast.LENGTH_SHORT).show();           

                                }else {



                                    archvprefernces.addArchive(ProActivity.this, codelist.get(position));
                                    alphimg.setTag("yesarchive");
                                    codelist.remove(codelist.get(position));

                                    fnladpter.notifyDataSetChanged();

                            }



                                break;


                        }
                        return false;
                    }

                    private boolean checkArchivedItem(CodeList checkcode) {
                        boolean check = false;
                        List<CodeList> archives = archvprefernces.getArchives(ProActivity.this);
                        if (archives != null) {
                            for (CodeList codes : archives) {
                                if (codes.equals(checkcode)) {
                                    check = true;
                                    break;
                                }
                            }
                        }
                        return check;
                    }       

                    private Object getItem(int position)
                    {
                        return codelist.get(position);
                    }





                });

            archiveseList.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

                    @Override
                    public void onSwipeStart(int position) {
                        // swipe start

                    }

                    @Override
                    public void onSwipeEnd(int position) {
                        // swipe end
                    }
                });

            // set MenuStateChangeListener
            archiveseList.setOnMenuStateChangeListener(new SwipeMenuListView.OnMenuStateChangeListener() {
                    @Override
                    public void onMenuOpen(int position) {
                    }

                    @Override
                    public void onMenuClose(int position) {
                    }
                });

            archiveseList.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);


            archiveseList.setOnItemClickListener(new OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View arg1,
                                            int position, long arg3) {




                    }
                });



public void showAlert(String title, String message) {
    if (ProActivity.this != null && !ProActivity.this.isFinishing()) {
        AlertDialog alertDialog = new AlertDialog.Builder(ProActivity.this)
            .create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.setCancelable(false);

        // setting OK Button
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    // activity.finish();
                    getFragmentManager().popBackStackImmediate();
                }
            });
        alertDialog.show();
    }


}

private int dp2px(int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                                           getResources().getDisplayMetrics());
}

@Override
public void onBackPressed() {
    super.onBackPressed();

    overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);

}

}

我的日志猫

01-17 16:38:26.033 12088 12088 D   AndroidRuntime                               Shutting down VM
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               FATAL EXCEPTION: main
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               Process: com.enlightenme.pac, PID: 12088
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at java.util.ArrayList.get(ArrayList.java:308)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at com.enlightenme.pac.ProActivity$100000001.onMenuItemClick(ProActivity.java:209)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at com.baoyz.swipemenulistview.SwipeMenuListView$100000000.onItemClick(SwipeMenuListView.java:78)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at com.baoyz.swipemenulistview.SwipeMenuView.onClick(SwipeMenuView.java:85)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at android.view.View.performClick(View.java:4780)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at android.view.View$PerformClick.run(View.java:19866)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at android.os.Handler.handleCallback(Handler.java:739)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:95)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:135)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:5254)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Method.java:372)
01-17 16:38:26.051 12088 12088 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

【问题讨论】:

    标签: android listview sharedpreferences


    【解决方案1】:

    问题似乎在这个if condition 在你的onMenuItemClick()

    if (checkArchivedItem(codelist.get(position)))
    

    您只是初始化了您的代码列表,但从未在此处设置任何数据,因为您在此处设置为存档列表

    archives = archvprefernces.getArchives(ProActivity.this);
    codelist = new ArrayList<CodeList>();
    

    因此,您的代码列表的大小始终为零,因此它在if condition 中为您提供IndexOutOfBoundsException,我在上面提到当位置值大于0时,对于您的情况,它是2对于当前案例。

    因此,将数据设置到您的代码列表并检查您的代码中onMenuItemClick() 返回的位置不应大于您的codelist.size() 可以帮助您解决任何IndexOutOfBoundsException

    希望现在对您有所帮助。

    【讨论】:

    • 是的,完全有道理改变了这样的代码 codelist = new ArrayList(archives);,谢谢兄弟。唷
    【解决方案2】:

    索引2无效,大小为0

    在您的 onMenuItemClick 某处,您尝试从数组列表中获取对象,但数组列表为空。注销档案和代码列表的大小,以确保在尝试添加/删除项目之前,其中确实有对象。

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 2020-02-17
      相关资源
      最近更新 更多