我一直致力于创建可变数量的自定义视图,但我无法创建任何不会导致应用程序强制停止的内容。我查看了几个不同的资源,但一直找不到任何帮助。对不起,如果这很明显,我对此有点新鲜。
我的主要活动代码
package com.example.customview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.example.customview.widget.DoubleImageView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearlayout = (LinearLayout) findViewById(R.id.linear_layout);
DoubleImageView doubleimageview;
for (int i =0; i <5; i++) {
doubleimageview = new DoubleImageView(this);
linearlayout.addView(doubleimageview);
}
}
}
我的Double_image.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<com.example.customview.widget.DoubleImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/flag_us"
android:drawableRight="@drawable/flag_uk"
android:textColor="#FFF"
android:textSize="32sp"
android:text="@string/score"
android:spacing="15dp"/>
</LinearLayout>
</ScrollView>
对我而言,这应该可以将double_image视图添加到可滚动的线性布局中,我不确定缺少什么。
这是我的错误日志
11-05 20:43:58.552: E/AndroidRuntime(29844): FATAL EXCEPTION: main
11-05 20:43:58.552: E/AndroidRuntime(29844): Process: com.example.customview, PID: 29844
11-05 20:43:58.552: E/AndroidRuntime(29844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customview/com.example.customview.MainActivity}: java.lang.NullPointerException
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.os.Looper.loop(Looper.java:136)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.ActivityThread.main(ActivityThread.java:5105)
11-05 20:43:58.552: E/AndroidRuntime(29844): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 20:43:58.552: E/AndroidRuntime(29844): at java.lang.reflect.Method.invoke(Method.java:515)
11-05 20:43:58.552: E/AndroidRuntime(29844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
11-05 20:43:58.552: E/AndroidRuntime(29844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
11-05 20:43:58.552: E/AndroidRuntime(29844): at dalvik.system.NativeStart.main(Native Method)
11-05 20:43:58.552: E/AndroidRuntime(29844): Caused by: java.lang.NullPointerException
11-05 20:43:58.552: E/AndroidRuntime(29844): at com.example.customview.MainActivity.onCreate(MainActivity.java:40)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.Activity.performCreate(Activity.java:5275)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-05 20:43:58.552: E/AndroidRuntime(29844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
11-05 20:43:58.552: E/AndroidRuntime(29844): ... 11 more
&#13;
答案 0 :(得分:0)
您没有在setContentView()
中调用onCreate()
,这意味着findViewById()
肯定会返回null。方法应该是这样的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.double_image);
LinearLayout linearlayout = (LinearLayout) findViewById(R.id.linear_layout);
...