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

huangapple 未分类评论59阅读模式
英文:

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

问题

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

以下是Logcat中的错误信息:

  1. 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
  2. 2020-07-25 11:58:56.903 11494-11569/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
  3. 2020-07-25 11:58:57.356 11494-11494/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
  4. 2020-07-25 11:58:58.761 11494-11494/com.example.dotchat E/AndroidRuntime: FATAL EXCEPTION: main
  5. Process: com.example.dotchat, PID: 11494
  6. 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
  7. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
  8. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3033)
  9. ...
  10. Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
  11. at com.example.dotchat.LoginActivity.onCreate(LoginActivity.java:35)
  12. ...

这是您的 LoginActivity.java 文件:

  1. ...
  2. import androidx.appcompat.widget.Toolbar;
  3. ...
  4. public class LoginActivity extends AppCompatActivity {
  5. ...
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_login);
  10. Toolbar toolbar = findViewById(R.id.toolbar);
  11. setSupportActionBar(toolbar);
  12. ...
  13. }
  14. ...
  15. }

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

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

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

  1. <androidx.appcompat.widget.Toolbar
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:id="@+id/toolbar"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:background="#79D0D1D0"
  8. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  9. app:popupTheme="@style/MenuStyle">
  10. </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

  1. 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
  2. 2020-07-25 11:58:56.903 11494-11569/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
  3. 2020-07-25 11:58:57.356 11494-11494/com.example.dotchat E/Parcel: Reading a NULL string not supported here.
  4. 2020-07-25 11:58:58.761 11494-11494/com.example.dotchat E/AndroidRuntime: FATAL EXCEPTION: main
  5. Process: com.example.dotchat, PID: 11494
  6. 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
  7. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
  8. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3033)
  9. at android.app.ActivityThread.-wrap11(Unknown Source:0)
  10. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1747)
  11. at android.os.Handler.dispatchMessage(Handler.java:106)
  12. at android.os.Looper.loop(Looper.java:200)
  13. at android.app.ActivityThread.main(ActivityThread.java:6971)
  14. at java.lang.reflect.Method.invoke(Native Method)
  15. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
  16. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  17. Caused by: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
  18. at com.example.dotchat.LoginActivity.onCreate(LoginActivity.java:35)
  19. at android.app.Activity.performCreate(Activity.java:7225)
  20. at android.app.Activity.performCreate(Activity.java:7216)
  21. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
  22. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
  23. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3033)&#160;
  24. at android.app.ActivityThread.-wrap11(Unknown Source:0)&#160;
  25. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1747)&#160;
  26. at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
  27. at android.os.Looper.loop(Looper.java:200)&#160;
  28. at android.app.ActivityThread.main(ActivityThread.java:6971)&#160;
  29. at java.lang.reflect.Method.invoke(Native Method)&#160;
  30. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)&#160;
  31. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)&#160;

