0

when I set the switch button at action bar but some error is thrown.

04-10 12:17:36.986 5121-5121/in.spotrack.soubhagya E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: in.spotrack.soubhagya, PID: 5121
                                                                     java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference
                                                                         at in.spotrack.soubhagya.Operator.onCreateOptionsMenu(Operator.java:28)
                                                                         at android.app.Activity.onCreatePanelMenu(Activity.java:3373)
                                                                         at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
                                                                         at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
                                                                         at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
                                                                         at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1370)
                                                                         at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1650)
                                                                         at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
                                                                         at android.os.Handler.handleCallback(Handler.java:789)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

this is code in which we can inflate the menu with the switch button.

 package in.spotrack.soubhagya;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class Operator extends AppCompatActivity {


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem itemSwitch = menu.findItem(R.id.actionMenu);
    Log.i("Abhinav", String.valueOf(R.layout.switch_layout));
    itemSwitch.setActionView(R.layout.switch_layout);
    final Switch sw=menu.findItem(R.id.actionMenu).getActionView().findViewById(R.id.switchView);
    getMenuInflater().inflate(R.menu.mainmenu,menu);
    return super.onCreateOptionsMenu(menu);
}
}

menu.xml code

<?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/actionMenu"
    android:title=""
    app:showAsAction="always"
    android:actionLayout="@layout/switch_layout"/>
</menu>

switch_layout code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Switch
    android:id="@+id/switchView"
    android:layout_width="70dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:scaleX="1.3"
    android:scaleY="1.3"
    android:switchMinWidth="45dp"
    android:padding="6dp"
    />
 </LinearLayout>
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55

1 Answers1

0

On the onCreateOptionsMenu() Method, there the getActionView() is giving null, so the application is outputting NullPOinterException.

 final Switch sw=menu.findItem(R.id.actionMenu).getActionView().findViewById(R.id.switchView);

for understanding NullPointerException you go here.

Yo can Also Print

System.err.print(menu.findItem(R.id.actionMenu));

and see what this is holding and check the entity class for the proper sending of data. Getter and Setter should also be checked.

suman heuju
  • 121
  • 3