1

我在一本书之后写了两组代码。它只是显示一个带有一些复选框的对话框,它们在功能方面是相似的,但它们在编程上略有不同。第一个在模拟器中运行良好,第二个说“在 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;
 }

}
4

0 回答 0