weixin_45401701 2019-11-04 17:31 采纳率: 0%
浏览 360
已采纳

Android 一用charAt()就卡退了

不知道怎么回事。。

package com.example.lenovo.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"123456".charAt(2),Toast.LENGTH_SHORT).show();
            }
        });
    }
}

图片说明

报错:


11/04 17:21:36: Launching app
$ adb push D:\study\Android\MyApplication2\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.lenovo.myapplication
$ adb shell pm install -r "/data/local/tmp/com.example.lenovo.myapplication"
Success


$ adb shell am start -n "com.example.lenovo.myapplication/com.example.lenovo.myapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 4133 on device Nexus_5_API_24 [emulator-5554]
W/System: ClassLoader referenced unknown path: /data/app/com.example.lenovo.myapplication-1/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.example.lenovo.myapplication, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.example.lenovo.myapplication-1/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/gralloc_ranchu: Gralloc pipe failed

                  [ 11-04 09:21:43.377  4133: 4133 D/         ]
                  HostConnection::get() New Host Connection established 0xa63882c0, tid 4133


                  [ 11-04 09:21:43.425  4133: 4224 D/         ]
                  HostConnection::get() New Host Connection established 0xa6388740, tid 4224
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/ResourceType: No package identifier when getting value for resource number 0x00000033
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.lenovo.myapplication, PID: 4133
                  android.content.res.Resources$NotFoundException: String resource ID #0x33
                      at android.content.res.Resources.getText(Resources.java:331)
                      at android.widget.Toast.makeText(Toast.java:287)
                      at com.example.lenovo.myapplication.MainActivity$1.onClick(MainActivity.java:19)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22260)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.

  • 写回答

4条回答 默认 最新

  • 王能 2019-11-05 14:52
    关注

    都没仔细看问题啊,Toast.makeText有2个重载方法,你的char被隐式强转成int的那个了。
    改成String就行了:String.valueOf("123456".charAt(2))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus