从活动类调用方法时,应用程序崩溃

时间:2017-04-27 06:22:23

标签: java android

我正在尝试创建一个既利用MediaButtonReceiver类又使用Activity类中的方法的应用程序。为了让Activity类代码运行,我创建了一个MainActivity的新实例,并尝试运行包含我想要运行的代码的方法。我后来发现这是不可能的,最容易解决的是什么?

MainActivity.java

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ((AudioManager) getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(
            this,
            external.class));

    //external main  = new external();


}

public void variables (){ //code run when button is pressed in external.java

    final ToggleButton toggleWF = (ToggleButton) findViewById(R.id.WiFi_toggle);
    final ToggleButton toggleBT = (ToggleButton) findViewById(R.id.BT_toggle);
    final ToggleButton toggleNFC = (ToggleButton) findViewById(R.id.NFC_toggle);
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (toggleWF.isChecked()) {
        if (wifi.isWifiEnabled()) {
            wifi.setWifiEnabled(false);
        } else {
            wifi.setWifiEnabled(true);
        }
    }

    if (toggleBT.isChecked()) {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.disable();
        } else {
            mBluetoothAdapter.enable();
        }
    }

    if (toggleNFC.isChecked()){
        Class<?> NfcManagerClass;
        Method setNfcEnabled, setNfcDisabled;
        boolean Nfc = false;
        if(mNfcAdapter.isEnabled()) {
            try {
                NfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
                setNfcDisabled = NfcManagerClass.getDeclaredMethod("disable");
                setNfcDisabled.setAccessible(true);
                Nfc = (Boolean) setNfcDisabled.invoke(mNfcAdapter);
            } catch (ClassNotFoundException e) {
            } catch (NoSuchMethodException e) {
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e){
            }
        }
        else {
            try {
                NfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
                setNfcEnabled = NfcManagerClass.getDeclaredMethod("enable");
                setNfcEnabled.setAccessible(true);
                Nfc = (Boolean) setNfcEnabled.invoke(mNfcAdapter);
            } catch (ClassNotFoundException e) {
            } catch (NoSuchMethodException e) {
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }

            if (Nfc == false) {
                //add code here if NFC fails to disable / enable

            }
        }
    }


}





}

第二课制作MainActivity的新实例

public class external extends MediaButtonReceiver {



@Override
public void onReceive(Context context, Intent intent) {
    Log.d("Button", "pressed");



    MainActivity main  = new MainActivity();

    main.variables();


}
}

2 个答案:

答案 0 :(得分:0)

让您的方法保持静态

public static void variables (){....}

然后简单地使用它

MainActivity.variables();

答案 1 :(得分:0)

Take instance of activity like this

<pre> <code> public class external extends MediaButtonReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("Button", "pressed");

  if(context instanceof MainActivity) {
      MainActivity main = ((MainActivity)context);
      main.variables();
    }
  }
}   </code> </pre>