java.lang.RuntimeException: 无法在Android Studio中启动活动

huangapple go评论70阅读模式
英文:

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(&quot;users&quot;);
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

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!--suppress ALL --&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;
android:background=&quot;@drawable/background&quot;&gt;
&lt;com.google.android.material.textfield.TextInputLayout
android:id=&quot;@+id/Uname&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;24dp&quot;
android:layout_marginEnd=&quot;24dp&quot;
android:hint=&quot;Full name&quot;
android:inputType=&quot;textEmailAddress&quot;
android:selectAllOnFocus=&quot;true&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/Upass&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.871&quot;
tools:ignore=&quot;MissingConstraints&quot;
android:autofillHints=&quot;&quot; &gt;
&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;com.google.android.material.textfield.TextInputLayout
android:id=&quot;@+id/Upass&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;24dp&quot;
android:layout_marginEnd=&quot;24dp&quot;
android:hint=&quot;Password&quot;
android:imeOptions=&quot;actionDone&quot;
android:importantForAutofill=&quot;no&quot;
android:inputType=&quot;textPassword&quot;
android:selectAllOnFocus=&quot;true&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.532&quot;
tools:ignore=&quot;MissingConstraints&quot; &gt;
&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;com.google.android.material.textfield.TextInputLayout
android:id=&quot;@+id/uni_email&quot;
android:layout_width=&quot;363dp&quot;
android:layout_height=&quot;50dp&quot;
android:layout_marginTop=&quot;28dp&quot;
android:ems=&quot;10&quot;
android:hint=&quot;University email&quot;
android:inputType=&quot;textEmailAddress&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.248&quot;
android:importantForAutofill=&quot;no&quot; &gt;
&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;Button
android:id=&quot;@+id/btn_sign&quot;
android:layout_width=&quot;221dp&quot;
android:layout_height=&quot;52dp&quot;
android:layout_gravity=&quot;start&quot;
android:layout_marginStart=&quot;48dp&quot;
android:layout_marginEnd=&quot;48dp&quot;
android:background=&quot;@drawable/signup&quot;
android:enabled=&quot;false&quot;
android:text=&quot;Sign in&quot;
android:textColor=&quot;#fff&quot;
android:transitionName=&quot;button_tran&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.815&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Create an account&quot;
android:textColor=&quot;#FFFFFF&quot;
android:textSize=&quot;24sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.497&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.128&quot; /&gt;
&lt;RadioButton
android:id=&quot;@+id/AdminUser&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Admin&quot;
android:textColor=&quot;#041B70&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.672&quot;
tools:ignore=&quot;MissingConstraints&quot;
tools:layout_editor_absoluteX=&quot;54dp&quot; /&gt;
&lt;RadioButton
android:id=&quot;@+id/StudentUser&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;68dp&quot;
android:layout_marginRight=&quot;68dp&quot;
android:text=&quot;Student&quot;
android:textColor=&quot;#041B70&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.609&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.672&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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&#39;t find class &quot;com.google.android.material.textfield.TextInputLayout&quot; on path: DexPathList[[zip file &quot;/data/app/com.example.ksuers-AD1q19UGUyfr67j28j6DBA==/base.apk&quot;],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.

&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.Light.DarkActionBar&quot;&gt;

to

&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.Light.DarkActionBar&quot;&gt;

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:

&lt;!-- Material Theme --&gt; 
&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.Light.DarkActionBar&quot;&gt; 
    &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
    &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
&lt;/style&gt;

答案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

huangapple
  • 本文由 发表于 2020年9月24日 19:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64045457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定