2

问题是,当我单击自定义微调器自定义对话框时,会出现列表视图单选按钮和底部按钮,但默认情况下没有选择任何项目,即使我选择任何选择应用程序也会因为 SpinnerDialog 中的 nullpointerexception 而崩溃 - > onItemClick -> onItemSelectedListener.onItemSelected(text)
我从这里获取示例代码并使用 alertdialog 而不是对话框

我的客户微调班

public class SpinnerCustomDialog extends Spinner implements DialogInterface.OnClickListener  {
protected OnAddListener mListener = null;

public SpinnerCustomDialog(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}


public SpinnerCustomDialog(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public SpinnerCustomDialog(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public interface OnAddListener {
    void onAdd(Spinner inSpinner);
}


@Override
public boolean performClick() {
    //boolean handled = super.performClick(); //TODO how to avoid this skip ?

    boolean handled = false;
    if (!handled) {
        handled = true;
        Context context = getContext();

        Log.w(SpinnerCustomDialog.class.getName(), "checlk1");
        AccountDS datasource;
        datasource = new AccountDS(context);
        datasource.open();
        List<Accounts> values = datasource.getAllAccountList();
        Log.w(BalanceActivity.class.getName(), "List Size " + values.size());
        datasource.close();

        ArrayAdapter<Accounts> adapter = new ArrayAdapter<Accounts>(context, android.R.layout.simple_list_item_single_choice, values);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //SpinnerDialog lDialog = new SpinnerDialog(context, getSelectedItemPosition(), adapter, this);
        SpinnerDialog lDialog = new SpinnerDialog(context, 1, adapter, this);
        lDialog.setTitle("select category");
        lDialog.show();
    }

    return handled;
}

@Override
public void onClick(DialogInterface dialog, int which) 
{
    Log.w(SpinnerCustomDialog.class.getName(), "onClick");
    setSelection(which);        
    dialog.dismiss();
}
}

我的客户微调对话框代码

public class SpinnerDialog extends AlertDialog implements OnItemClickListener, View.OnClickListener {

DialogInterface.OnClickListener mListener;
View promptsView;
OnItemSelectedListener onItemSelectedListener;
public Context mContext; 

public interface OnItemSelectedListener
{
    public void onItemSelected(String itemValue);       
}

protected SpinnerDialog(Context context, int inPosition, ListAdapter inSpinnerAdapter, DialogInterface.OnClickListener inListener) {
    super(context);
    // TODO Auto-generated constructor stub
    //this.setContentView(com.abdulwasie.mybalance.R.layout.activity_spinner_dialog);
    promptsView = getLayoutInflater().inflate(R.layout.activity_spinner_dialog, null);
    this.setView(promptsView);
    mListener = inListener;
    mContext = context;
    Log.w(SpinnerDialog.class.getName(), "inside spinner dialog 1");        
    ListView lList = (ListView)promptsView.findViewById(R.id.listCategory);



    lList.setAdapter(inSpinnerAdapter);
    lList.setOnItemClickListener(this);
    lList.setChoiceMode(ListView.CHOICE_MODE_SINGLE );
    lList.setSelection(1);

    Log.w(SpinnerDialog.class.getName(), "inside spinner dialog 2");
    setButton(DialogInterface.BUTTON_POSITIVE, "Positive",
            new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // This is replaced later
                    Log.w(SpinnerDialog.class.getName(), "button clicked");

                }
            });
    Log.w(SpinnerDialog.class.getName(), "inside spinner dialog 3");

}


@Override
public void onClick(View v) 
{
    if(mListener != null)
        mListener.onClick(this, DialogInterface.BUTTON_POSITIVE);           
}


@Override
public void onItemClick(AdapterView parent, View view, int position, long id) 
{
    if(mListener != null)
        mListener.onClick(SpinnerDialog.this, position);
    String text = parent.getItemAtPosition(position).toString();
    Log.w(SpinnerDialog.class.getName(), "onItemClick " + text);
    onItemSelectedListener.onItemSelected(text);        
}   
}

和我的活动课

public class RegisterActivity extends Activity  {
static final int DATE_DIALOG_ID = 1;
private int mYear;
private int mMonth;
private int mDay;
private EditText pickDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    pickDate = (EditText) findViewById(R.id.pickdate);

    pickDate.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                showDialog(DATE_DIALOG_ID);
            }
            return false;
        }
    });

    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    updateDate();


    //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    //String currentDateandTime = sdf.format(new Date());

    //pickDate.setText(mDay + "/" + mMonth + "/" + mYear);
    //pickDate.setText(currentDateandTime);


    AccountDS datasource;
    datasource = new AccountDS(this);
    datasource.open();

    List<Accounts> values = datasource.getAllAccountList();
    Log.w(BalanceActivity.class.getName(), "List Size " + values.size());

    datasource.close();

    ArrayAdapter<Accounts> adapter = new ArrayAdapter<Accounts>(this, android.R.layout.simple_list_item_1, values);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    final Spinner my_spin=(Spinner)findViewById(R.id.spinner1);
    if (my_spin == null) {
        Log.w(BalanceActivity.class.getName(), "my spin is null ");
    }

    my_spin.setAdapter(adapter);


    SpinnerCustomDialog my_spin1 = (SpinnerCustomDialog)findViewById(R.id.spinner2);
    my_spin1.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
        {
            Log.w(RegisterActivity.class.getName(), "onItemSelected");

        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) 
        {

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_register, menu);
    return true;
}

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        Log.w(BalanceActivity.class.getName(), mDay + " - " + mMonth + " - " + mYear );
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);
    }
    return null;
}

protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
    case DATE_DIALOG_ID:

        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
        break;
    }
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = view.getMonth();
        mDay = dayOfMonth;
        Log.w(BalanceActivity.class.getName(), "onDateSet: " + mDay + " - " + mMonth + " - " + mYear );
        updateDate();
    }
};

private void updateDate() {
    try {


        pickDate.setText(
                new StringBuilder()
                // Month is 0 based so add 1
                .append(mDay).append("/")
                .append(mMonth + 1).append("/")
                .append(mYear));
    } catch (Exception ex) {

    }

}

}

和我的活动布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="70dp"
tools:context=".RegisterActivity" >

<EditText
    android:id="@+id/pickdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:cursorVisible="false"
    android:editable="false"
    android:ems="7"
    android:inputType="none"
    android:singleLine="true" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/pickdate"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/pickdate"
    android:layout_toRightOf="@+id/pickdate"
    android:prompt="@+string/selectAccount"
    android:ems="5" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/pickdate"
    android:ems="10" >
</EditText>

<com.abdulwasie.mybalance.SpinnerCustomDialog
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText2"
    android:layout_toLeftOf="@+id/spinner1"
    android:prompt="@+string/hello_world" />

</RelativeLayout>

日志 :

12-08 09:11:04.278: E/AndroidRuntime(10993): FATAL EXCEPTION: main
12-08 09:11:04.278: E/AndroidRuntime(10993): java.lang.NullPointerException
12-08 09:11:04.278: E/AndroidRuntime(10993):    at com.abdulwasie.mybalance.SpinnerDialog.onItemClick(SpinnerDialog.java:74)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at android.widget.ListView.performItemClick(ListView.java:3569)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at android.os.Handler.handleCallback(Handler.java:587)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at android.os.Looper.loop(Looper.java:150)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at android.app.ActivityThread.main(ActivityThread.java:4385)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at java.lang.reflect.Method.invokeNative(Native Method)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at java.lang.reflect.Method.invoke(Method.java:507)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12-08 09:11:04.278: E/AndroidRuntime(10993):    at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0