我在android工作,我想做一个scrollview。 这是我的代码:创建水平滚动视图时出错?
package com.pericent;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
public class HelloTabWidget extends Activity {
private String TAG="HelloTabWidget";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG,"i am just before everything");
HorizontalScrollView hr=new HorizontalScrollView(this);
hr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
LinearLayout layout=new LinearLayout(this);
LinearLayout mainlayout=new LinearLayout(this);
mainlayout=(LinearLayout)findViewById(R.id.upper1);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Log.v(TAG,"i am just after the declarations");
for(int i=0;i<100;i++){
TextView txt=new TextView(this);
txt.setText("Text " + i);
layout.addView(txt);
}
Log.v(TAG,"i am after the for loop");
hr.addView(layout);
mainlayout.addView(hr);//<<---this is creating NullPointerException
setContentView(R.layout.cecking);
Log.v(TAG,"i am after the everything");
}
}
,这我cecking.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:id="@+id/upper1">
</LinearLayout>
每当我运行程序时,就会发生这样的错误: -
Unable to start activity ComponentInfo{com.pericent/com.pericent.HelloTabWidget}: java.lang.NullPointerException
,并在此行发生错误: - mainlayout.addView(hr);
请帮我找出错误的原因。 预先感谢您。
谢谢先生......我已经完成了这项工作,其实我已经犯了错误,我没有写下这行:-this.setContenView(R.layout.checking);再次感谢你... –
Adil先生,您来自巴基斯坦的拉合尔......很高兴认识您..我来自印度...... –