简体   繁体   English

CodenameOne Java Hello World 示例中的 String.Format“错误:找不到符号”

[英]CodenameOne Java String.Format "error: cannot find symbol" on Hello World example

A super-simple String.format("this is a test %d",5) doesn't work in my HelloWorld CodenameOne project: I get "error: cannot find symbol".超级简单的 String.format("this is a test %d",5) 在我的 HelloWorld CodenameOne 项目中不起作用:我收到“错误:找不到符号”。

It doesn't seem to matter what format I used, I always get the same error.我使用什么格式似乎并不重要,我总是得到同样的错误。 This seems to be an import problem, though I'm not importing any special packages outside of the defaults.这似乎是一个导入问题,尽管我没有导入默认设置之外的任何特殊包。

Here is the java source:这是 java 来源:

package com.test.test;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class MyApplication {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

/*
Updating property file: C:\Users\admin\Desktop\test2\build\built-jar.properties
Compile is forcing compliance to the supported API's/features for maximum device compatibility. This allows smaller
        code size and wider device support
Compiling 1 source file to C:\Users\admin\Desktop\test2\build\tmp
C:\Users\admin\Desktop\test2\src\com\test\test\MyApplication.java:39: error: cannot find symbol
        s = String.format("this is a test %d",5);
  symbol:   method format(String,int)
  location: class String
1 error
C:\Users\admin\Desktop\test2\build.xml:62: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)        
*/        
        String s;
        s = String.format("this is a test %d",5);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}

CodenameOne compiles your source code using its own subset of the Java SE API, which is missing some features that the standard Java API includes. CodenameOne 使用它自己的 Java SE API 子集编译您的源代码,它缺少标准 Java API 包含的一些功能。

Quoting their FAQ :引用他们的常见问题解答

What features of Java are supported?支持Java的哪些特性? What features of Java aren't supported?不支持 Java 的哪些功能?

The most obvious thing missing is reflections.最明显的缺失是反射。 The main problem is that when we package the VM into devices that don't have Java, we would have to include EVERYTHING.主要问题是,当我们将 package VM 插入没有 Java 的设备时,我们必须包含所有内容。 If reflections were included, they wouldn't work anyway since we obfuscate the code for the platforms where reflections do work (eg Android).如果包含反射,它们无论如何都不会工作,因为我们混淆了反射工作平台(例如 Android)的代码。 On top of that reflection code is generally slow and a bad idea on a mobile device to begin with.最重要的是,反射代码通常很慢,而且在移动设备上开始时不是一个好主意。 As an alternative some developers were successful with bytecode manipulation which is something that is completely seamless to the server and as performant/efficient as handcoding.作为替代方案,一些开发人员在字节码操作方面取得了成功,字节码操作与服务器完全无缝连接,并且与手动编码一样高效/高效。

Many of the desktop API's such as java.net, java.io.File etc. aren't very appropriate for mobile devices and just didn't make it.许多桌面 API,例如 java.net、java.io.File 等,都不太适合移动设备,只是没有成功。 We provide our own alternatives which are more portable and better suited for mobile settings.我们提供自己的替代品,这些替代品更便携,更适合移动设置。

Of the other missing things, if you run into a missing method or ability, there are cases where that functionality can be added.在其他缺失的东西中,如果您遇到缺失的方法或能力,在某些情况下可以添加该功能。

Specifically, its version of java.lang.String does not include the format method.具体来说,其java.lang.String版本不包含format方法。

In this case, it can be rewritten using simple string concatenation:在这种情况下,可以使用简单的字符串连接重写:

    String s = "this is a test " + 5;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM