gpt4 book ai didi

java - 在 Android 中动态实例化按钮

转载 作者:行者123 更新时间:2023-12-01 17:43:27 28 4
gpt4 key购买 nike

假设我在activity_main.xml中有10个按钮,其id为b1、b2、b3、b4...直到...b10;我想动态实例化它们。我尝试了以下代码。

Button b[] = new Button[10];

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

for( int i = 0; i < b.length ; i++ )
{
b[i] = (Button)findViewById(getResources().getIdentifier("b"+(i+1), "drawable", getPackageName()));
System.out.println("TAG OF "+b[i].getId()+" is "+b[i].getTag());
}
}

我收到以下错误

2019-09-28 23:18:40.250 3096-3096/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-09-28 23:18:40.250 3096-3096/? E/libpersona: scanKnoxPersonas
2019-09-28 23:18:40.250 3096-3096/? E/libpersona: Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
2019-09-28 23:18:41.237 3096-3096/com.example.raagidentifier E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.raagidentifier, PID: 3096
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.raagidentifier/com.example.raagidentifier.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.Button.getId()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.Button.getId()' on a null object reference
at com.example.raagidentifier.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:7258)
at android.app.Activity.performCreate(Activity.java:7249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1222)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:7000) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

怎样才能达到想要的结果?

更新

根据评论中的建议,我使用了“id”而不是“drawable”。

b[i] = (Button)findViewById(getResources().getIdentifier("b"+(i+1), "id", getPackageName()));

我收到以下错误。

09/29 00:38:57: Launching 'app' on samsung SM-G610F.
$ adb shell am start -n "com.example.raagidentifier/com.example.raagidentifier.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Connected to process 10760 on device 'samsung-sm_g610f-52001c95fe0794f1'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/zygote: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2559)
at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:647)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:790)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:730)
at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:863)
at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:824)
at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374)
at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:749)
at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:659)
at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:552)
at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:161)
at void com.example.raagidentifier.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:18)
at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:7258)
at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7249)
at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1222)
at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2927)
at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:3059)
at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1724)
at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
at void android.os.Looper.loop() (Looper.java:164)
at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:7000)
at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:441)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1408)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.raagidentifier-9TtRSpsKjCr22NxWyAtMAQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.raagidentifier-9TtRSpsKjCr22NxWyAtMAQ==/lib/arm, /system/lib, /system/vendor/lib]]
at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:125)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
at void androidx.core.view.ViewCompat.setBackground(android.view.View, android.graphics.drawable.Drawable) (ViewCompat.java:2559)
at void androidx.appcompat.widget.ActionBarContainer.<init>(android.content.Context, android.util.AttributeSet) (ActionBarContainer.java:63)
at java.lang.Object java.lang.reflect.Constructor.newInstance0(java.lang.Object[]) (Constructor.java:-2)
at java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[]) (Constructor.java:334)
at android.view.View android.view.LayoutInflater.createView(java.lang.String, java.lang.String, android.util.AttributeSet) (LayoutInflater.java:647)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:790)
at android.view.View android.view.LayoutInflater.createViewFromTag(android.view.View, java.lang.String, android.content.Context, android.util.AttributeSet) (LayoutInflater.java:730)
at void android.view.LayoutInflater.rInflate(org.xmlpull.v1.XmlPullParser, android.view.View, android.content.Context, android.util.AttributeSet, boolean) (LayoutInflater.java:863)
at void android.view.LayoutInflater.rInflateChildren(org.xmlpull.v1.XmlPullParser, android.view.View, android.util.AttributeSet, boolean) (LayoutInflater.java:824)
at android.view.View android.view.LayoutInflater.inflate(org.xmlpull.v1.XmlPullParser, android.view.ViewGroup, boolean) (LayoutInflater.java:515)
I/zygote: at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean) (LayoutInflater.java:423)
at android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup) (LayoutInflater.java:374)
at android.view.ViewGroup androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:749)
at void androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:659)
at void androidx.appcompat.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:552)
at void androidx.appcompat.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:161)
at void com.example.raagidentifier.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:18)
at void android.app.Activity.performCreate(android.os.Bundle, android.os.PersistableBundle) (Activity.java:7258)
at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:7249)
at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1222)
at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2927)
at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:3059)
at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1724)
at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:106)
at void android.os.Looper.loop() (Looper.java:164)
at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:7000)
at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:441)
at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1408)

最佳答案

这是@Kishan的java版本回答

static final int NUM_OF_BUTTONS=10;
List<Button> buttonsList = new ArrayList<>(NUM_OF_BUTTONS);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);

for( int i=0 ;i<NUM_OF_BUTTONS;i++){
String value = "btn"+(i+1);
int id = getResources().getIdentifier(value,"id",getPackageName());
buttonsList.add((Button) findViewById(id));
}

}

关于java - 在 Android 中动态实例化按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58148823/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com