0

我正在寻找小部件Switch。不是 switch 语句或 switch 活动等。

网上有非常少见的教程。

我的问题是 xml 中的图形布局看不到开关。

我的 xml

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="推送通知"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_marginTop="5dp"
        android:textColor="#ffffff"
        android:textSize="15dp" />

    <Switch
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textOff="关"
        android:textOn="开" />
</RelativeLayout>

错误发生在那里。

错误

Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layout to choose a different layout, or fix the theme style references.

NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first.

Failed to find style 'switchStyle' in current theme java.lang.NullPointerException Exception details are logged in Window > Show View > Error Log

我不知道该怎么做?

4

3 回答 3

2

你的 AndroidManifest.xml 应该有

 <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="16" />

因为 Switch 小部件仅支持 14 及以上 API 级别..

给你一个简单的例子..

main.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

            <Switch
                android:id="@+id/bluetooth"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="32dip"
                android:text="Bluetooth"
                />

</GridLayout>

ur .java



package time.pass;

import android.os.Bundle;
import android.app.Activity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class TimePass extends Activity implements CompoundButton.OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_time_pass );

    Switch s = (Switch) findViewById(R.id.bluetooth);
    if (s != null) {
        s.setOnCheckedChangeListener(this);
    }
}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   Toast.makeText(this, "Bluetooth is " + (isChecked ? "on" : "off"),
           Toast.LENGTH_SHORT).show();
}
}
于 2012-12-20T14:00:49.153 回答
0

您可以使用 android.support.v7.widget.SwitchCompat 来实现向后兼容性。

于 2015-11-25T14:34:11.427 回答
-3

使用您的 xml 文件的主题作为Theme.DeviceDefault

于 2012-06-27T10:26:50.170 回答