क्यों करता है मैं इसके लिए घंटों के लिए समाधान ढूंढ रहा हूं। मैं सिर्फ एक्शनबार (जैसे ब्लूटूथ सेटिंग्स में) के तहत एक स्विच जोड़ना चाहता हूं। मुझे यहां एक समान प्रश्न मिला, लेकिन शायद यह पुराना था।getActionView() वापस शून्य
MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.myswitch);
switchButton = (Switch) item.getActionView();
return super.onCreateOptionsMenu(menu);
}
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_switch"
android:title="off/on"
app:showAsAction="always"
app:actionLayout="@layout/switchlayout"
app:actionViewClass="android.support.v7.widget.Switch" />
</menu>
switchlayout.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="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Switch
android:id="@+id/myswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#1E88E5" />
</RelativeLayout>
लेकिन कोई फर्क नहीं पड़ता कि मुझे क्या करना मैं हमेशा मिलती है:
वैसे भी यहां मेरे कोड है'Invode करने के लिए ... getActionView() एक अशक्त वस्तु संदर्भ
मैं उलझन में हूँ पर क्योंकि मैं सिर्फ आइटम R.id.myswitch
से पहले लाइन परिभाषित किया गया है परिभाषित
प्रयास, मैं गड़बड़ है कि अप किया था?
कि यह तय हो गई है, धन्यवाद! – Plays2