Android如果否则无法在ListActivity中工作

时间:2013-12-10 20:15:34

标签: android android-intent if-statement

好的我有一个ListActivity,在listItem中我必须检查它是否有效。

这是我的代码:

public class mainActivity extends ListActivity{

    Intent openActivityMainMenu;
    tourDB getArivalDate;
    long getlastID;
    String getAD;
    String Check;
    public static boolean checkPro = false;
    private static final String mainMenuList[] = {"Create New","Now Going Tour","My Trip History","Famous Tourist Attractions","Alarm Settings","About Us"};
    @Override
    protected void onCreate(Bundle teamblackCatMainPage) {

        super.onCreate(teamblackCatMainPage);

        try{
            getArivalDate = new tourDB(mainActivity.this);
        }catch(SQLException e){
            e.printStackTrace();
        }

        getArivalDate.open();



        setListAdapter(new MenuArrayAdapter(this, mainMenuList));
    } 

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);

        String positionItemMainMenu = (String) getListAdapter().getItem(position);

        if (positionItemMainMenu.contentEquals("Create New")) {

            //checkPro = createNewActivity_2.openPro;

            //if (checkPro == false) {

                openActivityMainMenu = new Intent("com.blackcat.blackcattourorganizer.CREATENEWACTIVITY_1");
                startActivity(openActivityMainMenu);


            //}else {

                //Toast.makeText(getApplicationContext(), "One Profile Is Running", Toast.LENGTH_LONG).show();
            //}
        }else if (positionItemMainMenu.contentEquals("Now Going Tour")) {

            getArivalDate.open();
            getlastID = getArivalDate.getMaxID();
            getAD = getArivalDate.getArivaldateforCheck(getlastID);
            getArivalDate.close();
            Check = getAD;

            if (getAD.contentEquals(Check) || getAD.contentEquals(null)){

                openActivityMainMenu = new Intent("com.blackcat.blackcattourorganizer.ONGOINGACTIVITY");
                startActivity(openActivityMainMenu);    

            }else{

                Toast.makeText(getApplicationContext(), "Create profile First", Toast.LENGTH_SHORT).show();

            }

        }else if (positionItemMainMenu.contentEquals("My Trip History")) {

            openActivityMainMenu = new Intent("com.blackcat.blackcattourorganizer.TOURLISTACTIVITY");
            startActivity(openActivityMainMenu);

        }else if (positionItemMainMenu.contentEquals("Famous Tourist Attractions")) {

            openActivityMainMenu = new Intent("com.blackcat.blackcattourorganizer.FAMOUSTOURACTIVITY");
            startActivity(openActivityMainMenu);

        }else if (positionItemMainMenu.contentEquals("Alarm Settings")) {

            openActivityMainMenu = new Intent("com.blackcat.blackcattourorganizer.ALARMSETACTIVITY");
            startActivity(openActivityMainMenu);

        }else if (positionItemMainMenu.contentEquals("About Us")) {

            openActivityMainMenu = new Intent("com.blackcat.blackcattourorganizer.ABOUTUSACTIVITY");
            startActivity(openActivityMainMenu);
        }
    }
}

这是logcat。我做错了什么?

12-10 14:00:27.542: E/AndroidRuntime(2387): FATAL EXCEPTION: main
12-10 14:00:27.542: E/AndroidRuntime(2387): java.lang.NullPointerException
12-10 14:00:27.542: E/AndroidRuntime(2387):     at java.lang.String.contentEquals(String.java:1701)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at com.blackcat.blackcattourorganizer.mainActivity.onListItemClick(mainActivity.java:74)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.os.Handler.handleCallback(Handler.java:725)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.os.Looper.loop(Looper.java:137)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at java.lang.reflect.Method.invoke(Method.java:511)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-10 14:00:27.542: E/AndroidRuntime(2387):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

如果没有更多代码,我猜你的MenuArrayAdapter没有正确实现getItem方法。

检查适配器处理getItem(int position)请求的方式以及为什么要将null作为答案。

答案 1 :(得分:0)

positionItemMainMenu显然为null。

可能MenuArrayAdapter没有将数组mainMenuList设置为属性项。 或者你可以覆盖getItem方法。