Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1280
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-16 19:55
文章分类
文章存档

2015年(1)

我的朋友
最近访客

分类: Android平台

2015-04-16 22:26:44

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

解决方法:两种

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

        

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

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

main.xml

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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>
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

x.xml

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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>
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

 

activity中的代码:

package leemo.e;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class EeeActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView tv = (TextView) findViewById(R.id.tv);
		tv.setText("content is change");
		
		
		LayoutInflater layout=this.getLayoutInflater();
		View view=layout.inflate(R.layout.x,null);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
				LinearLayout.LayoutParams.FILL_PARENT,  
				LinearLayout.LayoutParams.WRAP_CONTENT);
				addContentView(view,params);
		Button bt = (Button) view.findViewById(R.id.bt);
		bt.setText("???÷?? ");
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(EeeActivity.this, "button click",
						Toast.LENGTH_SHORT).show();
			}
		});
		
	}
	
}
这样也能达到同样的效果 ,不过发现个问题,控件的位置不好控制,留待以后吧。。。。
阅读(394) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~