我正在尝试将xml布局资源嵌入到在代码中创建的自定义视图中。
例如,我有一个名为about.xml的xml文件
我正在尝试使用inflate方法,但是我收到了一个NULL POINTER EXCEPTION CALL并杀死了我的应用。
在我的onCreate方法中,我正在调用
linearOut = new LinearLayout(this);
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
scroll = new ScrollView(this);
linearOut.addView(scroll);
scroll.addView(linear);
text=new TextView(this);
text.setHeight(250);
text.setWidth(320);
text.setBackgroundColor(Color.argb(255,255,255,255));
text.setTextColor(-16777216); // BLACK
text.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Nulla eu magna lorem, at posuere elit. Cum sociis natoque " +
"penatibus et magnis dis parturient montes, nascetur ridiculus " +
"mus. Donec accumsan, tortor dictum sodales euismod, erat felis " +
"molestie dui, tincidunt sagittis diam est quis quam. Maecenas " +
"pulvinar risus at turpis vestibulum vitae interdum ante facilisis. " +
"Duis sed justo sodales elit consequat varius pharetra vel purus. " +
"Phasellus sem ipsum, placerat a cursus a, pellentesque eu turpis. " +
"In posuere fermentum dui, quis sagittis tortor congue sed. " +
"Vestibulum accumsan purus et nulla aliquet tristique. " +
"arcu ac suscipit ullamcorper, ipsum nunc congue massa.");
// lets add more Object View
View itemView = inflater.inflate(R.layout.about, linear); // this is the layout
linear.addView(itemView);
linear.addView(text);
setContentView(linearOut);
堆栈追踪:
09-27 14:23:53.947: ERROR/AndroidRuntime(1672): FATAL EXCEPTION: main
09-27 14:23:53.947: ERROR/AndroidRuntime(1672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adunit.android/com.adunit.android.BannerIDs}: java.lang.NullPointerException
09-27 14:23:53.947: ERROR/AndroidRuntime(1672): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-27 14:23:53.947: ERROR/AndroidRuntime(1672): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-27 14:23:53.947: ERROR/AndroidRuntime(1672): at
我知道我这样做是错的,但有一种方法可以解决我的问题。
提前再次感谢