小编Chr*_*ium的帖子

我的java代码有一个明显的错误.为什么要编译并运行?

public class HelloWorld {
    public static void main (String args[]){
        System.out.println ("Hello ");
        http://www.google.com
        System.out.println ("World!");
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码只是编译和执行正常.为什么编译器没有报告任何错误?

java syntax

38
推荐指数
2
解决办法
1249
查看次数

按下"返回"按钮时提示用户

询问用户的哪个好地方,当他点击后退按钮时是否要退出应用程序?我考虑了onPauseonStop,但是只要应用程序在其他应用程序之后消失,这些方法就会触发.

更新:应用程序还应询问用户是否尝试从按钮(在应用程序本身)中退出应用程序,而不是后退硬键.

android android-activity

13
推荐指数
2
解决办法
7744
查看次数

将SimpleAdapter与Spinner一起使用

我是Android开发的新手.我试图通过使用SimpleAdapter填充一个微调器.但是微调器的列表显示空白元素.当我单击任何元素时,其文本在Toast中正确显示.请告诉我下面的代码中有什么问题.

 public void onCreate(Bundle savedInstanceState) {

  private List<Map<String, String>> data = new ArrayList<Map<String, String>>();

  String[] from = new String[] { "colorsData" };
  int[] to = new int[] { R.id.spinner };

  String[] colors = getResources().getStringArray(R.array.colorsData);

  for (int i = 0; i < colors.length; i++) {
   data.add(addData(colors[i]));
  }

  Spinner spinner = (Spinner) findViewById(R.id.spinner);

  SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, android.R.layout.simple_spinner_item, from, to);
  simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(simpleAdapter);

  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    Toast.makeText(
      parent.getContext(),
      "Selected …
Run Code Online (Sandbox Code Playgroud)

android spinner simpleadapter

7
推荐指数
1
解决办法
1万
查看次数

成员应该在类或构造函数中初始化吗?

初始化变量,特别是类级别的对象引用是一个好习惯吗?请考虑以下示例;

public class MyClass {

    private static MyObject myObject;

    public static void main(String[] args) {

        myObject = new MyObject();
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

public class MyClass {

    private MyObject myObject = new MyObject();

    public static void main(String[] args) {

          // Other code
    }
}
Run Code Online (Sandbox Code Playgroud)

哪种方式最好?请指导我们两者的利弊.

问候.

java

7
推荐指数
1
解决办法
599
查看次数

各种Android模拟器

我的SDK中有默认(随附SDK)和三星Galaxy Tab模拟器.我想知道其他第三方(摩托罗拉,LG,夏普等)模拟器是否可用,以便我可以测试我的应用程序以获取更多设备.请提供带有链接的名称.

android emulation android-emulator

6
推荐指数
1
解决办法
1594
查看次数

显示Toast消息

我正在开发一个基于测验的应用程序,当用户点击任何选项时,它应该是一个合适的消息,例如"你的ans是正确的","你的ans是错的".我想要的是

 1. how to display that sort of messages ?or is it the only way to display such msgs ?
 2. If the user clicks wrong option the it should show the correct answer as well as the msg

here is what i have done so far but its not working giving force close!!!


public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        switch (v.getId()) 
        {
        case R.id.button1:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) 
            {
                displayAnswer();

            }
            else
            {
                errorAnswer();

            } …
Run Code Online (Sandbox Code Playgroud)

java android toast

2
推荐指数
1
解决办法
315
查看次数