尝试将项目放入微调器时出错 - Android

时间:2014-02-24 19:10:21

标签: android sqlite error-handling spinner android-spinner

我目前正在尝试从SQLite数据库中添加项目并将它们放入微调器中。下面显示了数据库和主要活动的代码,如果还有其他需要添加的话,请说明。我也发现了错误。

错误:

02-24 14:01:59.222  23660-23660/com.example.bank_app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.bank_app.main_menu_activity.onOptionsItemSelected(main_menu_activity.java:149)
        at android.app.Activity.onMenuItemSelected(Activity.java:2640)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
        at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
        at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
        at android.view.View.performClick(View.java:4489)
        at android.view.View$PerformClick.run(View.java:18803)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5455)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)

主要活动代码:

List<String> SpinnerArray = db.getAccountsNames();

// Outputs items in SpinnerArray to logcat
/*for (int j = 0; j < SpinnerArray.size(); j++)
{
    Log.d("Output", j + ": " + SpinnerArray.get(j).toString());
}*/

ArrayAdapter spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, SpinnerArray);  
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
Spinner accountSpinner = (Spinner)findViewById(R.id.spinner1);
accountSpinner.setAdapter(spinnerAdapter);

数据库处理程序:

// Getting All Account Names return ArrayList<Account)
public ArrayList<String> getAccountsNames()
{
    ArrayList<String> accountNameList = new ArrayList<String>();

    String selectQry = "SELECT * FROM " + Table_Account;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQry, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst())
    {
        do
        {
            //Account account = new Account();
            //account.setAccountName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));

            // Add to account list
            accountNameList.add(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
        }while(cursor.moveToNext());
    }
    db.close();
    return accountNameList;
}

我知道SpinnerArray中有信息,因为这些项目打印到logcat,但我的错误发生在以下代码的某处:

ArrayAdapter spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, SpinnerArray);  
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
Spinner accountSpinner = (Spinner)findViewById(R.id.spinner1);
accountSpinner.setAdapter(spinnerAdapter);

感谢您的帮助!

XML - (这是自定义警报对话框的xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >


    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Select account to delete: "
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_weight="1" />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Delete" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />

</LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

如您所说,标识为spinner1的微调器位于自定义警报对话框中。但是,活动中的findViewById()会尝试从活动视图层次结构中找到它。您的对话框不是活动层次结构的一部分。您必须搜索对话框层次结构。

例如,更改

Spinner accountSpinner = (Spinner)findViewById(R.id.spinner1);

Spinner accountSpinner = (Spinner)dialog.findViewById(R.id.spinner1);

其中dialog是您正在显示的对话框。

(你并没有真正展示你如何膨胀对话框,所以可能有更好的方法。)

答案 1 :(得分:0)

试试这个

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);                    
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner accountSpinner = (Spinner) findViewById(R.id.spinner1);
accountSpinner.setAdapter(dataAdapter);       

答案 2 :(得分:0)

由于拉尔托的帮助,问题的答案如图所示!

        case R.id.action_delete_account:
        {
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.delete_account);
            dialog.setTitle("Delete Account");
            dialog.setCancelable(true);

            spn = (Spinner)dialog.findViewById(R.id.spinner1);
            List<String> SpinnerArray = db.getAccountsNames();
            /*for (int j = 0; j < SpinnerArray.size(); j++)
            {
                Log.d("Output", j + ": " + SpinnerArray.get(j).toString());
            }*/

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item, SpinnerArray);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //Log.d("Output", Integer.toString(dataAdapter.getCount()));
            spn.setAdapter(dataAdapter);

            //TextView title = (TextView) dialog.findViewById(R.id.titleTextView);
            btnDelete = (Button) dialog.findViewById(R.id.btnDelete);
            btnCancel = (Button) dialog.findViewById(R.id.btnCancel);

            btnDelete.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    displayListView();
                }
            });
            btnCancel.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    dialog.cancel();
                }
            });

            dialog.show();
        }
        default:
        {
            return super.onOptionsItemSelected(item);
        }
    }