我在MainActivity中使用NavigationView
。现在,我的应用程序在Android 5.0
及更高版本中正常运行。但是,它并没有在Android 4.0.4
中运行。它在XML
中显示<NavigationView />
错误。
我认为应该有所帮助,而不是添加multidex
支持。但是,它没有。
目前我在NavigationView
&amp; header_layout
中的1个启动器图标。所以,我认为不应该OutOfMemoryError
。
这是我的代码
的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.gripxtech.kasimrangwala.unipayretailer"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:multidex:1.0.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.malinskiy:superrecyclerview:1.1.1'
compile 'com.codinguser.android:contactpicker:3.0.0@aar'
compile 'com.squareup.okhttp:okhttp:2.7.0'
compile 'org.apache.commons:commons-lang3:3.4'
compile project(':materialdatetimepicker-library')
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start" >
<FrameLayout
android:id="@+id/nav_content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_main" />
</android.support.v4.widget.DrawerLayout>
我无法弄清楚该怎么做。谢谢你提前。
更新
这是我的Logcat错误
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gripxtech.kasimrangwala.unipayretailer/com.gripxtech.kasimrangwala.unipayretailer.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.gripxtech.kasimrangwala.unipayretailer.MainActivity.onCreate(MainActivity.java:47)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
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:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.gripxtech.kasimrangwala.unipayretailer.MainActivity.onCreate(MainActivity.java:47)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:190)
at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:250)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:172)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:94)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.gripxtech.kasimrangwala.unipayretailer.MainActivity.onCreate(MainActivity.java:47)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
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:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:190)
at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:250)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:172)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:94)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.gripxtech.kasimrangwala.unipayretailer.MainActivity.onCreate(MainActivity.java:47)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/side_nav_bar.xml from drawable resource ID #0x7f020059
at android.content.res.Resources.loadDrawable(Resources.java:1697)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.view.View.<init>(View.java:1951)
at android.view.View.<init>(View.java:1899)
at android.view.ViewGroup.<init>(ViewGroup.java:286)
at android.widget.LinearLayout.<init>(LinearLayout.java:120)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:190)
at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:250)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:172)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:94)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.gripxtech.kasimrangwala.unipayretailer.MainActivity.onCreate(MainActivity.java:47)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2 at android.content.res.Typ
更新2
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity {
@Bind(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
@Bind(R.id.nav_view)
NavigationView mNavigationView;
TextView mUserFullName;
TextView mUserBalacne;
private static final String PREFERENCES_FILE = "mymaterialapp_settings";
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private int mCurrentSelectedPosition;
SessionManager mManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setUpNavDrawer();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void setUpNavDrawer() {
Menu menu = mNavigationView.getMenu();
menu.getItem(mCurrentSelectedPosition).setChecked(true);
loadFragment(mCurrentSelectedPosition);
setUpNavView();
setUpUserData();
}
public DrawerLayout getDrawerLayout() {
return mDrawerLayout;
}
public NavigationView getNavigationView() {
return mNavigationView;
}
public TextView getmUserFullName() {
return mUserFullName;
}
public TextView getmUserBalacne() {
return mUserBalacne;
}
public void setUpNavView() {
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.action_recharge:
if (mCurrentSelectedPosition != 0) {
loadFragment(0);
}
break;
case R.id.action_refill_wallet:
if (mCurrentSelectedPosition != 1) {
loadFragment(1);
}
break;
case R.id.action_payment:
if (mCurrentSelectedPosition != 2) {
loadFragment(2);
}
break;
case R.id.action_logout:
logout();
break;
default:
break;
}
return true;
}
});
}
public void setUpUserData() {
View view = mNavigationView.getHeaderView(0);
mUserFullName = (TextView) view.findViewById(R.id.tvNavUserFullName);
mUserBalacne = (TextView) view.findViewById(R.id.tvNavUserBalance);
mManager = new SessionManager(MainActivity.this);
mUserFullName.setText(mManager.getUserFullName());
mUserBalacne.setText(mManager.getUserBalance());
}
public void logout() {
mManager.setLogin(false);
startActivity(new Intent(MainActivity.this, LoginActivity.class));
MainActivity.this.finish();
}
public void saveSharedSetting(int settingValue) {
SharedPreferences sharedPref = getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(STATE_SELECTED_POSITION, settingValue);
editor.commit();
}
public int readSharedSetting() {
SharedPreferences sharedPref = getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
return sharedPref.getInt(STATE_SELECTED_POSITION, 0);
}
public void loadFragment(int currentSelectedPosition) {
mCurrentSelectedPosition = currentSelectedPosition;
Fragment fragment = null;
if (mCurrentSelectedPosition == 0) {
setTitle(getResources().getString(R.string.app_name));
fragment = RechargeTabsFragment.newInstance();
} else if (mCurrentSelectedPosition == 1) {
setTitle(getResources().getString(R.string.action_refill_wallet));
fragment = WalletFragment.newInstance();
} else if (mCurrentSelectedPosition == 2) {
setTitle(getResources().getString(R.string.action_payment));
fragment = PaymentFragment.newInstance();
}
clearBackStack();
getSupportFragmentManager().beginTransaction().replace(R.id.nav_content_main, fragment).commit();
saveSharedSetting(mCurrentSelectedPosition);
}
public void clearBackStack() {
FragmentManager fragmentManager = getSupportFragmentManager();
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
fragmentManager.popBackStackImmediate();
}
}
}
答案 0 :(得分:2)
最后,我找到了它。
首先,这是我的nav_header.xml
文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tvNavUserFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/tvNavUserBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_sub_head" />
</LinearLayout>
现在,专注于线号。 5。
android:background="@drawable/side_nav_bar"
它只不过是梯度。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:endColor="?attr/colorPrimaryDark"
android:startColor="?attr/colorPrimary"
android:type="linear" />
</shape>
我改变了这样的颜色值。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:endColor="@color/primary_dark"
android:startColor="@color/primary"
android:type="linear" />
</shape>
现在,它正在Android 2.3.3仿真器上运行。 希望这将有助于将来的一些人。 谢谢大家。
答案 1 :(得分:0)
确保您对Android设计支持库具有正确的依赖关系。选择错误的一个很容易 - 因为Google似乎发布了两个不同的依赖字符串:
在此回答时,Android developer blog(以及用户hungryghost的注释)具有正确的依赖关系字符串,而Support library主页上的依赖关系字符串不起作用。
使用这个:compile&#39; com.android.support:design:22.2.0&#39;
请注意,版本是22.2.0(错误:22.0.0),并且该包名为design(错误:support-design)
完成这些更改后,请使用Android Studio菜单&#34;重建项目&#34;或&#34;同步&#34;更改gradle文件后有时会出现的按钮。