将Java转换为Kotlin会破坏上下文菜单。

huangapple go评论96阅读模式
英文:

Convert java to kotlin breaks contextmenu

问题

我已经将您提供的内容翻译成中文,如下:

我一直在将一个项目转换为 Kotlin,并发现了一个问题。来自 Java 代码的上下文菜单在生成的 Kotlin 代码中出现问题。这是一个项目源代码的简化测试。它仅包含一个主活动(main activity),一个单一的布局和一个上下文菜单。Java 版本可用,但是 Kotlin 版本会崩溃。我唯一能想到的不寻常之处是我注册的视图是 RelativeLayout 中的一个 imageView。

Java 版本中的 MainActivity.java 代码如下:

  1. public class MainActivity extends AppCompatActivity {
  2. private static int animationSpeed = 0;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. registerForContextMenu(findViewById(R.id.imageView));
  8. }
  9. @Override
  10. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  11. super.onCreateContextMenu(menu, v, menuInfo);
  12. MenuInflater inflater = getMenuInflater();
  13. inflater.inflate(R.menu.speed_select, menu);
  14. menu.getItem(animationSpeed).setChecked(true);
  15. }
  16. @Override
  17. public boolean onContextItemSelected(MenuItem item) {
  18. int itemId = item.getItemId();
  19. boolean rv = true;
  20. switch (itemId) {
  21. case R.id.animate_slow:
  22. animationSpeed = 0;
  23. break;
  24. case R.id.animate_normal:
  25. animationSpeed = 1;
  26. break;
  27. case R.id.animate_fast:
  28. animationSpeed = 2;
  29. break;
  30. default:
  31. Log.d("onContextItemSelected", String.format("menu item unhandled:0x%08x", itemId));
  32. rv = false;
  33. }
  34. return rv;
  35. }
  36. }

Kotlin 版本中的 MainActivity.kt 代码如下:

  1. class MainActivity : AppCompatActivity() {
  2. companion object {
  3. private var animationSpeed = 0
  4. }
  5. override fun onCreate(savedInstanceState: Bundle?) {
  6. super.onCreate(savedInstanceState)
  7. setContentView(R.layout.activity_main)
  8. registerForContextMenu(findViewById(R.id.imageView))
  9. }
  10. override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenuInfo) {
  11. super.onCreateContextMenu(menu, v, menuInfo)
  12. val inflater = menuInflater
  13. inflater.inflate(R.menu.speed_select, menu)
  14. menu.getItem(animationSpeed)?.isChecked = true
  15. }
  16. override fun onContextItemSelected(item: MenuItem): Boolean {
  17. val itemId = item.itemId
  18. var rv = true
  19. when (itemId) {
  20. R.id.animate_slow -> animationSpeed = 0
  21. R.id.animate_normal -> animationSpeed = 1
  22. R.id.animate_fast -> animationSpeed = 2
  23. else -> {
  24. Log.d("onContextItemSelected", String.format("menu item unhandled:0x%08x", itemId))
  25. rv = false
  26. }
  27. }
  28. return rv
  29. }
  30. }

另外,您提供的菜单文件 speed_select.xml 和活动布局文件也保持不变。

英文:

I've been converting a project to Kotlin and discovered a problem. The context menu from the java code is broken in the generated kotlin.<br/>This is a simplified test of the source from the project. It consists of only a main activity with a single layout and a context menu. The java version works but the kotlin version crashes. The only thing I can think of that is unusual is that the view I'm registering is an imageView in a RelativeLayout.

  1. java.lang.NullPointerException:
  2. Parameter specified as non-null is null:
  3. method kotlin.jvm.internal.Intrinsics.checkNotNullParameter
  4. , parameter menuInfo
  5. at com...MainActivity.onCreateContextMenu(MainActivity.kt)
  6. at android.view.View.createContextMenu(View.java:8392)
  7. at com.android.internal.view.menu.ContextMenuBuilder
  8. .show(ContextMenuBuilder.java:81)
  9. at com.android.internal.policy.impl
  10. .PhoneWindow$DecorView
  11. .showContextMenuForChild(PhoneWindow.java:2517)
  12. at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)

MainActivity.java is:

  1. public class MainActivity extends AppCompatActivity {
  2. private static int animationSpeed = 0;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. registerForContextMenu(findViewById(R.id.imageView));
  8. }
  9. @Override
  10. public void onCreateContextMenu(ContextMenu menu, View v,
  11. ContextMenu.ContextMenuInfo menuInfo) {
  12. super.onCreateContextMenu(menu, v, menuInfo);
  13. MenuInflater inflater = getMenuInflater();
  14. inflater.inflate(R.menu.speed_select, menu);
  15. menu.getItem(animationSpeed).setChecked(true);
  16. }
  17. @Override
  18. public boolean onContextItemSelected(MenuItem item) {
  19. int itemId = item.getItemId();
  20. boolean rv = true;
  21. switch(itemId) {
  22. case R.id.animate_slow: animationSpeed = 0; break;
  23. case R.id.animate_normal: animationSpeed = 1; break;
  24. case R.id.animate_fast: animationSpeed = 2; break;
  25. default: Log.d(&quot;onContextItemSelected&quot;, String.format(
  26. &quot;menu item unhandled:0x%08x&quot;, itemId)
  27. );
  28. rv = false;
  29. }
  30. return rv;
  31. }
  32. }

