android.widget.Toolbar 无法转换为 androidx.appcompat.widget.Toolbar

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

android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar

问题

应用程序在点击按钮时崩溃,并且根据Logcat错误信息中的日志有任何问题。

以下是Logcat中的错误信息:

2020-07-25 11:58:56.040 11494-11620/com.example.dotchat E/ActivityThread: Failed to find provider info for cn.teddymobile.free.anteater.den.provider
2020-07-25 11:58:56.903 11494-11569/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
2020-07-25 11:58:57.356 11494-11494/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
2020-07-25 11:58:58.761 11494-11494/com.example.dotchat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dotchat, PID: 11494
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dotchat/com.example.dotchat.LoginActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3033)
        ...
    Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at com.example.dotchat.LoginActivity.onCreate(LoginActivity.java:35)
        ...

这是您的 LoginActivity.java 文件:

...
import androidx.appcompat.widget.Toolbar;
...

public class LoginActivity extends AppCompatActivity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ...
    }
    ...
}

根据错误信息,问题似乎出在 LoginActivity.java 文件中的 onCreate 方法中。错误发生在以下行:

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

错误消息指出 android.widget.Toolbar 无法转换为 androidx.appcompat.widget.Toolbar。这可能是因为您在布局文件 bar_layout.xml 中定义的 Toolbar 使用了错误的导入。您应该在布局文件的根元素中使用正确的 Toolbar 类导入:

<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#79D0D1D0"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/MenuStyle">
</androidx.appcompat.widget.Toolbar>

确保在布局文件 activity_login.xmlactivity_register.xml 中,以及在 activity_main.xml 中正确使用了 androidx.appcompat.widget.Toolbar

同时,确保您的项目的 Gradle 配置文件中使用了适当的依赖,以确保使用的库版本是兼容的。

英文:

Applicaton crashes when click on the button and is the any problem in thne gradle file based on the logcat error.

Following is the logcat error

2020-07-25 11:58:56.040 11494-11620/com.example.dotchat E/ActivityThread: Failed to find provider info for cn.teddymobile.free.anteater.den.provider
2020-07-25 11:58:56.903 11494-11569/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
2020-07-25 11:58:57.356 11494-11494/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
2020-07-25 11:58:58.761 11494-11494/com.example.dotchat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dotchat, PID: 11494
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dotchat/com.example.dotchat.LoginActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3033)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1747)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:200)
        at android.app.ActivityThread.main(ActivityThread.java:6971)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
        at com.example.dotchat.LoginActivity.onCreate(LoginActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:7225)
        at android.app.Activity.performCreate(Activity.java:7216)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3033)&#160;
        at android.app.ActivityThread.-wrap11(Unknown Source:0)&#160;
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1747)&#160;
        at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
        at android.os.Looper.loop(Looper.java:200)&#160;
        at android.app.ActivityThread.main(ActivityThread.java:6971)&#160;
        at java.lang.reflect.Method.invoke(Native Method)&#160;
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)&#160;
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)&#160;

This is my Registeractivity.java file

package com.example.dotchat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;
import java.util.Objects;

public class RegisterActivity extends AppCompatActivity {

    EditText username, email, password;
    Button btn_register;

    FirebaseAuth auth;
    DatabaseReference reference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

       Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        


        username=findViewById(R.id.username);
        email=findViewById(R.id.email);
        password=findViewById(R.id.password);
        btn_register=findViewById(R.id.btn_register);

        auth=FirebaseAuth.getInstance();

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt_username =username.getText().toString();
                String txt_email =email.getText().toString();
                String txt_password =password.getText().toString();

