我正在尝试创建一个AlarmClock应用程序,但是,我遇到了一个问题。单击某个按钮(添加警报)时会出现此问题。该按钮执行以下操作:
public void addAlarm_Click(View v) {
AlertDialog.Builder myBuilder = new AlertDialog.Builder(MainActivity.this);
View myView = getLayoutInflater().inflate(R.layout.dialog_addalarm, null);
final TimePicker MyTimePicker = (TimePicker) myView.findViewById(R.id.timePicker);
MyTimePicker.setIs24HourView(true);
myBuilder.setView(myView);
final AlertDialog dialog = myBuilder.create();
Button btnAdd = (Button) myView.findViewById(R.id.dialog_AddAlarm_Btn_Add);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int h = MyTimePicker.getCurrentHour();
int m = MyTimePicker.getCurrentMinute();
Toast.makeText(MainActivity.this, "" + Integer.toString(h) + " " + Integer.toString(m),Toast.LENGTH_SHORT).show();
}
});
Button btnCancel = (Button) myView.findViewById(R.id.dialog_AddAlarm_Btn_Cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
//Toast.makeText(MainActivity.this, "Cancel Clicked", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
这是我的 dialog_addalarm.xml :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/Layout_MainHolder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/Layout_NameHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<TextView
android:id="@+id/TextView_Name"
android:labelFor="@+id/EditText_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/alarm_name" />
<EditText
android:id="@+id/EditText_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<TimePicker
android:id="@+id/timePicker"
android:timePickerMode="spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:headerBackground="@android:color/background_dark"
android:numbersSelectorColor="@android:color/background_dark" />
<LinearLayout
android:id="@+id/daypicker_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<include
android:id="@+id/daypicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/daypicker" />
</LinearLayout>
<LinearLayout
android:id="@+id/Layout_ButtonHolder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="@+id/dialog_AddAlarm_Btn_Cancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cancel" />
<Button
android:id="@+id/dialog_AddAlarm_Btn_Add"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add" />
</LinearLayout>
</LinearLayout>
以下是崩溃日志:
FATAL EXCEPTION: main
Process: com.example.alarmclock, PID: 31230
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4793)
at android.view.View$PerformClick.run(View.java:19959)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5596)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4793)
at android.view.View$PerformClick.run(View.java:19959)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5596)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:882)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:523)
at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
at android.view.LayoutInflater.inflate(LayoutInflater.java:368)
at com.example.persson.peter.alarmclock.MainActivity.addAlarm_Click(MainActivity.java:38)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4793)
at android.view.View$PerformClick.run(View.java:19959)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5596)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
09-12 11:25:33.774 31230-31230/com.example.alarmclock I/Process: Sending
我做了一些更改,当它突然开始表现得像这样(当我点击添加警报按钮时崩溃。)我不知道哪些更改确实导致了这个问题。
答案 0 :(得分:2)
崩溃是非常自我解释的。
You must specifiy a layout in the include tag: <include layout="@layout/layoutID" />
您想要的限定符是layout=""
不是 android:layout="
。
有关详细信息,请参阅此处:https://developer.android.com/training/improving-layouts/reusing-layouts.html#Include
答案 1 :(得分:1)
找到解决方案:
而不是 android:layout 使用布局:
<include
android:id="@+id/daypicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/daypicker" /> // check this line
答案 2 :(得分:0)
在对话框显示事件之前设置视图。
{{1}}
此处需要代码解释和调试代码。