我有一些信息存储为SharedPreferences.我需要从外部的Activity(从域模型类中)访问该信息.所以我在Activity中创建了一个静态方法,我只用它来获取共享首选项.
这给了我一些问题,因为显然不可能从静态方法调用方法"getSharedPreferences".
这是eclipse给我的信息:
Cannot make a static reference to the non-static method
getSharedPreferences(String, int) from the type ContextWrapper
Run Code Online (Sandbox Code Playgroud)
我尝试使用Activity实例来解决这个问题,如下所示:
public static SharedPreferences getSharedPreferences () {
Activity act = new Activity();
return act.getSharedPreferences("FILE", 0);
}
Run Code Online (Sandbox Code Playgroud)
此代码提供null点异常.
有解决方法吗?我试图这样做,我会进入一个android-code气味?
提前致谢.
我有一些字符串需要翻译和显示.这些字符串是变量.我在strings.xml文件中有翻译.
我想显示字符串的"翻译版本".例如,在Activity中:
String name = "Water";
TextView nameDisplay = new TextView(this).
nameDisplay.setText(name);
Run Code Online (Sandbox Code Playgroud)
在字符串文件中我有定义
<string name="Water">French word for Water</string>
Run Code Online (Sandbox Code Playgroud)
如果我使用这样的东西:
nameDisplay.setText(R.string.KnownName);
Run Code Online (Sandbox Code Playgroud)
它会奏效.但在我的情况下,名称存储在一个变量中,所以我不知道该怎么做才能使setText方法正常运行.
我目前的解决方法是
String translation = ""
if(name == "Water") {
translation = getString(R.string.Water);
}
else {
...
}
nameDisplay.setText(translation);
Run Code Online (Sandbox Code Playgroud)
......但这不能很好地扩展.
有什么建议?
我应该将翻译版本存储在变量中吗?
当我使用Android模拟器时,我可以执行"adb logcat"来查看源自我的代码的输出消息(log/system.out.println).它还显示了执行期间发生的异常的堆栈跟踪.
但是,当我使用真正的手机时,"adb logcat"不会显示/显示任何内容.
我也尝试了"adb -d logcat",它也没有显示任何内容.
有没有办法让它与真正的手机一起工作?
谢谢.
更新:
我刚试过"adb -s?logcat"('?'是设备的序列号),也没有结果.
我尝试了另一个"adb"命令来查看是否有任何工作:"adb -s?bugreport".这印刷了很多东西.示例:"内存信息","CPU信息"和一些Java特定的东西.所以有些东西正在起作用.
我在onCreate方法和Activity中有一些代码,并注意到它被调用了三次.这是正常的行为吗?谢谢.
我有一个Activity,它包含一个用XML定义的ListView(它不是ListActivity类的子类).
我想在ListView为空时显示一条消息,所以我尝试使用setEmptyView方法:
listView = (ListView) findViewById(R.id.list);
TextView emptyListText = new TextView(this);
// also tried with the following line uncommented
// emptyListText.setId(android.R.id.empty);
emptyListText.setText(getString(R.string.EmptyList));
listView.setEmptyView(emptyListText);
Run Code Online (Sandbox Code Playgroud)
但它没有用.
有任何想法吗?
谢谢.
我正在尝试创建一个涵盖整整一个月的日期范围,即
[开始日期; 结束日期]
因此,我有一个参考日期,并尝试从中创建一个新的日期.我对"endDate"有疑问,因为我希望它接近一天结束时(即23:59:59).
我正在使用的代码如下:
public static Date previousMonthLastDate(Date referenceDate) {
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(referenceDate);
calendar.add(Calendar.MONTH, -1); // move to the previous month
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, lastDay);
// set the time to be the end of the day
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
Run Code Online (Sandbox Code Playgroud)
此代码在Android 模拟器上按预期工作.但是,在真实手机上运行它会给出错误的日期.因此,我认为这是某种时区问题.
在电话上,而不是说2010年8月31日,它给出了2010年9月1日.此值接缝将在将HOUR_OF_DAY设置为23的代码行之后设置.
有任何想法吗?
谢谢.
我正在编写一个功能,将类放入程序的某个包中会很有帮助.另外,我只想要子类化某个类的类.
我需要这些类才能在它们上调用静态方法.
有自动方法吗?如果是这样,它会慢吗?
如果我不清楚,我想要的是这样的:
ArrayList<Class<? extends MySuperClass>> classes = ;
classes.add(MyClass.class);
classes.add(MyClass2.class);
Run Code Online (Sandbox Code Playgroud)
我不想为每个类调用add,而是自动获取该类列表.
类的数量很小,所以如果自动技巧很慢,我不介意手动声明它们 - 这个应用程序适用于移动平台.
无论哪种方式,我还想知道如何为ArrayList中的每个方法调用静态方法:
// error The method nameOfStaticMethod is undefined for the type Class<capture#2-of ? extends MySuperClass>
classes.get(0).nameOfStaticMethod ();
Run Code Online (Sandbox Code Playgroud)
感谢您的意见.
我正在尝试恢复视图的背景颜色.
我有几个可选择的视图.当用户单击其中一个视图时,将执行以下代码并且视图变为黄色:
View newSelection, previousSelection;
...
if(previousSelection != null) {
previousSelection.setBackgroundColor(Color.BLACK); // problem here
}
newSelection.setBackgroundColor(Color.YELLOW);
Run Code Online (Sandbox Code Playgroud)
但是,我想重置以前选择的视图的颜色.但是,我不知道它是哪种颜色(我在上面的代码中将它设置为Color.BLACK).我无法在View类中找到getBackgroundColor或类似的方法.如果我拥有它,我可以保存以前的颜色,并在选择新视图时将其放回原处.
我在TableRow中有一个TextView.此TextView中包含的文本比屏幕大.因此,我希望将文本包装成多行.
TableLayout table = (TableLayout)findViewById(R.id.myTable);
TableRow row = new Tablerow(context);
TextView view = new TextView(context);
view.setText(stringWithLotsOfCharacters);
view.setSingleLine(false);
row.add(view);
table.addview(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)
我尝试使用"setSingleLine",但它没有我想要的效果.
有什么建议?谢谢.
我正在尝试为我的 kotlin 代码生成文档。我的一些类和函数有 KDOC 注释。
我试图像这样从 CLI 运行 dokka:
java -jar dokka-cli-1.4.20.jar
结果是:
警告:Dokka 1.4.* 是一个 alpha 项目进度:初始化插件加载插件:[] 加载:[
] 抑制:[
] PROGRESS:有效性检查 PROGRESS:创建文档模型 PROGRESS:退出生成:没有任何记录 PROGRESS:
=== 时间测量 ===
Run Code Online (Sandbox Code Playgroud)Initializing plugins: 26 Validity check: 32 Creating documentation models: 28 Exiting Generation: Nothing to document: 1
我试图执行这个:
所有运行的结果都相似。
关于如何解决这个问题的任何想法?谢谢。
我想创建这样的东西,其中每一行都被"管道"字符包围.
| First Line |
| 100 200 |
| 1000 2000 |
Run Code Online (Sandbox Code Playgroud)
我试图通过printf +格式化(并没有明确计算填充数),但我在格式化语法方面遇到了一些麻烦.这是我目前的代码:
System.out.printf("| FIRST LINE" + "%50s\n", "|");
System.out.printf("| 100 200" + "%50s", "|");
System.out.printf("| 1000 2000" + "%50s", "|");
Run Code Online (Sandbox Code Playgroud)
我试图指出每行的最大值是50个字符,是行中第一个字符"管道",而行中的最后一个字符是另一个"管道").
问题是50个空格的放置没有考虑到左侧部分已经使用的字符(即"|第一行").上面的代码类似于:
System.out.format("%s %50s\n", "| FIRST LINE", "|");
Run Code Online (Sandbox Code Playgroud)
那么,我如何定义输出格式,以便考虑宽度两个字符串?
提前致谢.
我正在尝试使用malloc和realloc,并针对以下问题提出了一些代码:
我想创建一个未知大小的字符串,不设置任何限制.我可以向用户询问nr个字符,但我更倾向于在用户键入每个字符时调整str的大小.
所以我试图用malloc + realloc做这个,并且想法是每次用户输入一个新的char时,我使用realloc请求+1内存来保存char.
在尝试实现这一点时,我犯了一个错误,最后做了以下事情:
int main () {
/* this simulates the source of the chars... */
/* in reality I would do getch or getchar in the while loop below... */
char source[10];
int i, j;
for (i=0, j=65; i<10; i++, j++) {
source[i] = j;
}
/* relevant code starts here */
char *str = malloc(2 * sizeof(char)); /* space for 1 char + '\0' */
int current_size = 1;
i = 0;
while(i<10) {
char …Run Code Online (Sandbox Code Playgroud) 我有一个程序,有两个int类型的变量.
int num;
int other_num;
/* change the values of num and other_num with conditional increments */
printf ("result = %f\n", (float) num / other_num);
float r = (float) num / other_num;
printf ("result = %f\n", r);
Run Code Online (Sandbox Code Playgroud)
第一个printf中写入的值与第二个printf写入的值不同(当打印6个小数位时,为0.000001).
在划分之前,值是:
num = 10201
other_num = 2282
Run Code Online (Sandbox Code Playgroud)
我已将结果数字打印到15位小数.这些数字在小数点后7位有所不同,这解释了第6位的差异.
以下是15位小数的数字:
4.470201577563540
4.470201492309570
Run Code Online (Sandbox Code Playgroud)
我知道浮点舍入问题,但我期望在赋值和printf参数中执行时计算结果是相同的.
为什么这个期望不正确?
谢谢.