Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中,尝试使用SwitchCompat时会出现通货膨胀错误_Android_Switchcompat - Fatal编程技术网

在Android中,尝试使用SwitchCompat时会出现通货膨胀错误

在Android中,尝试使用SwitchCompat时会出现通货膨胀错误,android,switchcompat,Android,Switchcompat,我试图使用android.support.v7.widget.SwitchCompat在最小SDK为10的情况下创建一个交换机,在寻找解决方案时,我得到了错误android.view.InflateException:Binary XML文件行#9:error-inflating类android.support.v7.widget.SwitchCompat,我发现它可能与声明的样式有关?因此,我已将样式更改为Theme.AppCompat,但我仍然收到无法膨胀的错误。我很确定我已经添加了正确的支

我试图使用
android.support.v7.widget.SwitchCompat
在最小SDK为10的情况下创建一个交换机,在寻找解决方案时,我得到了错误
android.view.InflateException:Binary XML文件行#9:error-inflating类android.support.v7.widget.SwitchCompat
,我发现它可能与声明的样式有关?因此,我已将样式更改为
Theme.AppCompat
,但我仍然收到无法膨胀的错误。我很确定我已经添加了正确的支持库(我正在使用Eclipse Kepler),因为导入android.support.v7.widget.SwitchCompat时没有错误。这是所有相关代码,有人能看到问题出在哪里吗

Manifest:
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
....


Style:
<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="Theme.AppCompat">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
...
</resources>



XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal" >

<android.support.v7.widget.SwitchCompat
    android:id="@+id/test1_SW"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="50dp"
    android:layout_marginStart="50dp"
    android:layout_marginTop="25dp"
    android:checked="false"
    android:text="SwitchCompat"
    android:textOff="OFF"
    android:textOn="ON" />

</RelativeLayout>


Activity:
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.widget.CompoundButton;

public class SwitchTest extends Activity implements
    CompoundButton.OnCheckedChangeListener {
private SwitchCompat test1_SW;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.cutin, R.anim.cutout);
    setContentView(R.layout.switch_test);

    // --- one
    test1_SW = (SwitchCompat) findViewById(R.id.test1_SW);
    test1_SW.setSwitchPadding(40);
    test1_SW.setOnCheckedChangeListener(this);
    // --- END one

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
    case R.id.test1_SW:
        Log.i("test1_SW", isChecked + "");
        break;
    }

}

}
清单:
....
风格:
...
XML:
活动:
导入android.app.Activity;
导入android.os.Bundle;
导入android.support.v7.widget.SwitchCompat;
导入android.util.Log;
导入android.widget.CompoundButton;
公共类SwitchTest扩展了活动实现
CompoundButton.OnCheckedChangeListener{
专用交换机兼容测试1_SW;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
覆盖过渡(R.anim.cutin,R.anim.cutout);
setContentView(R.layout.switch_测试);
//---一
test1_SW=(SwitchCompat)findviewbyd(R.id.test1_SW);
测试1_开关设置(40);
test1_SW.setOnCheckedChangeListener(此);
//---结束一
}
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
开关(buttonView.getId()){
案例R.id.test1\U SW:
Log.i(“test1_SW”,isChecked+”);
打破
}
}
}

问题在于您的
活动
正在扩展
android.app.Activity
,而不是您需要与
AppCompat
库一起使用的支持
活动
。只要改变一下:

import android.app.Activity;

或者,因为我认为API 22中已经不推荐使用:

import android.support.v7.app.AppCompatActivity;

一旦你改变了这些,它应该会工作。

我认为你的思路是正确的,我已经将Activity改为AppCompatActivity,但现在我得到了错误
java.lang.NoSuchFieldError:android.support.v7.appcompat.R$styleable.Theme\u windowActionBar
当我在谷歌上搜索该错误时,人们建议备份到库的v20。这没有帮助,还会出现更多错误,所以现在我回到V21,非常确定您需要确保应用程序在styles.xml中使用的主题也使用AppCompat主题,而不是默认主题。我还建议您使用SDKManager下载的最新版本的支持库,并确保您的应用程序引用的项目中确实有最新的资源文件,否则您可能会得到错误(您需要的不仅仅是Jar)我通过从我的工作区中删除支持库appcompat_v7并从sdk位置重新导入来修复它。
import android.support.v7.app.AppCompatActivity;