Questions tagged «java»

Java是一种流行的高级编程语言。如果您在使用或理解语言本身时遇到问题,请使用此标签。这个标签很少单独使用,最常与[spring],[spring-boot],[jakarta-ee],[android],[javafx],[gradle]和[maven]结合使用。

8
导航抽屉项目图标未显示原始颜色
我正在尝试为导航抽屉的菜单中的项目旁边显示一个图标,但是由于某种原因,该图标始终显示为灰色,而不是原始颜色(棕色)。有什么方法可以防止这种情况发生,以显示图标的原始颜色? MainActivity.java public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); if (navigationView != null) { setupDrawerContent(navigationView); } } private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) …

17
将原始long数组转换为long列表
这可能是一个简单的,总机问题,但我的第一次尝试出乎意料地完全失败了。我想获取一组原始的long并将其转换为列表,我试图这样做: long[] input = someAPI.getSomeLongs(); List<Long> inputAsList = Arrays.asList(input); //Total failure to even compile! 什么是正确的方法?


5
插入后获取生成的ID
我将SQLite与Android一起使用,我想知道获取插入的行的生成ID的最佳方法。 我认为一种解决方案可以在包含之后进行搜索,但它并不是最好的方法。
138 java  android  sqlite 


10
如何在Java中初始化字节数组?
我必须在Java中以字节数组形式存储一些常量值(UUID),而我想知道初始化这些静态数组的最佳方法是什么。这就是我目前的做法,但我觉得必须有更好的方法。 private static final byte[] CDRIVES = new byte[] { (byte)0xe0, 0x4f, (byte)0xd0, 0x20, (byte)0xea, 0x3a, 0x69, 0x10, (byte)0xa2, (byte)0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte)0x9d }; private static final byte[] CMYDOCS = new byte[] { (byte)0xba, (byte)0x8a, 0x0d, 0x45, 0x25, (byte)0xad, (byte)0xd0, 0x11, (byte)0x98, (byte)0xa8, 0x08, 0x00, 0x36, 0x1b, …
138 java  arrays  byte 

4
是否可以将Gson实例用作模型bean中的静态字段(重用)?
这是我实现的模型: public class LoginSession { private static final Gson gson = new Gson(); private String id; private String name; private long timestamp; public LoginSession(String id, String name) { this.id = id; this.name = name; this.timestamp = System.currentTimeMillis(); } public String toJson() { return gson.toJson(this); } public static LoginSession fromJson(String json) …

18
休眠JPA序列(非ID)
是否可以对不是标识符/不属于复合标识符的某些列使用数据库序列? 我正在使用hibernate作为jpa提供程序,并且我有一个表,其中包含一些生成的值(使用序列)的列,尽管它们不是标识符的一部分。 我想要的是使用序列为实体创建新值,其中该序列的列不是主键(的一部分): @Entity @Table(name = "MyTable") public class MyEntity { //... @Id //... etc public Long getId() { return id; } //note NO @Id here! but this doesn't work... @GeneratedValue(strategy = GenerationType.AUTO, generator = "myGen") @SequenceGenerator(name = "myGen", sequenceName = "MY_SEQUENCE") @Column(name = "SEQ_VAL", unique = false, nullable = …
138 java  hibernate  jpa  sequence 

19
Android中的SQLite如何更新特定行
我一直在尝试更新特定行已有一段时间了,看来有两种方法可以做到这一点。根据我的阅读和尝试,您可以使用: execSQL(String sql) 方法 或者: update(String table, ContentValues values, String whereClause, String[] whereArgs) 方法。 (让我知道这是否不正确,因为我是android新手,还是SQL新手。) 因此,让我了解我的实际代码。 myDB.update(TableName, "(Field1, Field2, Field3)" + " VALUES ('Bob', 19, 'Male')", "where _id = 1", null); 我正在努力做到这一点: 更新Field1,Field2和Field3,其中主键(_id)等于1。 Eclipse在“更新”一词的正下方给出了一条红线,并给出了以下解释: SQLiteDatabase类型的方法update(String,ContentValues,String,String [])不适用于参数(String,String,String,null) 我猜我没有正确分配ContentValues。谁能指出我正确的方向?
138 java  android  sql  eclipse  sqlite 

9
在一条语句中一次将多个条目添加到HashMap
我需要初始化一个常量HashMap,并希望在一行语句中完成。避免这样的事情: hashMap.put("One", new Integer(1)); // adding value into HashMap hashMap.put("Two", new Integer(2)); hashMap.put("Three", new Integer(3)); 类似于目标C: [NSDictionary dictionaryWithObjectsAndKeys: @"w",[NSNumber numberWithInt:1], @"K",[NSNumber numberWithInt:2], @"e",[NSNumber numberWithInt:4], @"z",[NSNumber numberWithInt:5], @"l",[NSNumber numberWithInt:6], nil] 看了这么多,我还没有找到任何显示如何做到这一点的例子。

3
如何在web.xml中指定默认错误页面?
当用户遇到某些错误(例如,代码为404的错误)时,我正在使用web.xml中的<error-page>元素来指定友好错误页面: <error-page> <error-code>404</error-code> <location>/Error404.html</location> </error-page> 但是,我希望如果用户不符合中指定的任何错误代码<error-page>,则他或她应该看到默认错误页面。我该如何使用web.xml中的元素呢?

10
如何在Java中实现无穷大?
Java是否可以代表每种数值数据类型的无穷大?如何实现它以便我可以对其进行数学运算? 例如 int myInf = infinity; //However it is done myInf + 5; //returns infinity myInf*(-1); //returns negative infinity 我尝试使用非常大的数字,但是我想要一个适当,简单的解决方案。
138 java  double  infinity 

5
自动装配实现相同接口的两个bean-如何将默认bean设置为自动装配?
背景: 我有一个Spring 2.5 / Java / Tomcat应用程序。下面的bean在整个应用程序中的许多地方都使用过 public class HibernateDeviceDao implements DeviceDao 以下是新的bean: public class JdbcDeviceDao implements DeviceDao 第一个bean的配置如下(包含了软件包中的所有bean) <context:component-scan base-package="com.initech.service.dao.hibernate" /> 第二个(新)bean是单独配置的 <bean id="jdbcDeviceDao" class="com.initech.service.dao.jdbc.JdbcDeviceDao"> <property name="dataSource" ref="jdbcDataSource"> </bean> 启动服务器时,这(当然)会导致异常: 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[com.sevenp.mobile.samplemgmt.service.dao.DeviceDao]类型的唯一bean:期望的单个匹配bean,但发现2:[deviceDao,jdbcDeviceDao] 从试图像这样自动装配bean的类中 @Autowired private DeviceDao hibernateDevicDao; 因为有两个bean实现相同的接口。 问题: 是否可以配置bean,以便 1.我不必对现有的类进行更改,因为这些类已经HibernateDeviceDao自动接线 2.仍然可以像这样使用第二个(新)bean: @Autowired @Qualifier("jdbcDeviceDao") 也就是说,我需要一种将HibernateDeviceDaobean 配置为自动装配的默认bean的方法,同时允许在JdbcDeviceDao带有@Qualifier注释的显式指定中同时使用the 。 我已经尝试过的: 我尝试设置属性 autowire-candidate="false" …

6
是否可以重命名带有依赖项的Maven jar?
我目前正在使用jar-with-dependencies程序集来创建这样的jar。但是,我的罐子的名称有点长。 由于AS400上的RPG程序正在使用此jar,因此我想缩短它,以使这些开发人员的工作变得更轻松。但是,除了手工之外,我还没有找到一种方法来从通常的罐子中重命名罐子project-name-version-classifier-jar-with-dependencies.jar。我想要类似的东西project-name-version-classifier-full.jar 无论如何,在没有基本复制jar-with-dependencies程序集描述符并将其完全调用的情况下执行此操作吗? 另外,我想继续将没有组装的类路径的jar存储在存储库中。 我需要两个工件。具有我的分类器的jar包含构建所针对的区域。具有所有依赖项的jar,其中也包括区域。 project-name-version-region-full.jar并project-name-version-region.jar应存储在存储库中。在第一个示例中,分类器是全区域的,在第二个示例中是区域。后者正在工作。

11
什么时候使用Spring Integration vs.Camel?
作为经验丰富的Spring用户,我以为Spring Integration在需要一些(JMS)消息传递功能(更多详细信息)的最新项目中最有意义。在使用Spring Integration工作了几天之后,考虑到要配置一些请求-响应(侦听不同的JMS队列)通信所必须配置的通道数量,仍然感觉到很多配置开销。 因此,我一直在寻找一些Camel与Spring Integration有何不同的背景信息,但是似乎那里的信息还很多余,我发现: http://java.dzone.com/articles/spring-integration-and-apache(从2009年12月开始,在Spring Integration和Camel中实现真实的集成方案之间进行了非常中立的比较) http://hillert.blogspot.com/2009/10/apache-camel-alternatives.html(将骆驼与其他解决方案进行比较,2009年10月) http://raibledesigns.com/rd/entry/taking_apache_camel_for_a(Matt Raible,2008年10月) 问题是:您在使用一个堆栈而不是另一个堆栈时获得了哪些经验?在哪种情况下,您会建议Camel缺少Spring Integration支持?您在哪里看到各自的优缺点?来自现实世界项目的任何建议都将受到高度赞赏。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.