我是Android开发的新手。请帮我识别代码中的问题。我的代码进入了InflateException。 请帮帮我。
我的代码存在与MaterialEditText类相关的错误,我已经搜索了解决方案,但到目前为止还没有找到可行的解决方案。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_vertical"
tools:context="com.android.appgallery.quizworld.MainActivity">
<ImageView
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/wrap_login"
/>
<RelativeLayout
android:id="@+id/wrap_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="8dp"
>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/info_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:com.rengwuxian.materialedittext="http://schemas.android.com/tools"
android:layout_margin="8dp"
app:cardElevation="4dp">
<LinearLayout
android:padding="16dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.rengwuxian.materialedittext:MaterialEditText
android:id="@+id/edtUser"
android:hint="User Name"
android:textColorHint="@color/colorPrimary"
android:textColor="@color/colorPrimary"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:met_baseColor="@color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="@color/colorPrimary"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext:MaterialEditText
android:id="@+id/edtPassword"
android:hint="Password"
android:textColorHint="@color/colorPrimary"
android:textColor="@color/colorPrimary"
android:textSize="24sp"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:met_baseColor="@color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="@color/colorPrimary"
app:met_singleLineEllipsis="true"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_below="@id/info_login"
android:orientation="horizontal"
android:weightSum="2"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_sign_up"
android:text="Sign Up"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_sign_in"
android:text="Sign In"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyButton" parent="Theme.AppCompat.Light">
<item name="colorControlHighlight">#1CB3BC</item>
</style>
</resources>
MainActivity
package com.android.appgallery.quizworld;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.android.appgallery.quizworld.BroadcastReceiver.AlarmReceiver;
import com.android.appgallery.quizworld.Common.Common;
import com.android.appgallery.quizworld.Model.User;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rengwuxian.materialedittext.MaterialEditText;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
MaterialEditText edtNewUser,edtNewPassword,edtNewEmail; //For Sign Up
MaterialEditText edtUser,edtPassword; //For Login
Button btnSignUp,btnSignIn;
FirebaseDatabase database;
DatabaseReference users;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
try {
setContentView(R.layout.activity_main);
}catch(Exception e){
//InflateException ex=new InflateException(parser.getPositionDescription()+" "+e.getMessage());
InflateException ex=new InflateException(e.getMessage());
ex.initCause(e);
throw ex;
}
registerAlarm();
database= FirebaseDatabase.getInstance();
users=database.getReference("Users");
edtUser=(MaterialEditText) findViewById(R.id.edtUser);
edtPassword=(MaterialEditText)findViewById(R.id.edtPassword);
btnSignIn= (Button) findViewById(R.id.btn_sign_in);
btnSignUp= (Button) findViewById(R.id.btn_sign_up);
btnSignUp.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
showSignUpDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
signIn(edtUser.getText().toString(),edtPassword.getText().toString());
}
});
}
private void registerAlarm() {
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,9);
calendar.set(Calendar.MINUTE,40);
calendar.set(Calendar.SECOND,0);
Intent intent=new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)this.getSystemService(this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
private void signIn(final String user, final String pwd){
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.child(user).exists()){
if(!user.isEmpty()){
User login=dataSnapshot.child(user).getValue(User.class);
if(login.getPassword().equals(pwd)){
Intent homeActivity=new Intent(MainActivity.this,Home.class);
Common.currentUser= login;
startActivity(homeActivity);
finish();
}else{
Toast.makeText(MainActivity.this,"Wrong Password",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this,"Please enter your user name.",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this,"User does not exist",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void showSignUpDialog(){
AlertDialog.Builder alertDialog= new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Sign Up");
alertDialog.setMessage("Please fill full information");
LayoutInflater inflater=this.getLayoutInflater();
View sign_up_layout = inflater.inflate(R.layout.sign_up_layout,null);
edtNewUser=(MaterialEditText) sign_up_layout.findViewById(R.id.edtNewUserName);
edtNewPassword=(MaterialEditText) sign_up_layout.findViewById(R.id.edtNewPassword);
edtNewEmail=(MaterialEditText) sign_up_layout.findViewById(R.id.edtNewEmail);
alertDialog.setView(sign_up_layout);
//Check for the icon
alertDialog.setIcon(R.drawable.ic_account_circle_black_24dp);
alertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialogInterface, int i){
dialogInterface.dismiss();
}
});
alertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialogInterface, int i) {
final User user = new User(edtNewUser.getText().toString(), edtNewPassword.
getText().toString(), edtNewEmail.getText().toString());
users.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(user.getUserName()).exists())
Toast.makeText(MainActivity.this, "User already exists!!!", Toast.LENGTH_SHORT).show();
else {
users.child(user.getUserName()).setValue(user);
Toast.makeText(MainActivity.this, "User registration successful!!!", Toast.LENGTH_SHORT).show();
}
}
public void onCancelled(DatabaseError databaseError) {
}
});
dialogInterface.dismiss();
}
});
alertDialog.show();
}
}
错误代码段:
01-18 23:14:13.656 23773-23773/? I/art: Late-enabling -Xcheck:jni
01-18 23:14:13.726 23773-23773/com.android.appgallery.quizworld D/LenovoAppIconTheme: ExtraResources;cleanCachedIcon;clear cache..
01-18 23:14:14.049 23773-23773/com.android.appgallery.quizworld W/System: ClassLoader referenced unknown path:
/data/app/com.android.appgallery.quizworld-2/lib/arm
01-18 23:14:14.079 23773-23773/com.android.appgallery.quizworld D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping
initialization.
01-18 23:14:14.095 23773-23773/com.android.appgallery.quizworld W/InstanceID/Rpc: Found 10014
01-18 23:14:14.102 23773-23773/com.android.appgallery.quizworld D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping
initialization.
01-18 23:14:14.144 23773-23773/com.android.appgallery.quizworld I/FA: App measurement is starting up, version: 10298
01-18 23:14:14.144 23773-23773/com.android.appgallery.quizworld I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
01-18 23:14:14.163 23773-23773/com.android.appgallery.quizworld V/FA: Collection enabled
01-18 23:14:14.163 23773-23773/com.android.appgallery.quizworld V/FA: App package, google app id: com.android.appgallery.quizworld,
1:778477946548:android:1b7a8c110537f112
01-18 23:14:14.163 23773-23773/com.android.appgallery.quizworld I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app
com.android.appgallery.quizworld
01-18 23:14:14.163 23773-23773/com.android.appgallery.quizworld D/FA: Debug-level message logging enabled
01-18 23:14:14.210 23773-23773/com.android.appgallery.quizworld V/FA: Registered activity lifecycle callback
01-18 23:14:14.212 23773-23773/com.android.appgallery.quizworld I/FirebaseInitProvider: FirebaseApp initialization successful
01-18 23:14:14.213 23773-23773/com.android.appgallery.quizworld I/InstantRun: starting instant run server: is main process
01-18 23:14:14.219 23773-23792/com.android.appgallery.quizworld V/FA: Using measurement service
01-18 23:14:14.225 23773-23792/com.android.appgallery.quizworld V/FA: Connecting to remote service
01-18 23:14:14.297 23773-23773/com.android.appgallery.quizworld W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter
android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList,
android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-18 23:14:14.306 23773-23773/com.android.appgallery.quizworld V/FA: onActivityCreated
01-18 23:14:14.472 23773-23773/com.android.appgallery.quizworld D/AndroidRuntime: Shutting down VM
01-18 23:14:14.473 23773-23773/com.android.appgallery.quizworld E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.appgallery.quizworld, PID: 23773
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.android.appgallery.quizworld/com.android.appgallery.quizworld.MainActivity}: android.view.InflateException: Binary XML file line
#0: Binary XML file line #0: Error inflating class MaterialEditText
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line
#0: Binary XML file line #0: Error inflating class MaterialEditText
at com.android.appgallery.quizworld.MainActivity.onCreate
(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line
#0: Binary XML file line #0: Error inflating class MaterialEditText
Caused by: android.view.InflateException: Binary XML file line
#0: Error inflating class MaterialEditText
Caused by: java.lang.ClassNotFoundException: Didn't find class
"android.view.MaterialEditText" on path: DexPathList[[zip file "/data/app/com.android.appgallery.quizworld-2/base.apk", zip file
"/data/app/com.android.appgallery.quizworld-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.android.appgallery.quizworld-
2/split_lib_slice_0_apk.apk", zip file "/data/app/com.android.appgallery.quizworld-2/split_lib_slice_1_apk.apk", zip file
"/data/app/com.android.appgallery.quizworld-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.android.appgallery.quizworld-
2/split_lib_slice_3_apk.apk", zip file "/data/app/com.android.appgallery.quizworld-2/split_lib_slice_4_apk.apk", zip file
"/data/app/com.android.appgallery.quizworld-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.android.appgallery.quizworld-
2/split_lib_slice_6_apk.apk", zip file "/data/app/com.android.appgallery.quizworld-2/split_lib_slice_7_apk.apk", zip file
"/data/app/com.android.appgallery.quizworld-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.android.appgallery.quizworld-
2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.android.appgallery.quizworld-2/lib/arm, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass
(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView
(LayoutInflater.java:609)
at android.view.LayoutInflater.onCreateView
(LayoutInflater.java:700)
at
com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
at android.view.LayoutInflater.onCreateView
(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag
(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag
(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate
(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren
(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate
(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren
(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate
(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren
(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate
(LayoutInflater.java:861)
01-18 23:14:14.474 23773-23773/com.android.appgallery.quizworld E/AndroidRuntime: at android.view.LayoutInflater.rInflateChildren
(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate
(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate
(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate
(LayoutInflater.java:377)
at
android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView
(AppCompatActivity.java:140)
at com.android.appgallery.quizworld.MainActivity.onCreate
(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:755)
答案 0 :(得分:-1)
您的问题可能是在布局中,您正在使用
com.rengwuxian.materialedittext:MaterialEditText
在文档中这样解释时
com.rengwuxian.materialedittext.MaterialEditText
来源: https://github.com/rengwuxian/MaterialEditText/wiki/Layout-and-Code-Example
问候!