-1
我正在嘗試創建一個自定義容器,該容器從包含幾個按鈕,編輯文本等的線性佈局延伸,可以通過編程方式添加到我的應用程序中。到目前爲止,我可以加我的自定義佈局以編程方式使用LayoutInflater,但是當我試圖在上點擊監聽器設置爲包含在佈局內的按鈕,我的應用程序崩潰,在啓動時Set on Click Listener for custom view崩潰應用程序
這裏每次都是我的簡化代碼:
exercise_entry_layout.xml,這是該視圖的佈局:
<com.mdmil.app.ExerciseEntry xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</com.mdmil.app.ExerciseEntry>
ExerciseEntry.java,java類
public class ExerciseEntry extends LinearLayout
{
public ExerciseEntry(Context context) {
this(context, null, 0);
}
public ExerciseEntry(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ExerciseEntry(final Context context, @Nullable AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
Button button = findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// stuff
}
});
}
}
這就是我如何MainActivity.java視圖添加到應用程序:
View entry = LayoutInflater.from(this).inflate(R.layout.exercise_entry_layout, mConstraintLayout, false);
mConstraintLayout.addView(entry);
的logcat的輸出是這樣的:
08-14 14:29:14.422 20918-20918/? I/zygote: Not late-enabling -Xcheck:jni (already on)
08-14 14:29:14.451 20918-20918/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
08-14 14:29:14.755 20918-20918/com.mdmil.app I/InstantRun: starting instant run server: is main process
08-14 14:29:15.028 20918-20918/com.mdmil.app D/AndroidRuntime: Shutting down VM
08-14 14:29:15.035 20918-20918/com.mdmil.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mdmil.app, PID: 20918
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mdmil.app/com.mdmil.app.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.mdmil.app.ExerciseEntry
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class com.mdmil.app.ExerciseEntry
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.mdmil.app.ExerciseEntry
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:650)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:793)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.mdmil.app.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6980)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.mdmil.app.ExerciseEntry.<init>(ExerciseEntry.java:56)
at com.mdmil.app.ExerciseEntry.<init>(ExerciseEntry.java:0)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:650)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:793)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.mdmil.app.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6980)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
08-14 14:29:15.061 20918-20925/com.mdmil.app W/zygote: Suspending all threads took: 17.310ms
我是適度的新到Android,等我實施任何反饋是受歡迎的。 謝謝!
發佈完整的logcat輸出,以便我們可以看到錯誤以及發生了什麼行。 「_my app crashes_」沒有任何用處。 – csmckelvey
logcat輸出張貼在我的編輯:) – mdmiller002
'對null對象引用'引發:java.lang.NullPointerException:試圖調用虛擬方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)' '看起來'findViewById(R.id.button);'返回null。 – csmckelvey