This is my Registeractivity.java file

  1. package com.example.dotchat;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9. import androidx.annotation.NonNull;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import androidx.appcompat.widget.Toolbar;
  12. import com.google.android.gms.tasks.OnCompleteListener;
  13. import com.google.android.gms.tasks.Task;
  14. import com.google.firebase.auth.AuthResult;
  15. import com.google.firebase.auth.FirebaseAuth;
  16. import com.google.firebase.auth.FirebaseUser;
  17. import com.google.firebase.database.DatabaseReference;
  18. import com.google.firebase.database.FirebaseDatabase;
  19. import java.util.HashMap;
  20. import java.util.Objects;
  21. public class RegisterActivity extends AppCompatActivity {
  22. EditText username, email, password;
  23. Button btn_register;
  24. FirebaseAuth auth;
  25. DatabaseReference reference;
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_register);
  30. Toolbar toolbar=findViewById(R.id.toolbar);
  31. setSupportActionBar(toolbar);
  32. Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
  33. username=findViewById(R.id.username);
  34. email=findViewById(R.id.email);
  35. password=findViewById(R.id.password);
  36. btn_register=findViewById(R.id.btn_register);
  37. auth=FirebaseAuth.getInstance();
  38. btn_register.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. String txt_username =username.getText().toString();
  42. String txt_email =email.getText().toString();
  43. String txt_password =password.getText().toString();
  44. if (TextUtils.isEmpty(txt_username) ||TextUtils.isEmpty(txt_email) ||TextUtils.isEmpty(txt_password)){
  45. Toast.makeText(RegisterActivity.this,&quot;All fields are required!&quot;,Toast.LENGTH_SHORT).show();
  46. } else if (txt_password.length()&lt;8){
  47. Toast.makeText(RegisterActivity.this,&quot;Password must be atleast 8 characters!&quot;,Toast.LENGTH_SHORT).show();
  48. } else {
  49. register(txt_username,txt_email,txt_password);
  50. }
  51. }
  52. });
  53. }
  54. private void register(final String username, String email, String password){
  55. auth.createUserWithEmailAndPassword(email,password)
  56. .addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
  57. @Override
  58. public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
  59. if (task.isComplete()) {
  60. FirebaseUser firebaseUser = auth.getCurrentUser();
  61. assert firebaseUser != null;
  62. String userid = firebaseUser.getUid();
  63. reference = FirebaseDatabase.getInstance().getReference(&quot;Users&quot;).child(userid);
  64. HashMap&lt;String,String&gt; hashMap=new HashMap&lt;&gt;();
  65. hashMap.put(&quot;id&quot;,userid);
  66. hashMap.put(&quot;username&quot;,username);
  67. hashMap.put(&quot;imageURL&quot;,&quot;default&quot;);
  68. reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
  69. @Override
  70. public void onComplete(@NonNull Task&lt;Void&gt; task) {
  71. if (task.isSuccessful()){
  72. Intent intent=new Intent(RegisterActivity.this,MainActivity.class);
  73. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  74. startActivity(intent);
  75. finish();
  76. }
  77. }
  78. });
  79. } else {
  80. Toast.makeText(RegisterActivity.this,&quot;Sorry! You can&#39;t register with this email or password&quot;,Toast.LENGTH_SHORT).show();
  81. }
  82. }
  83. });
  84. }
  85. }

This is LoginActivity.java file

  1. package com.example.dotchat;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9. import androidx.annotation.NonNull;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import androidx.appcompat.widget.Toolbar;
  12. import com.google.android.gms.tasks.OnCompleteListener;
  13. import com.google.android.gms.tasks.Task;
  14. import com.google.firebase.auth.AuthResult;
  15. import com.google.firebase.auth.FirebaseAuth;
  16. import java.util.Objects;
  17. //import android.widget.Toolbar
  18. public class LoginActivity extends AppCompatActivity {
  19. EditText email,password;
  20. Button btn_login;
  21. FirebaseAuth auth;
  22. @Override protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_login);
  25. Toolbar toolbar=findViewById(R.id.toolbar);
  26. setSupportActionBar(toolbar);
  27. Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
  28. auth=FirebaseAuth.getInstance();
  29. email=findViewById(R.id.email);
  30. password=findViewById(R.id.password);
  31. btn_login=findViewById(R.id.btn_login);
  32. btn_login.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. String txt_email=email.getText().toString();
  36. String txt_password=password.getText().toString();
  37. if(TextUtils.isEmpty(txt_email) || TextUtils.isEmpty(txt_password)){
  38. Toast.makeText(LoginActivity.this,&quot;All fields are required&quot;,Toast.LENGTH_SHORT).show();
  39. } else {
  40. auth.signInWithEmailAndPassword(txt_email,txt_password)
  41. .addOnCompleteListener(new OnCompleteListener&lt;AuthResult&gt;() {
  42. @Override
  43. public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
  44. if (task.isSuccessful()){
  45. Intent intent=new Intent(LoginActivity.this,MainActivity.class);
  46. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  47. startActivity(intent);
  48. finish();
  49. } else {
  50. Toast.makeText(LoginActivity.this,&quot;Authentication failed&quot;,Toast.LENGTH_SHORT).show();
  51. }
  52. }
  53. });
  54. }
  55. }
  56. });
  57. }
  58. }

This is bar_layout.xml file

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

