我正在使用Firebase开发一款适用于Android的简单社交网络应用。该应用程序的主要活动是登录屏幕。登录在运行Android 7.0(API 24)的模拟器上运行正常,但在Android 4.4(API 19)中登录时崩溃。这是Logcat:
06-29 20:32:52.428 31471-31471/com.oliviamontoya.letstradezines E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.oliviamontoya.letstradezines, PID: 31471
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oliviamontoya.letstradezines/com.oliviamontoya.letstradezines.Main2Activity}: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
at android.app.ActivityThread.access$700(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5082)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.oliviamontoya.letstradezines.Main2Activity.onCreate(Main2Activity.java:54)
at android.app.Activity.performCreate(Activity.java:5287)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2145)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
at android.app.ActivityThread.access$700(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5082)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.oliviamontoya.letstradezines.Main2Activity.onCreate(Main2Activity.java:54)
at android.app.Activity.performCreate(Activity.java:5287)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2145)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
at android.app.ActivityThread.access$700(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5082)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020070
at android.content.res.Resources.getValue(Resources.java:1163)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:196)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:189)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:427)
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems(NavigationMenuPresenter.java:519)
at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.update(NavigationMenuPresenter.java:460)
at android.support.design.internal.NavigationMenuPresenter.updateMenuView(NavigationMenuPresenter.java:117)
at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:252)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:174)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:100)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.oliviamontoya.letstradezines.Main2Activity.onCreate(Main2Activity.java:54)
at android.app.Activity.performCreate(Activity.java:5287)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2145)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
at android.app.ActivityThread.access$700(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1401)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5082)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
at dalvik.system.NativeStart.main(Native Method)
这是Java:
public class MainActivity extends AppCompatActivity {
ImageView im;
TextView tv;
EditText email;
EditText password;
Button b;
Button b2;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
im = (ImageView) findViewById(R.id.img);
tv = (TextView) findViewById(R.id.txt);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
b = (Button) findViewById(R.id.button);
b.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
b.setTextColor(Color.WHITE);
b2 = (Button) findViewById(R.id.button2);
b2.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
b2.setTextColor(Color.WHITE);
}
public void logIn(View view) {
mAuth.signInWithEmailAndPassword(email.getText().toString(), password.getText().toString())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail:failed", task.getException());
Toast.makeText(MainActivity.this, "Authorization failed.",
Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
// ...
}
});
}
public void signUp(View view) {
Intent intent = new Intent(this, SignUp.class);
startActivity(intent);
}
}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:layout_margin="10dp"
android:gravity="center"
tools:context="com.oliviamontoya.letstradezines.MainActivity">
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout 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"
android:orientation="vertical"
android:layout_margin="10dp"
android:gravity="center"
tools:context="com.oliviamontoya.letstradezines.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/txt"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:text="Let's Trade Zines is a social network for the zine community!" />
<EditText
android:id="@+id/email"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLength="30"
android:hint="Email" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:ellipsize="start"
android:maxLength="30"
android:hint="Password" />
<GridLayout 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="wrap_content"
android:layout_height="wrap_content"
android:columnCount="2"
android:gravity="center"
tools:context="com.oliviamontoya.letstradezines.MainActivity">
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="20sp"
android:onClick="logIn"
android:text="Log In" />
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="20sp"
android:onClick="signUp"
android:text="Sign Up!" />
</GridLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
当我尝试登录时,有人能告诉我为什么会崩溃吗?
编辑:这是Main2Activity的Java和XML:
public class Main2Activity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ImageView profileImg;
TextView usernameDisplay;
TextView openTrades;
TextView messages;
TextView followedFeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
profileImg = (ImageView) findViewById(R.id.profileImg);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference databaseReference = database.getReference(firebaseUser.getUid());
//DatabaseReference child = databaseReference.child(username.getText().toString());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user.profilePic.isEmpty()) {
profileImg.setImageResource(R.mipmap.ic_launcher);
} else {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();
try {
final File localFile = File.createTempFile("profilePic", "jpg");
StorageReference ref = mStorageRef.child("images/" + firebaseUser.getUid() + "ProfilePic.jpg");
ref.getFile(localFile)
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap bMap = BitmapFactory.decodeFile(localFile.toString());
profileImg.setImageBitmap(bMap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle failed download
// ...
}
});
} catch(Exception e) {
Toast.makeText(Main2Activity.this, "Error creating file.",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
usernameDisplay = (TextView) findViewById(R.id.usernameDisplay);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String name = user.getDisplayName();
usernameDisplay.setText(name);
}
openTrades = (TextView) findViewById(R.id.openTrades);
openTrades.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
openTrades.setTextColor(Color.WHITE);
messages = (TextView) findViewById(R.id.messages);
messages.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
messages.setTextColor(Color.WHITE);
followedFeed = (TextView) findViewById(R.id.followedFeed);
followedFeed.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
followedFeed.setTextColor(Color.WHITE);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
Intent intent = new Intent(this, Profile.class);
startActivity(intent);
} else if (id == R.id.nav_trades) {
} else if (id == R.id.nav_messages) {
Intent intent = new Intent(this, Messages.class);
startActivity(intent);
} else if (id == R.id.nav_followFeed) {
} else if (id == R.id.nav_search) {
Intent intent = new Intent(this, Search.class);
startActivity(intent);
} else if (id == R.id.nav_logOut) {
FirebaseAuth.getInstance().signOut();
finish();
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main2"
app:menu="@menu/activity_main2_drawer" />
</android.support.v4.widget.DrawerLayout>
content_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.oliviamontoya.letstradezines.Main2Activity"
tools:showIn="@layout/app_bar_main2">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout 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"
android:layout_margin="10dp"
android:orientation="vertical"
android:gravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.oliviamontoya.letstradezines.Main2Activity"
tools:showIn="@layout/app_bar_main2">
<ImageView
android:id="@+id/profileImg"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center" />
<TextView
android:id="@+id/usernameDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:gravity="center"
android:layout_margin="10dp" />
<TextView
android:id="@+id/openTrades"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="20sp"
android:gravity="center"
android:text="This is where open trades will go." />
<TextView
android:id="@+id/messages"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="20sp"
android:gravity="center"
android:text="This is where messages will go." />
<TextView
android:id="@+id/followedFeed"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="20sp"
android:gravity="center"
android:text="This is where followed feed will go." />
</LinearLayout>
</ScrollView>
</LinearLayout>
app_bar_main2.xml和main2.xml是在我添加导航抽屉活动时从我给出的内容中未编辑的。 activity_main2_drawer.xml和nav_header_main2.xml与给我的文本和图标的一些编辑很接近,但我不能在这里完全适合它们。
答案 0 :(得分:1)
根据logcat,很明显在给班级充气时出现错误&#34;导航视图&#34;。
导航视图类在api级别22.2.0中添加,因此它不会在低于该级别的api级别中工作...
我建议您不要使用&#34;导航视图&#34;如果你正在使用api级别低于api级别22.
希望这会有所帮助。
答案 1 :(得分:0)