本に続いて 2 セットのコードを書きました。いくつかのチェックボックスを含むダイアログを表示するだけで、機能的には似ていますが、プログラム的にはわずかに異なります。1 つ目はエミュレータで正常に動作し、2 つ目は「source not found in JAR android.jar」と表示されます
作品:
public class DialogActivity2 extends Activity {
CharSequence[] items = { "Google", "Apple", "Microsoft" };
boolean[] itemsChecked = new boolean [items.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
showDialog(0);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("This is a dialog with some simple text...")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getBaseContext(),
"Cancel clicked!", Toast.LENGTH_SHORT).show();
}
}
)
.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int wich, boolean isChecked) {
Toast.makeText(getBaseContext(), items[wich] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show();
}
}
).create();
}
return null;
}
@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_main, menu);
return true;
}
}
「ソースが見つかりません」を生成します。
public class DialogActivity1 extends Activity {
CharSequence[] items = { "Goole", "Apple", "Microsoft" };
boolean[] itemsChecked = new boolean[items.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_activity1);
}
public void onClick(View v) {
showDialog(0);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Dialog with some text...");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int wichButton) {
Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();
}
}
);
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
}
}
);
builder.setMultiChoiceItems(items, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int wich, boolean isChecked) {
Toast.makeText(getBaseContext(),
items[wich] + (isChecked ? " checked" : " unchecked"), Toast.LENGTH_SHORT).show();
}
}
);
return builder.create();
}
return null;
}
@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_dialog_activity1, menu);
return true;
}
}