                if (TextUtils.isEmpty(txt_username) ||TextUtils.isEmpty(txt_email) ||TextUtils.isEmpty(txt_password)){
                    Toast.makeText(RegisterActivity.this,&quot;All fields are required!&quot;,Toast.LENGTH_SHORT).show();
                } else if (txt_password.length()&lt;8){
                    Toast.makeText(RegisterActivity.this,&quot;Password must be atleast 8 characters!&quot;,Toast.LENGTH_SHORT).show();
                } else {
                    register(txt_username,txt_email,txt_password);
                }
            }
        });
    }

    private void register(final String username, String email, String password){

        auth.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
                    @Override
                    public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                        if (task.isComplete()) {
                            FirebaseUser firebaseUser = auth.getCurrentUser();
                            assert firebaseUser != null;
                            String userid = firebaseUser.getUid();

                            reference = FirebaseDatabase.getInstance().getReference(&quot;Users&quot;).child(userid);

                            HashMap&lt;String,String&gt; hashMap=new HashMap&lt;&gt;();
                            hashMap.put(&quot;id&quot;,userid);
                            hashMap.put(&quot;username&quot;,username);
                            hashMap.put(&quot;imageURL&quot;,&quot;default&quot;);

                            reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                                @Override
                                public void onComplete(@NonNull Task&lt;Void&gt; task) {
                                    if (task.isSuccessful()){
                                        Intent intent=new Intent(RegisterActivity.this,MainActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }

                                }
                            });
                        } else {
                            Toast.makeText(RegisterActivity.this,&quot;Sorry! You can&#39;t register with this email or password&quot;,Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}

This is LoginActivity.java file

package com.example.dotchat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

import java.util.Objects;

//import android.widget.Toolbar

public class LoginActivity extends AppCompatActivity {

    EditText  email,password;
    Button btn_login;

    FirebaseAuth auth;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


       Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        auth=FirebaseAuth.getInstance();

        email=findViewById(R.id.email);
        password=findViewById(R.id.password);
        btn_login=findViewById(R.id.btn_login);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt_email=email.getText().toString();
                String txt_password=password.getText().toString();

                if(TextUtils.isEmpty(txt_email) || TextUtils.isEmpty(txt_password)){
                    Toast.makeText(LoginActivity.this,&quot;All fields are required&quot;,Toast.LENGTH_SHORT).show();
                } else {
                    auth.signInWithEmailAndPassword(txt_email,txt_password)
                            .addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
                                @Override
                                public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                                    if (task.isSuccessful()){
                                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        Toast.makeText(LoginActivity.this,&quot;Authentication failed&quot;,Toast.LENGTH_SHORT).show();
                                    }

                                }
                            });
                }
            }
        });

    }
}

This is bar_layout.xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.appcompat.widget.Toolbar xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:id=&quot;@+id/toolbar&quot;
    android:background=&quot;#79D0D1D0&quot;
    android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark.ActionBar&quot;
    app:popupTheme=&quot;@style/MenuStyle&quot;&gt;

&lt;/androidx.appcompat.widget.Toolbar&gt;

This is gradle file

apply plugin: &#39;com.android.application&#39;
apply plugin: &#39;com.google.gms.google-services&#39;

android {
    compileSdkVersion 29
    buildToolsVersion &quot;29.0.3&quot;

    defaultConfig {
        applicationId &quot;com.example.dotchat&quot;
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName &quot;1.0&quot;

        testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
        }
    }
}

dependencies {
    implementation fileTree(dir: &quot;libs&quot;, include: [&quot;*.jar&quot;])
    implementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
    implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
    implementation &#39;androidx.legacy:legacy-support-v4:1.0.0&#39;
    implementation &#39;com.google.android.material:material:1.1.0&#39;
    implementation &#39;com.google.firebase:firebase-auth:19.3.2&#39;
    implementation &#39;com.google.firebase:firebase-database:19.3.1&#39;
    implementation &#39;com.google.firebase:firebase-core:17.4.4&#39;
    implementation &#39;androidx.cardview:cardview:1.0.0&#39;
    implementation &#39;de.hdodenhof:circleimageview:2.2.0&#39;
    implementation &#39;com.github.bumptech.glide:glide:4.11.0&#39;
    implementation &#39;com.rengwuxian.materialedittext:library:2.1.4&#39;
    testImplementation &#39;junit:junit:4.13&#39;
    androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;
    androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.2.0&#39;

}