This is gradle file

  1. apply plugin: &#39;com.android.application&#39;
  2. apply plugin: &#39;com.google.gms.google-services&#39;
  3. android {
  4. compileSdkVersion 29
  5. buildToolsVersion &quot;29.0.3&quot;
  6. defaultConfig {
  7. applicationId &quot;com.example.dotchat&quot;
  8. minSdkVersion 21
  9. targetSdkVersion 29
  10. versionCode 1
  11. versionName &quot;1.0&quot;
  12. testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
  13. }
  14. buildTypes {
  15. release {
  16. minifyEnabled false
  17. proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
  18. }
  19. }
  20. }
  21. dependencies {
  22. implementation fileTree(dir: &quot;libs&quot;, include: [&quot;*.jar&quot;])
  23. implementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
  24. implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
  25. implementation &#39;androidx.legacy:legacy-support-v4:1.0.0&#39;
  26. implementation &#39;com.google.android.material:material:1.1.0&#39;
  27. implementation &#39;com.google.firebase:firebase-auth:19.3.2&#39;
  28. implementation &#39;com.google.firebase:firebase-database:19.3.1&#39;
  29. implementation &#39;com.google.firebase:firebase-core:17.4.4&#39;
  30. implementation &#39;androidx.cardview:cardview:1.0.0&#39;
  31. implementation &#39;de.hdodenhof:circleimageview:2.2.0&#39;
  32. implementation &#39;com.github.bumptech.glide:glide:4.11.0&#39;
  33. implementation &#39;com.rengwuxian.materialedittext:library:2.1.4&#39;
  34. testImplementation &#39;junit:junit:4.13&#39;
  35. androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;
  36. androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.2.0&#39;
  37. }

This is the activity_login.xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:orientation=&quot;vertical&quot;
  6. android:background=&quot;@drawable/appbackground&quot;
  7. android:layout_width=&quot;match_parent&quot;
  8. android:layout_height=&quot;match_parent&quot;
  9. tools:context=&quot;.LoginActivity&quot;&gt;
  10. &lt;ImageView
  11. android:layout_width=&quot;145dp&quot;
  12. android:layout_height=&quot;131dp&quot;
  13. android:layout_gravity=&quot;center&quot;
  14. android:layout_marginLeft=&quot;130dp&quot;
  15. android:layout_marginTop=&quot;50dp&quot;
  16. android:src=&quot;@drawable/logotblack1&quot; /&gt;
  17. &lt;TextView
  18. android:layout_width=&quot;wrap_content&quot;
  19. android:layout_height=&quot;wrap_content&quot;
  20. android:paddingBottom=&quot;35dp&quot;
  21. android:layout_marginLeft=&quot;275dp&quot;
  22. android:layout_marginTop=&quot;133dp&quot;
  23. android:text=&quot;C h a t&quot;
  24. android:textSize=&quot;20dp&quot;
  25. android:textColor=&quot;#000&quot; /&gt;
  26. &lt;include
  27. android:id=&quot;@+id/toolbar&quot;
  28. layout=&quot;@layout/bar_layout&quot;
  29. android:layout_width=&quot;match_parent&quot;
  30. android:layout_height=&quot;37dp&quot; /&gt;
  31. &lt;LinearLayout
  32. android:layout_width=&quot;match_parent&quot;
  33. android:layout_height=&quot;wrap_content&quot;
  34. android:layout_below=&quot;@id/toolbar&quot;
  35. android:layout_marginTop=&quot;224dp&quot;
  36. android:gravity=&quot;center_horizontal&quot;
  37. android:orientation=&quot;vertical&quot;
  38. android:padding=&quot;16dp&quot;&gt;
  39. &lt;TextView
  40. android:layout_width=&quot;wrap_content&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:paddingBottom=&quot;35dp&quot;
  43. android:text=&quot;Login into your Account&quot;
  44. android:textColor=&quot;#000&quot;
  45. android:textSize=&quot;20dp&quot;
  46. /&gt;
  47. &lt;EditText
  48. android:id=&quot;@+id/email&quot;
  49. android:layout_width=&quot;378dp&quot;
  50. android:layout_height=&quot;50dp&quot;
  51. android:layout_marginTop=&quot;10dp&quot;
  52. android:background=&quot;@drawable/edit_round&quot;
  53. android:drawableRight=&quot;@drawable/gmail&quot;
  54. android:hint=&quot; Email&quot;
  55. android:textColorHint=&quot;#fff&quot;
  56. android:inputType=&quot;textEmailAddress&quot; /&gt;
  57. &lt;EditText
  58. android:id=&quot;@+id/password&quot;
  59. android:layout_width=&quot;378dp&quot;
  60. android:layout_height=&quot;50dp&quot;
  61. android:layout_marginTop=&quot;10dp&quot;
  62. android:background=&quot;@drawable/edit_round&quot;
  63. android:drawableRight=&quot;@drawable/psw&quot;
  64. android:ems=&quot;10&quot;
  65. android:hint=&quot; Password&quot;
  66. android:textColorHint=&quot;#fff&quot;
  67. android:inputType=&quot;textPassword&quot; /&gt;
  68. &lt;Button
  69. android:id=&quot;@+id/btn_login&quot;
  70. android:layout_width=&quot;125dp&quot;
  71. android:layout_height=&quot;wrap_content&quot;
  72. android:layout_marginTop=&quot;25dp&quot;
  73. android:background=&quot;@drawable/button_roundl&quot;
  74. android:text=&quot;Login&quot;
  75. android:textColor=&quot;#000&quot; /&gt;
  76. &lt;/LinearLayout&gt;
  77. &lt;/RelativeLayout&gt;

