我正在构建一个警报框,它有一个 char 元素数组,用作多选复选框的数据。我的问题是如何使此警报框根据所选项目顺序将值返回为 1、2、3?IE。如果我选择 mercur 和 venus,我必须将值设为 1,2。我将如何实施?正如您在我尝试使用 for 循环后看到的那样,它现在正在打印选中了哪个复选框。请帮帮我!!!
public class MultiActivity extends Activity {
protected CharSequence[] _options = { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
protected boolean[] _selections = new boolean[ _options.length ];
protected Button _optionsButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_optionsButton = ( Button ) findViewById( R.id.button1);
_optionsButton.setOnClickListener( new ButtonClickHandler() );
}
public class ButtonClickHandler implements View.OnClickListener {
public void onClick( View view ) {
showDialog( 0 );
}
}
@Override
protected Dialog onCreateDialog( int id )
{
return
new AlertDialog.Builder( this )
.setTitle( "Planets" )
.setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() )
.setPositiveButton( "OK", new DialogButtonClickHandler() )
.create();
}
public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
{
public void onClick( DialogInterface dialog, int clicked, boolean selected )
{
Log.i( "ME", _options[ clicked ] + " selected: " + selected );
}
}
public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
public void onClick( DialogInterface dialog, int clicked )
{
switch( clicked )
{
case DialogInterface.BUTTON_POSITIVE:
printSelectedPlanets();
break;
}
}
}
protected void printSelectedPlanets(){
for( int i = 0; i < _options.length; i++ ){
Log.i( "ME", _options[ i ] + " selected: " + _selections[i] );
String abc = _options[ i ] + " selected: " + _selections[i];
TextView ab = (TextView)findViewById(R.id.textView1);
ab.setText(abc);
System.out.print(abc);
}
}
}
当我尝试运行下面提到的代码时,请参阅我的 logcat 中打印了以下错误。
11-21 14:23:59.905: E/AndroidRuntime(439): FATAL EXCEPTION: main
11-21 14:23:59.905: E/AndroidRuntime(439): java.lang.NullPointerException
11-21 14:23:59.905: E/AndroidRuntime(439): at com.workspace.multi.peek$2.onClick(peek.java:47)
11-21 14:23:59.905: E/AndroidRuntime(439): at com.android.internal.app.AlertController$AlertParams$4.onItemClick(AlertController.java:886)
11-21 14:23:59.905: E/AndroidRuntime(439): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-21 14:23:59.905: E/AndroidRuntime(439): at android.widget.ListView.performItemClick(ListView.java:3382)
11-21 14:23:59.905: E/AndroidRuntime(439): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
11-21 14:23:59.905: E/AndroidRuntime(439): at android.os.Handler.handleCallback(Handler.java:587)
11-21 14:23:59.905: E/AndroidRuntime(439): at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 14:23:59.905: E/AndroidRuntime(439): at android.os.Looper.loop(Looper.java:123)
11-21 14:23:59.905: E/AndroidRuntime(439): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-21 14:23:59.905: E/AndroidRuntime(439): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 14:23:59.905: E/AndroidRuntime(439): at java.lang.reflect.Method.invoke(Method.java:521)
11-21 14:23:59.905: E/AndroidRuntime(439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-21 14:23:59.905: E/AndroidRuntime(439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-21 14:23:59.905: E/AndroidRuntime(439): at dalvik.system.NativeStart.main(Native Method)
11-21 14:33:05.145: D/AndroidRuntime(495): Shutting down VM
11-21 14:33:05.145: W/dalvikvm(495): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-21 14:33:05.166: E/AndroidRuntime(495): FATAL EXCEPTION: main
11-21 14:33:05.166: E/AndroidRuntime(495): java.lang.NullPointerException
11-21 14:33:05.166: E/AndroidRuntime(495): at com.workspace.multi.MultiActivity$1.onClick(MultiActivity.java:51)
11-21 14:33:05.166: E/AndroidRuntime(495): at com.android.internal.app.AlertController$AlertParams$4.onItemClick(AlertController.java:886)
11-21 14:33:05.166: E/AndroidRuntime(495): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-21 14:33:05.166: E/AndroidRuntime(495): at android.widget.ListView.performItemClick(ListView.java:3382)
11-21 14:33:05.166: E/AndroidRuntime(495): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
11-21 14:33:05.166: E/AndroidRuntime(495): at android.os.Handler.handleCallback(Handler.java:587)
11-21 14:33:05.166: E/AndroidRuntime(495): at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 14:33:05.166: E/AndroidRuntime(495): at android.os.Looper.loop(Looper.java:123)
11-21 14:33:05.166: E/AndroidRuntime(495): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-21 14:33:05.166: E/AndroidRuntime(495): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 14:33:05.166: E/AndroidRuntime(495): at java.lang.reflect.Method.invoke(Method.java:521)
11-21 14:33:05.166: E/AndroidRuntime(495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-21 14:33:05.166: E/AndroidRuntime(495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-21 14:33:05.166: E/AndroidRuntime(495): at dalvik.system.NativeStart.main(Native Method)