使用findViewById
初始化变量时,我的Android活动崩溃了public class RaportGreutateActivity extends AppCompatActivity {
EditText etDate;
EditText etGreutate;
Button addValues;
DatabaseReference dr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_raport_greutate);
init();
}
public void init() {
etDate = (EditText) findViewById(R.id.etDataRaport);
etGreutate = (EditText) findViewById(R.id.etRaportGreutate);
addValues = (Button) findViewById(R.id.btnAdaugaraport);
dr = FirebaseDatabase.getInstance().getReference("raport");
addValues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Add();
}
});
}
这就是我的代码。
如果我评论init();从onCreate开始,我的活动开启得很好。
顺便说一句,这不是由于Add();函数,我试图在onCreate方法上手动初始化etDate,但结果仍然相同,所以问题出在初始化的地方。
我第一次尝试使用firebase实时数据库。
谢谢!
编辑:
这里的堆栈跟踪
01-03 22:21:45.482 12237-12237/com.example.dani.zamfireduard_1076 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dani.zamfireduard_1076, PID: 12237
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dani.zamfireduard_1076/com.example.dani.zamfireduard_1076.RaportGreutateActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
at com.example.dani.zamfireduard_1076.RaportGreutateActivity.init(RaportGreutateActivity.java:43)
at com.example.dani.zamfireduard_1076.RaportGreutateActivity.onCreate(RaportGreutateActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
XML
<TextView
android:id="@+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="28dp"
android:text="@string/Data" />
<EditText
android:id="@+id/etDataRaport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvData"
android:layout_alignBottom="@+id/tvData"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="@string/Varsta"
android:inputType="date" />
<EditText
android:id="@+id/etRaportGreutate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/etDataRaport"
android:layout_alignRight="@+id/etDataRaport"
android:layout_alignTop="@+id/btnAdaugaraport"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/etRaportGreutate"
android:layout_alignLeft="@+id/tvData"
android:layout_alignStart="@+id/tvData"
android:layout_marginBottom="11dp"
android:text="@string/Greutate" />
答案 0 :(得分:0)
String title = driver.findElement(By.cssSelector("#firstHeading")).getText();
if (title.contentEquals("Selenium (software)"))
System.out.println("Test Passed!");
else
System.out.println("Test Failed!");
这告诉您问题是您正在投射相对布局来编辑文本。
检查java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
和etDataRaport
的XML。