英文:
java.lang.RuntimeException: Unable to start activity in Androidstudio
问题
以下是翻译好的内容:
我写了一些代码与Firebase关联,因为我想为注册帐户构建一个数据库。但是当我运行“app”时,我发现了一些错误,我无法修复它,也不知道为什么会出现?而且它也阻止我在VM移动设备上打开应用程序。
希望您能帮助我,因为我已经尝试了许多互联网上的解决方案,但它们都不起作用。
1- MainActivity.java
package com.example.ksuers;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
EditText nameu;
EditText emailu;
EditText pass;
RadioButton user;
Button btnsign;
FirebaseDatabase rootNode;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameu = (EditText)findViewById(R.id.Uname);
emailu = (EditText)findViewById(R.id.uni_email);
pass = (EditText)findViewById(R.id.Upass);
user = (RadioButton)findViewById(R.id.StudentUser);
btnsign = (Button)findViewById(R.id.btn_sign);
btnsign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("users");
String name = nameu.getText().toString();
String uniemail = emailu.getText().toString();
String password = pass.getText().toString();
String type = user.getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, uniemail, password, type);
reference.setValue(helperClass);
}
});
}
}
2- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity"
android:background="@drawable/background">
<!-- TextInputLayout for name -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/Uname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:hint="Full name"
android:inputType="textEmailAddress"
android:selectAllOnFocus="true"
app:layout_constraintBottom_toTopOf="@+id/Upass"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.871">
</com.google.android.material.textfield.TextInputLayout>
<!-- TextInputLayout for password -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/Upass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:hint="Password"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="textPassword"
android:selectAllOnFocus="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.532">
</com.google.android.material.textfield.TextInputLayout>
<!-- More layout components... -->
</androidx.constraintlayout.widget.ConstraintLayout>
3- UserHelperClass.java
package com.example.ksuers;
public class UserHelperClass {
String name, uniemail, password, Type;
public UserHelperClass() {
}
public UserHelperClass(String name, String uniemail, String password, String type) {
this.name = name;
this.uniemail = uniemail;
this.password = password;
Type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUniemail() {
return uniemail;
}
public void setUniemail(String uniemail) {
this.uniemail = uniemail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
}
错误信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ksuers, PID: 7103
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ksuers/com.example.ksuers.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.google.android.material.textfield.TextInputLayout
...
Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.google.android.material.textfield.TextInputLayout
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.material.textfield.TextInputLayout" on path: DexPathList[[zip file "/data/app/com.example.ksuers-AD1q19UGUyfr67j28j6DBA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.ksuers-AD1q19UGUyfr67j28j6DBA==/lib/x86, /system/lib, /vendor/lib]]
...
以上是您提供的代码的翻译部分。如果您需要更多帮助解决错误,可以提供更多相关的信息。
英文:
I wrote code to associate it with Firebase, because i want to build a database for Register accounts.
but when i ( run "app" ) i find some error i can't fix it, and i dont know why show? And it prevents me from opening the app in the VM mobile as well.
I hope help me, as I have tried many solutions on the Internet, but they doesn't work.
1- MainActivity.java
package com.example.ksuers;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
//anuther video say TextInputLayout
EditText nameu;
EditText emailu;
EditText pass;
//Spinner college;
RadioButton user;
Button btnsign;
FirebaseDatabase rootNode;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameu=(EditText)findViewById(R.id.Uname);
emailu=(EditText)findViewById(R.id.uni_email);
pass=(EditText)findViewById(R.id.Upass);
//college=(Spinner)findViewById(R.id.Ucollege);
user=(RadioButton)findViewById(R.id.StudentUser);
//user=(RadioButton)findViewById(R.id.AdminUser);
btnsign=(Button)findViewById(R.id.btn_sign);
//to save data in firebase
btnsign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("users");
String name = nameu.getText().toString();
String uniemail = emailu.getText().toString();
String password = pass.getText().toString();
String type = user.getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, uniemail, password, type);
reference.setValue(helperClass);
}
});
}
}
2- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity"
android:background="@drawable/background">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/Uname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:hint="Full name"
android:inputType="textEmailAddress"
android:selectAllOnFocus="true"
app:layout_constraintBottom_toTopOf="@+id/Upass"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.871"
tools:ignore="MissingConstraints"
android:autofillHints="" >
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/Upass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:hint="Password"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="textPassword"
android:selectAllOnFocus="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.532"
tools:ignore="MissingConstraints" >
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/uni_email"
android:layout_width="363dp"
android:layout_height="50dp"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="University email"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.248"
android:importantForAutofill="no" >
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_sign"
android:layout_width="221dp"
android:layout_height="52dp"
android:layout_gravity="start"
android:layout_marginStart="48dp"
android:layout_marginEnd="48dp"
android:background="@drawable/signup"
android:enabled="false"
android:text="Sign in"
android:textColor="#fff"
android:transitionName="button_tran"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.815" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create an account"
android:textColor="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.128" />
<RadioButton
android:id="@+id/AdminUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Admin"
android:textColor="#041B70"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="54dp" />
<RadioButton
android:id="@+id/StudentUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="68dp"
android:layout_marginRight="68dp"
android:text="Student"
android:textColor="#041B70"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.609"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672" />
</androidx.constraintlayout.widget.ConstraintLayout>
3- UserHelperClass.java
package com.example.ksuers;
public class UserHelperClass {
String name, uniemail, password, Type;
public UserHelperClass() {
}
public UserHelperClass(String name, String uniemail, String password, String type) {
this.name = name;
this.uniemail = uniemail;
this.password = password;
Type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUniemail() {
return uniemail;
}
public void setUniemail(String uniemail) {
this.uniemail = uniemail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
}
And the Error is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ksuers, PID: 7103
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ksuers/com.example.ksuers.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.material.textfield.TextInputLayout" on path: DexPathList[[zip file "/data/app/com.example.ksuers-AD1q19UGUyfr67j28j6DBA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.ksuers-AD1q19UGUyfr67j28j6DBA==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.ksuers.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案1
得分: 1
你忘记覆盖 Material 主题。
将:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
改为:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
这应该可以解决问题。如果你想继续使用现有的主题,那么你需要为 Material 主题添加许多样式。请查阅文档网站获取更多信息。
英文:
You are missing overriding the Material Theme.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
This should solve the problem. If you want to stick to the existing theme, then you need to aadd a lot of styles for the Material Theme. Check the documentation website for more information.
答案2
得分: 0
请确保你正在使用 Material Theme
主题:
在 styles.xml
文件中:
<!-- Material Theme -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
英文:
make sure you are using Material Theme
:
in styles.xml
:
<!-- Material Theme -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
答案3
得分: 0
将以下内容添加到您的构建文件中作为依赖项:
implementation 'com.google.android.material:material:1.2.1'
英文:
Add this to your build file as a dependencies.
implementation 'com.google.android.material:material:1.2.1'
答案4
得分: 0
尝试启用分包(multidex),可能您已达到上限。
https://developer.android.com/studio/build/multidex
英文:
Try to enable multidex, maybe you have reached cap
https://developer.android.com/studio/build/multidex
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论