问题描述
我是Android开发中的新手。我可以使该应用程序在Android 5.1中运行,但是当我尝试在Android 4.1中部署该应用程序时,它通常会在主要活动本身中崩溃。我理解它是因为我使用了矢量图像,而android 4.1与它不兼容。为了克服这个问题,我将代码设置如下:
I am a newbee in the Android development. I am able to make this app work in Android 5.1 and but however when I tried to deploy this app in Android 4.1 it used to crash in the main activity itself. I understood its becuase I used vector images and android 4.1 is not compatible with it. To overcome this I set the code as mentioned below:
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
You’ll note this new attribute only exists in the version 2.0 of the Gradle Plugin. If you are using Gradle 1.5 you’ll instead use
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
我现在收到此错误
代码文件
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Button btnRegister;
Button btnRequestLog;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
private String android_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if ( findViewById(R.id.fab) == null)
{}
else {
}
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);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
// Buttons
btnRegister = (Button) findViewById(R.id.btnRegister);
btnRequestLog = (Button) findViewById(R.id.btnRequestLog);
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_main"
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_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
请帮助我进行调试。我有点困惑,需要您的帮助。
Please help me in debugging this. I am kind of stuck and would need your help.
推荐答案
您正在使用矢量可绘制对象。在Android 4.x及更早版本上,仅在以下情况下支持此功能:
You are using vector drawables. On Android 4.x and earlier, this is only supported if you:
- 使用Android支持库23.x或更高版本✓
- 在gradle构建文件中启用它(vectorDrawables.useSupportLibrary = true)✓
- 使用
srcCompat
代替每当您使用ImageView
- use Android Support Libraries 23.x or later ✓
- enable it in the gradle build file (vectorDrawables.useSupportLibrary = true) ✓
- use
srcCompat
instead ofsrc
whenever you useImageView
最后一项可能是需要解决的问题。
The last item is probably the problem that needs to be fixed.
这篇关于应用程序在Android 5.1和6.1中运行良好,但在4.10(API 16)中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!