This is activity_main.xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;wrap_content&quot;
  7. android:orientation=&quot;vertical&quot;
  8. tools:context=&quot;.MainActivity&quot;&gt;
  9. &lt;com.google.android.material.appbar.AppBarLayout
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;&gt;
  12. &lt;androidx.appcompat.widget.Toolbar
  13. android:id=&quot;@+id/toolbar&quot;
  14. android:layout_width=&quot;match_parent&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:background=&quot;#fff&quot;
  17. android:theme=&quot;@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar&quot;
  18. app:popupTheme=&quot;@style/MenuStyle&quot;&gt;
  19. &lt;de.hdodenhof.circleimageview.CircleImageView
  20. android:layout_width=&quot;30dp&quot;
  21. android:layout_height=&quot;30dp&quot;
  22. android:id=&quot;@+id/profile_image&quot;
  23. /&gt;
  24. &lt;TextView
  25. android:layout_width=&quot;wrap_content&quot;
  26. android:layout_height=&quot;wrap_content&quot;
  27. android:layout_marginLeft=&quot;20dp&quot;
  28. android:text=&quot;Username&quot;
  29. android:textColor=&quot;#000&quot;
  30. android:textStyle=&quot;bold&quot;
  31. android:layout_marginStart=&quot;25dp&quot;/&gt;
  32. &lt;/androidx.appcompat.widget.Toolbar&gt;
  33. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  34. &lt;/LinearLayout&gt;

This is activity_register.xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:orientation=&quot;vertical&quot;
  6. android:background=&quot;@drawable/appbackground&quot;
  7. android:layout_width=&quot;match_parent&quot;
  8. android:layout_height=&quot;match_parent&quot;
  9. tools:context=&quot;.RegisterActivity&quot;&gt;
  10. &lt;ImageView
  11. android:layout_width=&quot;115dp&quot;
  12. android:layout_height=&quot;110dp&quot;
  13. android:layout_gravity=&quot;center&quot;
  14. android:layout_marginLeft=&quot;90dp&quot;
  15. android:layout_marginTop=&quot;100dp&quot;
  16. android:src=&quot;@drawable/logotblack1&quot; /&gt;
  17. &lt;TextView
  18. android:layout_width=&quot;wrap_content&quot;
  19. android:layout_height=&quot;wrap_content&quot;
  20. android:layout_marginLeft=&quot;200dp&quot;
  21. android:layout_marginTop=&quot;153dp&quot;
  22. android:paddingBottom=&quot;35dp&quot;
  23. android:text=&quot;C h a t&quot;
  24. android:textColor=&quot;#000&quot;
  25. android:textSize=&quot;30dp&quot; /&gt;
  26. &lt;include
  27. android:id=&quot;@+id/toolbar&quot;
  28. layout=&quot;@layout/bar_layout&quot;
  29. android:layout_width=&quot;match_parent&quot;
  30. android:layout_height=&quot;37dp&quot; /&gt;
  31. &lt;LinearLayout
  32. android:layout_width=&quot;match_parent&quot;
  33. android:layout_height=&quot;wrap_content&quot;
  34. android:layout_below=&quot;@id/toolbar&quot;
  35. android:layout_marginTop=&quot;278dp&quot;
  36. android:gravity=&quot;center_horizontal&quot;
  37. android:orientation=&quot;vertical&quot;
  38. android:padding=&quot;16dp&quot;&gt;
  39. &lt;TextView
  40. android:layout_width=&quot;wrap_content&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:paddingBottom=&quot;35dp&quot;
  43. android:text=&quot;Create a New Account&quot;
  44. android:textColor=&quot;#000&quot;
  45. android:textSize=&quot;25dp&quot;
  46. android:textStyle=&quot;bold&quot; /&gt;
  47. &lt;EditText
  48. android:id=&quot;@+id/username&quot;
  49. android:layout_width=&quot;279dp&quot;
  50. android:layout_height=&quot;50dp&quot;
  51. android:layout_marginTop=&quot;10dp&quot;
  52. android:background=&quot;@drawable/edit_round&quot;
  53. android:drawableRight=&quot;@drawable/usericon&quot;
  54. android:hint=&quot; Username&quot;
  55. android:textColorHint=&quot;#fff&quot;/&gt;
  56. &lt;EditText
  57. android:id=&quot;@+id/email&quot;
  58. android:layout_width=&quot;278dp&quot;
  59. android:layout_height=&quot;50dp&quot;
  60. android:layout_marginTop=&quot;10dp&quot;
  61. android:background=&quot;@drawable/edit_round&quot;
  62. android:drawableRight=&quot;@drawable/gmail&quot;
  63. android:hint=&quot; Email&quot;
  64. android:inputType=&quot;textEmailAddress&quot;
  65. android:textColorHint=&quot;#fff&quot;/&gt;
  66. &lt;EditText
  67. android:id=&quot;@+id/password&quot;
  68. android:layout_width=&quot;278dp&quot;
  69. android:layout_height=&quot;50dp&quot;
  70. android:layout_marginTop=&quot;10dp&quot;
  71. android:background=&quot;@drawable/edit_round&quot;
  72. android:drawableRight=&quot;@drawable/psw&quot;
  73. android:ems=&quot;10&quot;
  74. android:hint=&quot; Password&quot;
  75. android:inputType=&quot;textPassword&quot;
  76. android:textColorHint=&quot;#fff&quot;/&gt;
  77. &lt;Button
  78. android:layout_width=&quot;176dp&quot;
  79. android:layout_height=&quot;wrap_content&quot;
  80. android:id=&quot;@+id/btn_register&quot;
  81. android:layout_marginTop=&quot;25dp&quot;
  82. android:background=&quot;@drawable/button_round&quot;
  83. android:text=&quot;register now&quot;
  84. android:textColor=&quot;#000&quot; /&gt;
  85. &lt;/LinearLayout&gt;
  86. &lt;/RelativeLayout&gt;

