Android 运行时更改布局空指针异常
我正在打开一个对话框,其中有两个选项按钮,当我选择其中任何一个时,布局应该更改,下面是屏幕截图 下面是list.xml文件,其中有两个线性布局 英语(即英语泰卢固语列表)首先显示英语单词,然后显示泰卢固语单词Android 运行时更改布局空指针异常,android,android-layout,layout,view,android-view,Android,Android Layout,Layout,View,Android View,我正在打开一个对话框,其中有两个选项按钮,当我选择其中任何一个时,布局应该更改,下面是屏幕截图 下面是list.xml文件,其中有两个线性布局 英语(即英语泰卢固语列表)首先显示英语单词,然后显示泰卢固语单词 <LinearLayout android:id="@+id/engRowOfList" android:layout_width="fill_parent" android:layout_height="wrap_content" an
<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>
<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);
}
});
radioGroup.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
if(checkedId==R.id.radioEnglish);
{
设置可见性(View.VISIBLE);
tamilLayout.setVisibility(View.GONE);
}否则{
英语布局。设置可见性(视图。消失);
setVisibility(View.VISIBLE);
}
});
试试这个:
在活动中包含此子类:
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);
}
}
}
不要忘记将.ttf文件添加到项目中的asset/fonts文件夹中。(如果没有,请创建fonts文件夹。)。字体文件名也应使用小写字母。在文件夹中以及在类中
希望有帮助!!在运行时无法切换2个布局。最好的解决方案是在同一布局中放置2个不同的线性布局并切换(View.VISIBLE/View.GONE)当用户做出决定时它们的可见性。为什么不在用户选择任何选项时只创建一个布局并更改textview的内容..但在我更改textview的内容时会显示Arman(例如,当我选择“@+id/txtGuj”时,英文文本没有正确显示)当我放置英文文本时,英文文本将无法正确显示:(*可能是因为您在泰卢固语中使用了ttf字体。因此,我认为您必须根据选项选择在运行时更改字体。如果有英语泰米尔语,则将字体设置为泰米尔语。ttf;如果有泰米尔语英语,则将字体设置为任何英语字体,如verdana.ttf。这样将使其正常工作。非常感谢Arman为我提供了漂亮的suggestion:)但您能知道我如何在运行时设置属性lht:ttf_name=“font/telugu.ttf”?非常感谢您如此友好的回复,我也可以这样做,谢谢:)
<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);
}
});
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"));