遇到字符串问题

时间:2016-10-12 17:06:03

标签: java android string compiler-errors

我在WINDOWS上

我正在开展一个项目。我已下载android-7.jar并将其重命名为android7.zip并解压缩文件。这工作得很好。现在我有android的类。我主要关注的是android.app.AlertDialog,但是它有很多导入,所以我只是使用整个android源码。我有自己的java文件,com.tylerr147.dialog.showDialog

这是该文件:

//javac com/tylerr147/dialog/showDialog.java
package com.tylerr147.dialog;

import android.app.*;
import android.os.*;

public class showDialog extends Activity {

AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("hi");


public static void main(String[] args) {
}
}

我在main()之外有AlertDialog的原因是因为我不能在静态方法中使用AlertDialog。

除了我得到的一切都很好:

com\tylerr147\dialog\showDialog.java:10: error: <identifier> expected
adb.setTitle("hi");
            ^
com\tylerr147\dialog\showDialog.java:10: error: illegal start of type
adb.setTitle("hi");
             ^
2 errors

我不知道造成这个问题的原因。非常感谢任何帮助。

4 个答案:

答案 0 :(得分:0)

试试这个:

public class showDialog extends Activity {

AlertDialog.Builder adb = new AlertDialog.Builder(this);



public static void main(String[] args) {
    adb.setTitle("hi");
}
}

答案 1 :(得分:0)

由于您使用的是 this 的引用,因此以下代码必须放在实例方法中

AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("hi");

答案 2 :(得分:0)

你可以使用 OnCreate()方法相当于Java类中的 main()方法。 最好在Android Android activity life cycle - what are all these methods for?中查看Android中的活动生命周期方法,然后查看此How do I display an alert dialog on Android?

答案 3 :(得分:0)

你不想要一个带android的main方法。相反,您应该从扩展的Activity类重写onCreate方法并将其作为起点。

@Override
protected void onCreate(Bundle savedInstanceState{
    super.onCreate(savedInstanceState)
    adb = new AlertDialog.Builder(this);
    adb.setTitle("hi");
}
相关问题