【问题标题】:How to reduce the if else if ladder code length which is displaying the textviews based on the text inside the if condition如何减少 if else if 梯形代码长度,它根据 if 条件内的文本显示 textviews
【发布时间】:2019-02-16 18:25:26
【问题描述】:

我是 Android 开发的新手。我创建了一个应用程序,它将在单击文本视图时显示相应的文本视图。

为此,我给出了一个 if-else-if 梯形条件语句来显示文本视图。由于我有很多文本视图(大约 100 个),在构建项目 android studio 时说代码太长了。

如何解决这个问题。是否可以使用 MAP 遍历 textview IDS?

请帮帮我。

 public void click(View v) {

    switch (v.getId()) {
        case R.id.fam:
            // check if a child is set to a specific String
            String toSearchFor = "badaatext";
            boolean found = false;
            ArrayList<View> allViewsWithinMyTopView = getAllChildren(findViewById(R.id.linear));
            for (View child : allViewsWithinMyTopView) {
                if (child instanceof TextView) {
                    TextView childTextView = (TextView) child;
                    int intName = childTextView.getId();
                    System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intName));
                    if (childTextView.getResources().getResourceEntryName(intName).startsWith("badaatext")) {
                        found = true;
                        System.out.println("The text is foudn inside the layout");
                        System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intName));
                        childTextView.setVisibility(View.GONE);

                        String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
                        System.out.println("The Big TextView is : " + BadaaString);
                        String ChotaString = BadaaString.substring(9, 12);
                        System.out.println("The Small Textview is : " + ChotaString);
                        if (ChotaString.equalsIgnoreCase("rel")) {
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.rel);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("nam")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.nam);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mom")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mom);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("dad")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.dad);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("elb")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.elb);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("els")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.els);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("yos")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.yos);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("sil")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.sil);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("yob")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.yob);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("hus")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.hus);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("wif")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.wif);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("fre")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.fre);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("tea")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.tea);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("ser")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.ser);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mai")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mai);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("wom")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.wom);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("stm")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.stm);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("man")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.man);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("boy")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.boy);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gir")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gir);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gue")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gue);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("cus")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.cus);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("hoi")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.hoi);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gif")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gif);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("son")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.son);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("dau")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.dau);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gpa")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gpa);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gma")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gma);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gdp")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gdp);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gdm")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gdm);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("und")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.und);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("aud")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.aud);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("maa")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.maa);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mam")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mam);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mad")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mad);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("ata")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.ata);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("fla")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.fla);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("mla")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.mla);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("sod")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.sod);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("gru")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.gru);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("som")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.som);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("grm")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.grm);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("unp")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.unp);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else if(ChotaString.equalsIgnoreCase("aup")){
                            //Display the Small TextView:
                            TextView Engtxt1 = (TextView) findViewById(R.id.aup);
                            Engtxt1.setVisibility(View.VISIBLE);
                        }else{
                            System.out.println("No Matches found");
                        }

                    }
                }
            }
            if (!found) {
                //fail("Text '" + The text is not available in the layout");
                System.out.println("The TextView is not present inside the layout");
            }


            //Make the Clicked View to be Invisible
            TextView Engfam = (TextView) findViewById(R.id.fam);
            Engfam.setVisibility(View.GONE);

            //Adding the corresponding dynamic textview
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            /* Add textview 1 */
            TextView textViewFamily = new TextView(lessonSix.this);
//            textView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
//                    LinearLayout.LayoutParams.MATCH_PARENT));
            LinearLayout.LayoutParams textviewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewFamily, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);

            textviewLayoutParams.setMargins(0, 9, 0, 0);
//                linearLayout.addView(textView1, 1, textviewLayoutParams);
            textViewFamily.setId(R.id.badaatextFam);
            textViewFamily.setLayoutParams(textviewLayoutParams);
            textViewFamily.setBackgroundResource(R.drawable.shapelineinsideroundedrect
 );
textViewFamily.setText("FAMILY - குடும்பம்");
            textViewFamily.setGravity(Gravity.TOP | Gravity.CENTER);
            textViewFamily.setTextSize(20);
            textViewFamily.setTypeface(null, Typeface.BOLD);
            textViewFamily.setPadding(0, 25, 0, 25);
            textViewFamily.setVisibility(View.VISIBLE);
            linearLayout.addView(textViewFamily, 1);
            break;

 case R.id.gir:

            // check if a child is set to a specific String
            found = false;
            ArrayList<View> allViewsWithinMyTopView21 = getAllChildren(findViewById(R.id.linear));
            for (View child : allViewsWithinMyTopView21) {
                if (child instanceof TextView) {
                    childTextView = (TextView) child;
                    intchildName = childTextView.getId();
                    System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intchildName));
                    if (childTextView.getResources().getResourceEntryName(intchildName).startsWith("badaatext")) {
                        found = true;
                        System.out.println("The text is foudn inside the layout");
                        System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intchildName));
                        childTextView.setVisibility(View.GONE);

                        String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
                        System.out.println("The Big TextView is : " + BadaaString);
                        String ChotaString = BadaaString.substring(9, 12);
                        System.out.println("The Small Textview is : " + ChotaString);