MainActivity.kt is:

  1. class MainActivity : AppCompatActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContentView(R.layout.activity_main)
  5. registerForContextMenu(findViewById(R.id.imageView))
  6. }
  7. override fun onCreateContextMenu(menu: ContextMenu, v: View,
  8. menuInfo: ContextMenuInfo) {
  9. super.onCreateContextMenu(menu, v, menuInfo)
  10. val inflater = menuInflater
  11. inflater.inflate(R.menu.speed_select, menu)
  12. menu.getItem(animationSpeed).isChecked = true
  13. }
  14. override fun onContextItemSelected(item: MenuItem): Boolean {
  15. val itemId = item.itemId
  16. var rv = true
  17. when (itemId) {
  18. R.id.animate_slow -&gt; animationSpeed = 0
  19. R.id.animate_normal -&gt; animationSpeed = 1
  20. R.id.animate_fast -&gt; animationSpeed = 2
  21. else -&gt; {
  22. Log.d(&quot;onContextItemSelected&quot;, String.format(
  23. &quot;menu item unhandled:0x%08x&quot;, itemId))
  24. rv = false
  25. }
  26. }
  27. return rv
  28. }
  29. companion object {
  30. private var animationSpeed = 0
  31. }
  32. }

My menu file is:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; &gt;
  3. &lt;group
  4. android:checkableBehavior=&quot;single&quot;
  5. android:id=&quot;@+id/animate_speed&quot; &gt;
  6. &lt;item android:id=&quot;@+id/animate_slow&quot;
  7. android:title=&quot;@string/slow&quot; /&gt;
  8. &lt;item android:id=&quot;@+id/animate_normal&quot;
  9. android:title=&quot;@string/normal&quot; /&gt;
  10. &lt;item android:id=&quot;@+id/animate_fast&quot;
  11. android:title=&quot;@string/fast&quot; /&gt;
  12. &lt;/group&gt;
  13. &lt;/menu&gt;

The activity layout is:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. android:id=&quot;@+id/relative_layout&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;&gt;
  7. &lt;ImageView
  8. android:id=&quot;@+id/imageView&quot;
  9. android:layout_width=&quot;250dp&quot;
  10. android:layout_height=&quot;250dp&quot;
  11. android:layout_centerInParent=&quot;true&quot;
  12. android:background=&quot;@drawable/andy&quot;
  13. /&gt;
  14. &lt;/RelativeLayout&gt;

I've tried breaking in the onCreateContextMenu but never get there.<br/>
I'm using Kotlin 1.40, AndroidStudio 4.01, SDK 30, and gradle 4.01. I've been looking at the docs and the code for a couple of days now and to me, the generated kotlin looks right.<br/>Thanks!

Thanks to John Healy below this was solved.
John said he thought it might be in Kotlin's null-safety handling. I doubted so I added a log statement to the working Java code and menuInfo was coming in as a null. I added a @Nullable annotation to the Java declaration which gave me:

  1. public void onCreateContextMenu(
  2. ContextMenu menu, View v,
  3. @Nullable ContextMenu.ContextMenuInfo menuInfo)

Testing of the Java code showed the compiler and lint were happy and the code still ran. I again ran the jave through the conversion process and the resulting kotlin signature for the function is:

  1. override fun onCreateContextMenu(
  2. menu: ContextMenu, v: View,
  3. menuInfo: ContextMenuInfo?)

I tested the Kotlin and it now works too!

NOTE: for your edification and amusement I posted the source on
git hub.

答案1

得分: 3

我只是在发布这个回复到我的问题,以便人们注意到问题得到了解决,这要归功于一位评论者。这个修复方法只适用于使用菜单列表而不是创建单独菜单项的情况,并且只有在使用Kotlin时才需要进行修复,因为Kotlin处理空安全的方式不同。请查看问题末尾以及我对那个问题的评论,
Steve S。

英文:

I'm only puting up this reply to my question so people will notice a solution was found thanks to a commentor. This fix only applies if you are using a menu list rather than creating individual menu items and the fix is only necessary for Kotlin because of the way in which Kotlin handles null-safety. Please look at the end of the question and at my comment to that question,
<br>Steve S.

huangapple
  • 本文由 发表于 2020年9月2日 06:19:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63696170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定