This is the activity_login.xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout 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:orientation=&quot;vertical&quot;
    android:background=&quot;@drawable/appbackground&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.LoginActivity&quot;&gt;


    &lt;ImageView
        android:layout_width=&quot;145dp&quot;
        android:layout_height=&quot;131dp&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_marginLeft=&quot;130dp&quot;
        android:layout_marginTop=&quot;50dp&quot;
        android:src=&quot;@drawable/logotblack1&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:paddingBottom=&quot;35dp&quot;
        android:layout_marginLeft=&quot;275dp&quot;
        android:layout_marginTop=&quot;133dp&quot;
        android:text=&quot;C h a t&quot;
        android:textSize=&quot;20dp&quot;
        android:textColor=&quot;#000&quot; /&gt;

    &lt;include
        android:id=&quot;@+id/toolbar&quot;
        layout=&quot;@layout/bar_layout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;37dp&quot; /&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/toolbar&quot;
        android:layout_marginTop=&quot;224dp&quot;
        android:gravity=&quot;center_horizontal&quot;
        android:orientation=&quot;vertical&quot;
        android:padding=&quot;16dp&quot;&gt;

        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:paddingBottom=&quot;35dp&quot;
            android:text=&quot;Login into your Account&quot;
            android:textColor=&quot;#000&quot;
            android:textSize=&quot;20dp&quot;
            /&gt;

        &lt;EditText
            android:id=&quot;@+id/email&quot;
            android:layout_width=&quot;378dp&quot;
            android:layout_height=&quot;50dp&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:background=&quot;@drawable/edit_round&quot;
            android:drawableRight=&quot;@drawable/gmail&quot;
            android:hint=&quot;                                       Email&quot;
            android:textColorHint=&quot;#fff&quot;
            android:inputType=&quot;textEmailAddress&quot; /&gt;

        &lt;EditText
            android:id=&quot;@+id/password&quot;
            android:layout_width=&quot;378dp&quot;
            android:layout_height=&quot;50dp&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:background=&quot;@drawable/edit_round&quot;
            android:drawableRight=&quot;@drawable/psw&quot;
            android:ems=&quot;10&quot;
            android:hint=&quot;                                    Password&quot;
            android:textColorHint=&quot;#fff&quot;
            android:inputType=&quot;textPassword&quot; /&gt;

        &lt;Button
            android:id=&quot;@+id/btn_login&quot;
            android:layout_width=&quot;125dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;25dp&quot;
            android:background=&quot;@drawable/button_roundl&quot;
            android:text=&quot;Login&quot;
            android:textColor=&quot;#000&quot; /&gt;

    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;

This is activity_main.xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout 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;wrap_content&quot;
    android:orientation=&quot;vertical&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;

        &lt;androidx.appcompat.widget.Toolbar
            android:id=&quot;@+id/toolbar&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:background=&quot;#fff&quot;
            android:theme=&quot;@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar&quot;
            app:popupTheme=&quot;@style/MenuStyle&quot;&gt;

            &lt;de.hdodenhof.circleimageview.CircleImageView
                android:layout_width=&quot;30dp&quot;
                android:layout_height=&quot;30dp&quot;
                android:id=&quot;@+id/profile_image&quot;
                /&gt;
            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginLeft=&quot;20dp&quot;
                android:text=&quot;Username&quot;
                android:textColor=&quot;#000&quot;
                android:textStyle=&quot;bold&quot;
                android:layout_marginStart=&quot;25dp&quot;/&gt;

        &lt;/androidx.appcompat.widget.Toolbar&gt;
    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

&lt;/LinearLayout&gt;