//if (ChotaString.equalsIgnoreCase("fam")) {
// //Display the Small TextView:
// TextView Engtxt1 = (TextView) findViewById(R.id.fam);
//Engtxt1.setVisibility(View.VISIBLE);
//}
       int textViewId = context.getResources().getIdentifier(ChotaString, "id", context.getPackageName());
                        TextView Engtxt1 = (TextView) findViewById(textViewId);
                        Engtxt1.setVisibility(View.VISIBLE);

                    }

                }
            }
            if (!found) {
                //fail("Text '" + The text is not available in the layout");
                System.out.println("The TextView is not present inside the layout");
            }
            //Make the Clicked View to be Invisible
            TextView Enggir = (TextView) findViewById(R.id.gir);
            Enggir.setVisibility(View.GONE);


            //Adding the corresponding dynamic textview
            LinearLayout linearLayout21 = (LinearLayout) findViewById(R.id.linear);
            linearLayout21.setOrientation(LinearLayout.VERTICAL);
            /* Add textview 1 */
            TextView textViewGirl = new TextView(lessonSix.this);
//textView1.setLayoutParams(new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
//LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams textviewLayoutParams21 = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewGirl, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);
            textviewLayoutParams21.setMargins(0, 9, 0, 0);
//linearLayout.addView(textView1, 1, textviewLayoutParams);s
            textViewGirl.setId(R.id.badaatextGir);
            textViewGirl.setLayoutParams(textviewLayoutParams21);
            textViewGirl.setBackgroundResource(R.drawable.shapelineinsideroundedrect);
            textViewGirl.setText("GIRL - சிறுமி");
            textViewGirl.setGravity(Gravity.TOP | Gravity.CENTER);
            textViewGirl.setTextSize(20);
            textViewGirl.setTypeface(null, Typeface.BOLD);
            textViewGirl.setPadding(0, 25, 0, 25);
            textViewGirl.setVisibility(View.VISIBLE);
            linearLayout21.addView(textViewGirl, 40);
            break;

相关的 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context=".lessonSix">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--<TextView-->
        <!--android:id="@+id/badaatextOne"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="130dp"-->
        <!--android:layout_marginTop="5dp"-->
        <!--android:paddingTop="25dp"-->
        <!--android:background="@drawable/shapelineinsideroundedrect"-->
        <TextView
            android:id="@+id/fam"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/shaperoundedcornerrect"
            android:text="FAMILY"
            android:onClick="click"
            android:clickable="true"
            android:gravity="center"
            android:textSize="20dip"
            android:textStyle="bold"
            tools:ignore="OnClick" />

        <TextView
            android:id="@+id/rel"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/shaperoundedcornerrect"
            android:text="RELATION"
            android:clickable="true"
            android:onClick="click"
            android:gravity="center"
            android:textSize="20dip"
            android:textStyle="bold" />
 <TextView
            android:id="@+id/gir"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/shaperoundedcornerrect"
            android:text="GIRL"
            android:clickable="true"
            android:onClick="click"
            android:gravity="center"
            android:textSize="20dip"
            android:textStyle="bold"/>

</LinearLayout>
</ScrollView>

</RelativeLayout>

添加例外: 致命例外:主要 进程:com.seekho.noushad.dictionary,PID:30013 java.lang.IllegalStateException:无法在 android.view.View.performClick(View.java:4788) 的 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 执行 android:onClick 的方法在 android.view.View$PerformClick.run(View.java:19923) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android。 os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5401) 在 java.lang.reflect.Method.invoke(Native Method) 在 java.lang.reflect.Method。 com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) 调用(Method.java:372) 原因:java。 lang.reflect.InvocationTargetException 应用程序终止。

New Log(Exception): 

D/AndroidRuntime:关闭虚拟机 E/AndroidRuntime: 致命异常: main 进程:com.seekho.noushad.dictionary,PID:24918 java.lang.IllegalStateException:无法执行 android:onClick 的方法 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 在 android.view.View.performClick(View.java:4788) 在 android.view.View$PerformClick.run(View.java:19923) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5401) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:714) 引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在 android.view.View.performClick(View.java:4788) 在 android.view.View$PerformClick.run(View.java:19923) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5401) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:714) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.content.res.Resources android.content.Context.getResources()' 在 com.seekho.noushad.dictionary.lessonSix.click(lessonSix.java:4893) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在 android.view.View.performClick(View.java:4788) 在 android.view.View$PerformClick.run(View.java:19923) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5401) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:714) I/Process:发送信号。 PID:24918 SIG:9 应用程序终止。

【问题讨论】:

    标签: android android-studio textview


    【解决方案1】:

    您可以通过其名称获取TextView Id。由于您的if() 条件字符串等于TextView 的名称,您可以使用:

    int textViewId = context.getResources().getIdentifier(ChotaString, "id", context.getPackageName());
    TextView Engtxt1 = (TextView) findViewById(textViewId);
    Engtxt1.setVisibility(View.VISIBLE);
    

    而不是整个if(){}else{}

    【讨论】:

    • 你看到我附上的最新日志了吗
    • java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 上下文为空。显示你是如何初始化它的。
    • 公开课课程六扩展了 AppCompatActivity { 工具栏工具栏;私人 CharSequence mTitle;视图组视图组;公共意图意图;找到布尔值;文本视图 Engtxt;文本视图子文本视图; int intchildName;上下文上下文; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lesson_six);
    • 我没有看到你已经初始化了context。由于你的类是Activity,你可以使用this而不是context
    • 我使用了“this”而不是“context”,但得到了异常:原因:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setVisibility(int) ' 在 com.seekho.noushad.dictionary.lessonSix.click(lessonSix.java:4895) 的空对象引用上
    【解决方案2】:

    你可以使用 switch 代替 if else if ladder 。或任一地图,但在地图中,您需要为每个值提供一个键。

    【讨论】:

    • 但是对于 swich 语句,我也必须根据我的 textview id 的计数使用很多大小写
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多