Android中经常会使用多个xml文件,但在Mainactivity中使用的setContentView(R.layout.main)只加载main.xml文件,其他xml文件不加载进当前视图,当我们要用到其他xml文件中的控件是发现直接使用findViewById()方法时不报错但控件的值找不到为null,而一旦为该控件添加相应事件就会出现空指针异常。原因就在于控件并未加载进当前视图。

解决方法:两种

1、使用在main.xml中使用include语句

<include layout="@layout/x"/>

2、使用LayoutInflater 举个简单;例子

两个xml文件main.xml和x.xml

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/tv"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" /></LinearLayout>

x.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >   <Button        android:id="@+id/bt"       android:layout_width="wrap_content"       android:layout_height="wrap_content"              android:layout_below="@id/tv"       /></RelativeLayout>

activity中的代码:

       
 1 package leemo.e; 2  3  4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 import android.widget.LinearLayout;11 import android.widget.TextView;12 import android.widget.Toast;13 14 public class EeeActivity extends Activity {15     /** Called when the activity is first created. */16     @Override17     public void onCreate(Bundle savedInstanceState) {18         super.onCreate(savedInstanceState);19         setContentView(R.layout.main);20         TextView tv = (TextView) findViewById(R.id.tv);21         tv.setText("content is change");22         23         24         LayoutInflater layout=this.getLayoutInflater();25         View view=layout.inflate(R.layout.x,null);26         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  27                 LinearLayout.LayoutParams.FILL_PARENT,  28                 LinearLayout.LayoutParams.WRAP_CONTENT);29                 addContentView(view,params);30         Button bt = (Button) view.findViewById(R.id.bt);31         bt.setText("µã»÷ÎÒ ");32         bt.setOnClickListener(new OnClickListener() {33 34             @Override35             public void onClick(View v) {36                 // TODO Auto-generated method stub37                 Toast.makeText(EeeActivity.this, "button click",38                         Toast.LENGTH_SHORT).show();39             }40         });41         42     }43     44 }
   

这样也能达到同样的效果 ,不过发现个问题,控件的位置不好控制,留待以后吧。。。。

更多相关文章

  1. 菜鸟学Android(安卓)之 selector背景选择器
  2. Android(安卓)pdf viewer在android studio应用问题说明详解
  3. Android(安卓)studio build.gradle 各种错误解决总结
  4. Android前端—显示GIF动画
  5. Android语言国际化values资源文件命名规则
  6. Android(安卓)如何对/dev/log路径设备节点进行读写
  7. 生成keystore
  8. 第6章 数据存储
  9. Android(安卓)适配之FileProvider的使用

随机推荐

  1. Android JellyBean Keyguard锁屏
  2. android webView错误处理
  3. Android中内存占用的含义:(VSS,PSS,RSS,USS)
  4. Android中各种drawable的使用
  5. 用android中的Preferencescreen 启动一个
  6. android studio上imageloader初探
  7. linux android 如何把android项目打成jar
  8. android C编程技巧 及 C/C++开发测试
  9. 【Android(安卓)界面效果49】RecyclerVie
  10. TextView省略号