This is activity_register.xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout 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:orientation=&quot;vertical&quot;
    android:background=&quot;@drawable/appbackground&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.RegisterActivity&quot;&gt;


    &lt;ImageView
        android:layout_width=&quot;115dp&quot;
        android:layout_height=&quot;110dp&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_marginLeft=&quot;90dp&quot;
        android:layout_marginTop=&quot;100dp&quot;
        android:src=&quot;@drawable/logotblack1&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginLeft=&quot;200dp&quot;
        android:layout_marginTop=&quot;153dp&quot;
        android:paddingBottom=&quot;35dp&quot;
        android:text=&quot;C h a t&quot;
        android:textColor=&quot;#000&quot;
        android:textSize=&quot;30dp&quot; /&gt;

    &lt;include
        android:id=&quot;@+id/toolbar&quot;
        layout=&quot;@layout/bar_layout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;37dp&quot; /&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/toolbar&quot;
        android:layout_marginTop=&quot;278dp&quot;
        android:gravity=&quot;center_horizontal&quot;
        android:orientation=&quot;vertical&quot;
        android:padding=&quot;16dp&quot;&gt;

        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:paddingBottom=&quot;35dp&quot;
            android:text=&quot;Create a New Account&quot;
            android:textColor=&quot;#000&quot;
            android:textSize=&quot;25dp&quot;
            android:textStyle=&quot;bold&quot; /&gt;

        &lt;EditText

            android:id=&quot;@+id/username&quot;
            android:layout_width=&quot;279dp&quot;
            android:layout_height=&quot;50dp&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:background=&quot;@drawable/edit_round&quot;
            android:drawableRight=&quot;@drawable/usericon&quot;
            android:hint=&quot;                      Username&quot;
            android:textColorHint=&quot;#fff&quot;/&gt;

        &lt;EditText
            android:id=&quot;@+id/email&quot;
            android:layout_width=&quot;278dp&quot;
            android:layout_height=&quot;50dp&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:background=&quot;@drawable/edit_round&quot;
            android:drawableRight=&quot;@drawable/gmail&quot;
            android:hint=&quot;                          Email&quot;
            android:inputType=&quot;textEmailAddress&quot;
            android:textColorHint=&quot;#fff&quot;/&gt;

        &lt;EditText
            android:id=&quot;@+id/password&quot;
            android:layout_width=&quot;278dp&quot;
            android:layout_height=&quot;50dp&quot;
            android:layout_marginTop=&quot;10dp&quot;
            android:background=&quot;@drawable/edit_round&quot;
            android:drawableRight=&quot;@drawable/psw&quot;
            android:ems=&quot;10&quot;
            android:hint=&quot;                       Password&quot;
            android:inputType=&quot;textPassword&quot;
            android:textColorHint=&quot;#fff&quot;/&gt;

        &lt;Button
            android:layout_width=&quot;176dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:id=&quot;@+id/btn_register&quot;
            android:layout_marginTop=&quot;25dp&quot;
            android:background=&quot;@drawable/button_round&quot;
            android:text=&quot;register now&quot;
            android:textColor=&quot;#000&quot; /&gt;

    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;

This is activity_start.xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout 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;
    android:background=&quot;@drawable/appbackground&quot;
    tools:context=&quot;.StartActivity&quot;&gt;
    &lt;ImageView
        android:layout_width=&quot;131dp&quot;
        android:layout_height=&quot;110dp&quot;
        android:layout_marginLeft=&quot;75dp&quot;
        android:layout_marginTop=&quot;295dp&quot;
        android:src=&quot;@drawable/logotblack1&quot; /&gt;

    &lt;ImageView
        android:layout_width=&quot;181dp&quot;
        android:layout_height=&quot;147dp&quot;
        android:layout_marginLeft=&quot;200dp&quot;
        android:layout_marginRight=&quot;75dp&quot;
        android:layout_marginTop=&quot;299dp&quot;
        android:src=&quot;@drawable/chatblack&quot; /&gt;




        &lt;Button
            android:id=&quot;@+id/login&quot;
            android:layout_width=&quot;125dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;650dp&quot;
            android:layout_marginLeft=&quot;20dp&quot;
            android:background=&quot;@drawable/llogin&quot;
            android:text=&quot;Login&quot;
            android:textColor=&quot;#000&quot; /&gt;

        &lt;Button
            android:id=&quot;@+id/register&quot;
            android:layout_width=&quot;176dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;650dp&quot;
            android:layout_marginLeft=&quot;193dp&quot;
            android:background=&quot;@drawable/llogin&quot;
            android:text=&quot;register now&quot;
            android:textColor=&quot;#000&quot; /&gt;

