Android 应用程序在我使用 ImageButtons 后崩溃,之前我使用可绘制的形状并且它工作正常
The Android Application crashes after I use ImageButtons, previously I used drawable shapes and it was working fine
Android 应用程序在我使用 ImageButtons 后崩溃,之前我使用可绘制的形状并且它工作正常。
activity_app_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.smartcarassistant.AppListActivity">
<TextView
android:id="@+id/appListTextview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Let me Assist You..."
android:textColor="@color/common_google_signin_btn_text_light"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonNotification"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:fontFamily="@font/cherry_cream_soda"
android:src="@drawable/notifications"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonNotification"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Manage">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonNotification"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Notifications">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonMonthlyExpenses"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:background="@drawable/circle_background"
android:src="@drawable/expense"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonMonthlyExpenses"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Monthly Expense">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonMoodyMusicPlayer"
android:layout_width="100dp"
android:layout_marginLeft="5dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:src="@drawable/music"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonMoodyMusicPlayer"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Moody Music">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonMoodyMusicPlayer"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Player">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:layout_marginStart="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonMileage"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:src="@drawable/mileage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonMileage"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Mileage">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:id="@+id/buttonTravelWithWeather"
android:layout_width="100dp"
android:layout_marginStart="5dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:contentDescription="Travel With Weather"
android:src="@drawable/weather"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonTravelWithWeather"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Travel With">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonTravelWithWeather"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Weather">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonHelpDesk"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:layout_marginLeft="10dp"
android:src="@drawable/helpdesk"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonHelpDesk"
android:layout_centerInParent="true"
android:clickable="false"
android:text="HelpDesk">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonExploreAroundme"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:src="@drawable/explore"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonExploreAroundme"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Explore Around">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonExploreAroundme"
android:layout_centerInParent="true"
android:clickable="false"
android:text="ME">
</TextView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
AppListActivity.java
package com.smartcarassistant;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class AppListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_list);
final ImageButton btNotification=(ImageButton)findViewById(R.id.buttonNotification);
final ImageButton btMonthlyExpenses=(ImageButton)findViewById(R.id.buttonMonthlyExpenses);
btNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent4=new Intent(AppListActivity.this,NotificationActivity.class);
startActivity(intent4);
}
});
btMonthlyExpenses.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent3=new Intent(AppListActivity.this,MonthlyExpensesActivity.class);
startActivity(intent3);
}
});
}
}
只要我使用形状背景作为按钮的图标,代码就没问题,但是当我切换到 ImageButton
时,它在打开后立即崩溃。
首先从 xml 文件中的每个 ImageButton 中删除所有 "android:src=" 行
然后在应用级别 build.gradle 文件中将这些行添加到 defaultConfig 块
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
然后同步项目
然后在 activity 的 onCreate 中添加这一行:
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
然后像这样在 activity class 中设置图像
btNotification.setImageDrawable(getResources().getDrawable(R.drawable.notification));
问题已解决!我搜索了一下,然后我才知道是某些缓存造成的。清理项目后,使缓存失效并重新启动一切正常。
谢谢大家!
Android 应用程序在我使用 ImageButtons 后崩溃,之前我使用可绘制的形状并且它工作正常。
activity_app_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.smartcarassistant.AppListActivity">
<TextView
android:id="@+id/appListTextview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Let me Assist You..."
android:textColor="@color/common_google_signin_btn_text_light"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonNotification"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:fontFamily="@font/cherry_cream_soda"
android:src="@drawable/notifications"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonNotification"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Manage">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonNotification"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Notifications">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonMonthlyExpenses"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:background="@drawable/circle_background"
android:src="@drawable/expense"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonMonthlyExpenses"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Monthly Expense">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonMoodyMusicPlayer"
android:layout_width="100dp"
android:layout_marginLeft="5dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:src="@drawable/music"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonMoodyMusicPlayer"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Moody Music">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonMoodyMusicPlayer"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Player">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:layout_marginStart="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonMileage"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:src="@drawable/mileage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonMileage"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Mileage">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:id="@+id/buttonTravelWithWeather"
android:layout_width="100dp"
android:layout_marginStart="5dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:contentDescription="Travel With Weather"
android:src="@drawable/weather"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonTravelWithWeather"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Travel With">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonTravelWithWeather"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Weather">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonHelpDesk"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:layout_marginLeft="10dp"
android:src="@drawable/helpdesk"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonHelpDesk"
android:layout_centerInParent="true"
android:clickable="false"
android:text="HelpDesk">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:gravity="center"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:contentDescription="Travel With Weather"
android:id="@+id/buttonExploreAroundme"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle_background"
android:src="@drawable/explore"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonExploreAroundme"
android:layout_centerInParent="true"
android:clickable="false"
android:text="Explore Around">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_below="@id/buttonExploreAroundme"
android:layout_centerInParent="true"
android:clickable="false"
android:text="ME">
</TextView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
AppListActivity.java
package com.smartcarassistant;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class AppListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_list);
final ImageButton btNotification=(ImageButton)findViewById(R.id.buttonNotification);
final ImageButton btMonthlyExpenses=(ImageButton)findViewById(R.id.buttonMonthlyExpenses);
btNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent4=new Intent(AppListActivity.this,NotificationActivity.class);
startActivity(intent4);
}
});
btMonthlyExpenses.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent3=new Intent(AppListActivity.this,MonthlyExpensesActivity.class);
startActivity(intent3);
}
});
}
}
只要我使用形状背景作为按钮的图标,代码就没问题,但是当我切换到 ImageButton
时,它在打开后立即崩溃。
首先从 xml 文件中的每个 ImageButton 中删除所有 "android:src=" 行
然后在应用级别 build.gradle 文件中将这些行添加到 defaultConfig 块
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
然后同步项目
然后在 activity 的 onCreate 中添加这一行:
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
然后像这样在 activity class 中设置图像
btNotification.setImageDrawable(getResources().getDrawable(R.drawable.notification));
问题已解决!我搜索了一下,然后我才知道是某些缓存造成的。清理项目后,使缓存失效并重新启动一切正常。
谢谢大家!