This is activity_start.xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:background=&quot;@drawable/appbackground&quot;
  8. tools:context=&quot;.StartActivity&quot;&gt;
  9. &lt;ImageView
  10. android:layout_width=&quot;131dp&quot;
  11. android:layout_height=&quot;110dp&quot;
  12. android:layout_marginLeft=&quot;75dp&quot;
  13. android:layout_marginTop=&quot;295dp&quot;
  14. android:src=&quot;@drawable/logotblack1&quot; /&gt;
  15. &lt;ImageView
  16. android:layout_width=&quot;181dp&quot;
  17. android:layout_height=&quot;147dp&quot;
  18. android:layout_marginLeft=&quot;200dp&quot;
  19. android:layout_marginRight=&quot;75dp&quot;
  20. android:layout_marginTop=&quot;299dp&quot;
  21. android:src=&quot;@drawable/chatblack&quot; /&gt;
  22. &lt;Button
  23. android:id=&quot;@+id/login&quot;
  24. android:layout_width=&quot;125dp&quot;
  25. android:layout_height=&quot;wrap_content&quot;
  26. android:layout_marginTop=&quot;650dp&quot;
  27. android:layout_marginLeft=&quot;20dp&quot;
  28. android:background=&quot;@drawable/llogin&quot;
  29. android:text=&quot;Login&quot;
  30. android:textColor=&quot;#000&quot; /&gt;
  31. &lt;Button
  32. android:id=&quot;@+id/register&quot;
  33. android:layout_width=&quot;176dp&quot;
  34. android:layout_height=&quot;wrap_content&quot;
  35. android:layout_marginTop=&quot;650dp&quot;
  36. android:layout_marginLeft=&quot;193dp&quot;
  37. android:background=&quot;@drawable/llogin&quot;
  38. android:text=&quot;register now&quot;
  39. android:textColor=&quot;#000&quot; /&gt;
  40. &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"的属性,然后您的代码将可以正常运行。

  1. <style name="MenuStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
  2. <!-- 在这里编写您的属性 -->
  3. </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.

  1. &lt;style name=&quot;MenuStyle&quot; parent=&quot;ThemeOverlay.AppCompat.Dark.ActionBar&quot;&gt;
  2. &lt;!-- Write your attributes here --&gt;
  3. &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"

例如:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.dotchat">
  4. <application>
  5. <activity
  6. android:name=".LoginActivity"
  7. android:label="@string/activity_login"
  8. android:theme="@style/AppTheme.NoActionBar" />
  9. </application>
  10. </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:

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

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

发表评论

匿名网友

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

确定