&lt;/RelativeLayout&gt;

答案1

得分: 2

在您的 XML 文件中定义了错误的 Toolbar 类。请将其从以下内容进行更改:


&lt;Toolbar .../&gt;

改为

&lt;androidx.appcompat.widget.Toolbar .../&gt;

在您的 XML 中,您可能仅使用了&lt;Toolbar&gt;&lt;/Toolbar&gt;来声明工具栏,在这种情况下,工具栏将从android.widget包中创建。因此,如果您尝试通过将其强制转换为androidx.appcompat.widget.Toolbar并调用findViewById,肯定会抛出RuntimeException

如果您正在使用应该使用的 AndroidX,那么您必须将工具栏的 XML 声明更改为&lt;androidx.appcompat.widget.Toolbar&gt;&lt;/androidx.appcompat.widget.Toolbar&gt;

然后,您可以继续调用(androidx.appcompat.widget.Toolbar)的findViewById(..),这应该会成功。

英文:

Wrong Toolbar class defined in your xml file. Change it from

&lt;Toolbar .../&gt;

to

&lt;androidx.appcompat.widget.Toolbar .../&gt;

In your XML, you probably declared your toolbar using just <Toolbar></Toolbar> in that case, the toolbar will be created from the package android.widget. So if you try to call findViewById by casting it to androidx.appcompat.widget.Toolbar it will surely throw you a RuntimeException.

If you are using AndroidX, which you should, then you have to change the xml declaration of your toolbar to <androidx.appcompat.widget.Toolbar></androidx.appcompat.widget.Toolbar>

You can then proceed to call your (androidx.appcompat.widget.Toolbar) findViewByid(..) which should succeed

答案2

得分: 0

在bar_layout文件中,我认为您放入了在Android中未定义的错误弹出菜单样式。

为此,您需要在样式文件夹中创建一个名为"MenuStyle"的属性,然后您的代码将可以正常运行。

<style name="MenuStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <!-- 在这里编写您的属性 -->
</style>

或者,您可以选择使用Android中预定义的其他样式属性。

希望对您有所帮助 : )。

英文:

In the bar_layout file, I think you have put the wrong popup menu style that is not defined in android.

For this, you have to create a MenuStyle attribute in style folder, then you code run perfectly.

&lt;style name=&quot;MenuStyle&quot; parent=&quot;ThemeOverlay.AppCompat.Dark.ActionBar&quot;&gt;
    &lt;!--  Write your attributes here --&gt;
&lt;/style&gt;

or you can prefer other style attributes that are pre-defined in android.

Hope it helps ).

答案3

得分: 0

刚刚遇到了相同的问题。
除了按照Moaz Ragab的回答进行操作外,我还建议检查AndroidManifest.xml文件,确保对应的活动(在您的情况下是LoginActivity)具有android:theme="style/AppTheme.NoActionBar"

例如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dotchat">
    <application>
        <activity
            android:name=".LoginActivity"
            android:label="@string/activity_login"
            android:theme="@style/AppTheme.NoActionBar" />
    </application>
</manifest>
英文:

Just came across the same issue.
Besides following Moaz Ragab answer, I'd also suggest to check AndroidManifest.xml file and make sure you have android:theme="@style/AppTheme.NoActionBar" for the corresponding activity, which, in your case, is LoginActivity.

For example:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.dotchat&quot;&gt;
    &lt;application&gt;
        &lt;activity
            android:name=&quot;.LoginActivity&quot;
            android:label=&quot;@string/activity_login&quot;
            android:theme=&quot;@style/AppTheme.NoActionBar&quot; /&gt;
    &lt;/application&gt;
&lt;/manifest&gt;

huangapple
  • 本文由 发表于 2020年7月25日 14:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085289.html
匿名

发表评论

匿名网友

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

确定