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个回答

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

Toast.makeText(MainActivity.this,"123456".charAt(2),Toast.LENGTH_SHORT).show();
修改成
Toast.makeText(MainActivity.this,"3",Toast.LENGTH_SHORT).show();
或者
Toast.makeText(MainActivity.this,"123456".charAt(2).toString(),Toast.LENGTH_SHORT).show();
是否报错

如果还报错,说明和这行无关,
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
看这两行,第一个资源有没有存在,第二个按钮是不是存在

android.content.res.Resources$NotFoundException: String resource ID #0x33 老哥 你的id不存在啊 跟charAt无关 你看看xml里面 给button命名id 了么 如果命名了 id是button么

Toast.MakeText(Context context,CharSequence text,int duration)和(Context context,int resId,int duration),因为"123456".charAt(2)得到的值是
一个char类型的值,然后系统运行时会将char自动转换成int类型,,自动使用第二个构造方法。但是并没有3这个int类型的资源所以就报了找不到资源错误。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问