【问题标题】:Change layout at runtime NULL POINTER EXCEPTION在运行时更改布局 NULL POINTER EXCEPTION
【发布时间】:2013-09-17 18:07:45
【问题描述】:

我打开一个对话框,当我选择任何一个时,有两个选项按钮,下面的布局应该更改是屏幕截图

现在下面有 list.xml 文件,其中有两个线性布局

english(即英语-泰卢固语列表)先显示英文单词,然后显示泰卢固语单词

      <LinearLayout
    android:id="@+id/engRowOfList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="50dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtEng"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="10dp"
        android:textColor="#000000"
        android:textSize="22dip"
        android:textStyle="bold" />

    <com....TextView
        android:id="@+id/txtGuj"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="10dp"
        android:textColor="@color/orange"
        android:textSize="22dip"
        android:textStyle="bold"
        lht:ttf_name="fonts/telugu.ttf" />
</LinearLayout>

泰卢固语(首先显示泰卢固语单词,然后显示英文单词

 <LinearLayout
    android:id="@+id/teluguRowOfList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="50dp"
    android:orientation="vertical" >

    <com......GujTextView
        android:id="@+id/txtEng"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="10dp"
        android:textColor="#000000"
        android:textSize="22dip"
        android:textStyle="bold"
        lht:ttf_name="fonts/telugu.ttf" />

    <TextView
        android:id="@+id/txtGuj"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="10dp"
        android:textColor="@color/orange"
        android:textSize="22dip"
        android:textStyle="bold" />
</LinearLayout>

正如许多人建议的那样,您可以通过 LinearLayout.GONE/LinearLayout.VISIBLE 来做,所以我在下面的代码中尝试过,但仍然无法做到,下面是我的代码

   scAdapter = new SimpleCursorAdapter(getApplicationContext(),
            R.layout.list, cursor, new String[] { Const.ENGLISH,
                    Const.TELUGU }, new int[] { R.id.txtEng,
                    R.id.txtTelugu });

    scAdapter.setViewBinder(new ViewBinder() {

        public boolean setViewValue(View view,
                android.database.Cursor cursor, int columnIndex) {

            engListView = (LinearLayout) view.findViewById(R.id.engList);
            teluguListView = (LinearLayout) view.findViewById(R.id.teluguList);

            if (flagEnFl) { //flag for ENGLISH / TELUGU 
                engListView.setVisibility(LinearLayout.GONE);  ***//HERE I AM GETTING NULLPOINTEREXCEPTION EVERY TIME***
                teluguListView.setVisibility(LinearLayout.VISIBLE);
            } else {
                engListView.setVisibility(LinearLayout.VISIBLE);
                teluguListView.setVisibility(LinearLayout.GONE);
            }

任何人都可以帮助我如何处理这种情况

【问题讨论】:

  • 当用户选择任何选项时,为什么不只创建一个布局并更改文本视图的内容..
  • 但是当我更改 textview 的内容时,Armaan(英文文本无法正确显示,例如当我选择“@+id/txtGuj”时,当我放置英文文本时,英文文本将不会显示正确:(*
  • 可能是因为您使用了泰卢固语的 ttf 字体。所以我认为你必须根据选项选择在运行时更改字体。
  • 当有英语泰米尔语时,将字体设置为 tamil.ttf,当有泰米尔语英语时,将字体设置为任何英文字体,如 verdana.ttf。这样它就可以工作了。
  • 非常感谢 Armaan 给了我很好的建议 :) 但是你知道我如何在运行时设置属性 lht:ttf_name="fonts/telugu.ttf" 吗??

标签: android android-layout layout view android-view


【解决方案1】:

将两个布局文件合并为一个。隐藏不应首先显示的视图。 当用户从对话框中选择时,隐藏对话框并显示所需的视图。

【讨论】:

  • 非常感谢您这么好的回复,我也可以这样做谢谢:)
【解决方案2】:

简单的解决方案

将两种布局合并为一个,然后将它们设为VISIBLEGONE

<LinearLayout>
    <LinearLayout
     android:id="@+id/englishLayour>

    </LinearLayout>
   <LinearLayout
     android:id="@+id/englishLayour>

   </LinearLayout>

</LinearLayout>


radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId==R.id.radioEnglish);
            {
                  englishLayout.setVisibility(View.VISIBLE);
                  tamilLayout.setVisibility(View.GONE);

            }else{
                  englishLayout.setVisibility(View.GONE);
                  tamilLayout.setVisibility(View.VISIBLE);              
            }
    });

【讨论】:

    【解决方案3】:

    试试这个:

    在你的活动中包含这个子类:

    public class Typefaces{
    
            private final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
    
                public Typeface get(Context c, String name){
                    synchronized(cache){
                        if(!cache.containsKey(name)){
                            Typeface t = Typeface.createFromAsset(
                                    c.getAssets(), 
                                    String.format("fonts/%s.ttf", name)
                                );
                            cache.put(name, t);
                        }
                        return cache.get(name);
                    }
                }
    
            }
    

    然后像这样实现:

    Typefaces tf = new Typefaces();
    
    tvMessage.setTypeface(tf.get(context, "shruti"));
    

    不要忘记将 .ttf 文件添加到项目中的资产/字体文件夹中。(如果没有,请创建字体文件夹。)。并且字体文件名应该是小写的。在文件夹和课堂上也是如此。

    希望对你有帮助!!

    【讨论】:

      【解决方案4】:

      无法在运行时切换 2 布局。最好的解决方案是将 2 个不同的 LinearLayout 放在同一个布局中,并在用户做出决定时切换( View.VISIBLE / View.GONE )它们的可见性。

      【讨论】:

        猜你喜欢
        • 2011-10-05
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        相关资源
        最近更新 更多