【问题标题】:Error inflating class AppCompatButton膨胀类 AppCompatButton 时出错
【发布时间】:2015-11-05 18:53:03
【问题描述】:

我正在制作项目,要求所有 android 设备的用户界面都相同。为了实现这个要求,我用谷歌搜索,我得到了在所有 Android 版本中使用新的 AppCompact 库的解决方案。

所以问题是,我的布局中有一个按钮,我使用 AppCompatButton 而不是 Button。

甚至我的该布局的 Activity 都扩展了 AppCompatActivity。 但是当我运行该项目时,我遇到了错误并且应用程序崩溃了。

这是我的活动和布局代码

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     AppCompatButton appCompatButton = (AppCompatButton) findViewById(R.id.btn_submit);
     appCompatButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
         }
     });
  }
 }

布局:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                   
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.app.myway.MainActivity">

    <EditText
         android:id="@+id/et_email"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="100dp"
         android:hint="@string/add_email"
         android:paddingLeft="16dp"
         android:paddingRight="16dp" />

   <AppCompatButton
         android:id="@+id/btn_submit"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/submit" />
 </RelativeLayout>

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.myway/com.app.myway.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class AppCompatButton

【问题讨论】:

    标签: java android android-layout user-interface android-appcompat


    【解决方案1】:

    你需要:

      <android.support.v7.widget.AppCompatButton
         android:id="@+id/btn_submit"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/submit" />
    

    在你的 build.gradle 中:

     dependencies{
         compile 'com.android.support:appcompat-v7:23.1.0'
     }
    

    【讨论】:

    • 哦,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2019-11-29
    • 2017-02-28
    • 2021-07-10
    • 2020-08-01
    • 2016-02-28
    • 2013-05-17
    • 2016-09-22
    • 2016-05-28
    相关资源
    最近更新 更多