从活动返回后,手电筒功能崩溃

时间:2017-12-05 16:08:58

标签: android nullpointerexception

我有这个Android应用程序连接到mqtt经纪人并听取播放/暂停铃声或打开/关闭手电筒的说明。

它运行应该直到我更改我的设置并在此之后调用函数flashOnOff。然后我得到一个空指针异常,但我不明白为什么。

这是我的代码(我没有包含我的导入以节省一些空间):

public class MainActivity extends AppCompatActivity {

//    Layout related parameters
Toolbar myToolbar;
Spinner mySpinner;
ImageButton flashlightButton;
Button ringtone;
EditText message;

//  Camera/flashlight related parameters
Camera camera;
Camera.Parameters parameters;

//    MQTT related parameters
private String BrokerIp = "tcp://192.168.1.3:1883";
private int qos = 2;
static String Username = "user1";
static String Password = "user1";
String topicStr = "commands";
String clientId = "AndroidClient";
MqttConnectOptions options;
MqttAndroidClient client;
Vibrator vibrator;


//  Ringtone related parameters
Ringtone myRingtone;
MediaPlayer ringtoneMP ;


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


    vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    myRingtone = RingtoneManager.getRingtone(getApplicationContext(),uri);

    myToolbar = (Toolbar) findViewById(R.id.toolbar);
    mySpinner = (Spinner) findViewById(R.id.spinner);
    flashlightButton = (ImageButton) findViewById(R.id.image);

    myToolbar.setLogo(R.drawable.twoguyswatchmrrobot);
    myToolbar.setTitle(getResources().getString(R.string.app_name));

    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
            R.layout.custom_spinner_itam,
            getResources().getStringArray(R.array.Toolbar_dropdown_entries));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mySpinner.setAdapter(myAdapter);

    ringtoneMP = MediaPlayer.create(this,R.raw.games_of_thrones);
    ringtone = (Button) this.findViewById(R.id.ringtone);

    message = (EditText)findViewById(R.id.messagePublish);

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(MainActivity.this,
                    mySpinner.getSelectedItem().toString(),
                    Toast.LENGTH_SHORT)
                    .show();

            if (mySpinner.getSelectedItem().toString().equals("Exit App")){
                exit();
            }else if(mySpinner.getSelectedItem().toString().equals("Settings"))
            {
                Intent intent;
                intent = new Intent(MainActivity.this, SettingsActivity.class);
                intent.putExtra("CURRENT_IP",client.getServerURI());
                intent.putExtra("CURRENT_QOS", Integer.toString(qos));
                intent.putExtra("CURRENT_TOPIC",topicStr);
                startActivityForResult(intent,1);   //  this is so we can take the results back to mainActivity later
            }else{
                System.out.println("ara3e, den exw diale3ei tipota");
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }

    });

    //Flashlight on Create start
    if(isFlashAvailable())   //  check if flash is available on this device, if it is open camera (module) and make button clickable
    {
        camera = Camera.open();
        parameters = camera.getParameters();

    }else
    {  //  if flashlight is not supported dont let the user click the button

        flashlightButton.setEnabled(false);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Error.");
        builder.setMessage("Flashlight not available on this device. \nExit?");   //  inform the user and let him choose how to continue
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                exit();
            }
        });
        builder.setNegativeButton("Stay without flashlight", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();

            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    flashlightButton.setOnClickListener(new View.OnClickListener() { //  listen for flash button clicks
        @Override
        public void onClick(View view) {
            flashOnOff();
        }
    });

    ringtone.setOnClickListener(new View.OnClickListener() {    //  Ringtone listener
        @Override
        public void onClick(View view) {
            ringtoneOnOff(ringtoneMP);
        }
    });


    client = new MqttAndroidClient(MainActivity.this, BrokerIp, clientId);
//        client = pahoMqttClient.getMqttClient(MainActivity.this,BrokerIp,clientId);


    options = new MqttConnectOptions();
    options.setUserName(Username);
    options.setPassword(Password.toCharArray());

    try {
        IMqttToken token = client.connect(options);
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Toast.makeText(MainActivity.this, "connected", Toast.LENGTH_SHORT).show();
                setSubscription(client,topicStr,qos);
            }
            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Log.w("Mqtt","Failed to connect to:"+ BrokerIp + exception.toString());
                Toast.makeText(MainActivity.this, "Failed to connect to:" +exception.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

    client.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable throwable) {
            Log.d("Connection:"," Lost");
        }

        @Override
        public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
            myMessageArrived(s,mqttMessage);
        }
        @Override
        public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
            Log.d("Delivery"," completed with iMqttDeliveryToken: " + iMqttDeliveryToken);
        }
    });

}

//Flashlight start
@Override
protected void onStop() {
    super.onStop();
    if(camera!=null)
    {
        camera.release();
        camera = null;
    }
}
//Flashlight end

//Backkey exit confirmation
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        exitByBackKey();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

protected void exitByBackKey() {

    AlertDialog alertbox = new AlertDialog.Builder(this)
            .setMessage("Do you want to exit application?")
            .setPositiveButton("Yes", new
                    DialogInterface.OnClickListener() {

                        // when yes is clicked exit the application
                        public void onClick(DialogInterface arg0, int arg1) {
                            exit();
                        }
                    })
            .setNegativeButton("No", new
                    DialogInterface.OnClickListener() {

                        // when no is clicked do nothing
                        public void onClick(DialogInterface arg0, int arg1) {
                        }
                    })
            .show();

}
//Backkey end

// PUBLISH MESSAGE
private void setSubscription(MqttAndroidClient client, String topic, int qos){
    try{
        client.subscribe(topic, qos);
    }
    catch (MqttException e){
        e.printStackTrace();
    }
}

private void unsetSubscription(MqttAndroidClient client,String topic){
    try{
        client.unsubscribe(topic);
    }catch (MqttException e){
        e.printStackTrace();
    }
}

public void conn(View v){
    try {
        IMqttToken token = client.connect(options);
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Toast.makeText(MainActivity.this, "connected", Toast.LENGTH_SHORT).show();
                setSubscription(client,topicStr,qos);
//                    pub();
            }
            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Toast.makeText(MainActivity.this, "not connected", Toast.LENGTH_SHORT).show();
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

public void disconn(View v){
    if (client.isConnected()) {
        try {
            IMqttToken token = client.disconnect();
            token.setActionCallback(new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    // We are connected
                    Toast.makeText(MainActivity.this, "disconnected", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    // Something went wrong e.g. connection timeout or firewall problems
                    Toast.makeText(MainActivity.this, "could not disconnect", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }else {
        Toast.makeText(MainActivity.this, "Client is already disconnected", Toast.LENGTH_LONG).show();
    }
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void pub(View v) {
    String topic = topicStr;
    try {

        client.publish(topic, String.valueOf(message.getText()).getBytes(),qos,false);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

private void ringtoneOnOff(MediaPlayer ringtoneMP){
    if (ringtoneMP.isPlaying()){
        ringtoneMP.pause();
    }else{
        ringtoneMP.start();
    }
}

private void myMessageArrived(String s,MqttMessage mqttMessage){
    if ((mqttMessage.toString()).equals("Flash on")) {
        if (isFlashOn()) {
            Toast.makeText(MainActivity.this, "Flashlight already on", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Turning flashlight on", Toast.LENGTH_SHORT).show();
            flashOnOff();
        }
    } else if ((mqttMessage.toString()).equals("Flash off")) {
        if (isFlashOn()) {
            Toast.makeText(MainActivity.this, "Turning flashlight off", Toast.LENGTH_SHORT).show();
            flashOnOff();
        } else {
            Toast.makeText(MainActivity.this, "Flashlight already off", Toast.LENGTH_SHORT).show();
        }
    } else if ((mqttMessage.toString()).equals("Ringtone on")) {
        if (ringtoneMP.isPlaying()) {
            Toast.makeText(MainActivity.this, "Ringtone already playing", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(MainActivity.this, "Playing ringtone", Toast.LENGTH_SHORT).show();
            ringtoneMP.start();
        }
    } else if ((mqttMessage.toString()).equals("Ringtone off")) {
        if (ringtoneMP.isPlaying()) {
            ringtoneMP.pause();
        } else {
            Toast.makeText(MainActivity.this, "Ringtone already not being played", Toast.LENGTH_SHORT).show();
        }
    } else {
        Log.d("INFO:", "This Command does not exist");
    }
    vibrator.vibrate(500);
    myRingtone.play();
}

public void exit(){
    finish();
    System.exit(0);
}

public boolean isFlashAvailable(){  //  boolean function that returns true if flash is supported on this device

    return getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}

public void flashOnOff(){   //  self explanatory

    if (this.parameters.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_ON) || this.parameters.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_TORCH)){  //  if the flash is on torch mode
        Log.d("BHKE","408");
        this.flashlightButton.setImageResource(R.drawable.off);
        this.parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);  //  turn it off
    }else{
        Log.d("BHKE","412");
        this.flashlightButton.setImageResource(R.drawable.on);
        this.parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);    //  else turn it on
    }
    this.camera.setParameters(this.parameters);
    this.camera.startPreview();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    String tempBrokerIp;
    String temptopic="";
    Integer tempqos;
    Boolean BrokerIpChanged = true;
    Boolean qosChanged = true;
    Boolean topicChanged = true;
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case (1) : {
            if (resultCode == Activity.RESULT_OK) {
                tempBrokerIp = data.getStringExtra("CURRENT_IP");
                tempqos = Integer.parseInt(data.getStringExtra("CURRENT_QOS"));
                temptopic = data.getStringExtra("CURRENT_TOPIC");

                Log.d("Info BROKER, TOPIC, QOS", ":"+ tempBrokerIp+ " " + temptopic+ " " + tempqos);
                if (tempBrokerIp.equals(BrokerIp)) {
                    BrokerIpChanged=false;
                    Log.i("BrokerIpChanged =", BrokerIpChanged.toString());
                }
                if (tempqos.equals(qos)) {
                    qosChanged=false;
                    Log.i("qosChanged =", qosChanged.toString());
                }else {
                    qos = tempqos;
                }
                if (temptopic.equals(topicStr)){
                    topicChanged=false;
                    Log.i("topicChanged =", topicChanged.toString());
                }else{
                    topicStr = temptopic;
                }
                if (!BrokerIpChanged && !qosChanged && !topicChanged) return;

                if (BrokerIpChanged){
                    try {
                        client.disconnect();

                        BrokerIp = tempBrokerIp;
                        topicStr = temptopic;
                        qos = tempqos;
//                            final String clientId = MqttClient.generateClientId();
                        client = new MqttAndroidClient(MainActivity.this, BrokerIp, clientId);


                        options = new MqttConnectOptions();
                        options.setUserName(Username);
                        options.setPassword(Password.toCharArray());
//                            options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);  //  to user the latest mqtt version

                        try {
                            IMqttToken token = client.connect(options);
                            token.setActionCallback(new IMqttActionListener() {
                                @Override
                                public void onSuccess(IMqttToken asyncActionToken) {
                                    // We are connected
                                    Toast.makeText(MainActivity.this, "connected", Toast.LENGTH_SHORT).show();
                                    Log.w("Mqtt","Connected to:"+ BrokerIp);

                                    try{
                                        Log.v("INFO 11111:", "about to subscribe with: "+ topicStr + qos);
                                        client.subscribe(topicStr,qos);
                                    }catch (MqttException e){
                                        e.printStackTrace();
                                    }
                                }
                                @Override
                                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                                    // Something went wrong e.g. connection timeout or firewall problems
                                    Log.w("Mqtt","Failed to connect to:"+ BrokerIp + exception.toString());
                                    Toast.makeText(MainActivity.this, "Failed to connect to:" +exception.toString(), Toast.LENGTH_SHORT).show();
                                }

                            });
                            System.out.println(client.isConnected());
//                                if (client.isConnected()){
//                                    Log.v("INFO 22222:", "about to subscribe with: "+ temptopic + tempqos);
//                                    client.subscribe(temptopic,tempqos);
//                                }

                        } catch (MqttException e) {
                            e.printStackTrace();
                        }

                        client.setCallback(new MqttCallback() {
                            @Override
                            public void connectionLost(Throwable throwable) {

                            }

                            @Override
                            public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                                myMessageArrived(s,mqttMessage);
                            }

                            @Override
                            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

                            }
                        });

                    }catch (MqttException e){
                        e.printStackTrace();
                    }
                }else
                {
                    try {
                        client.unsubscribe(topicStr);
                        client.subscribe(temptopic,tempqos);

                        topicStr = temptopic;
                        qos = tempqos;

                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
    }
}

public boolean isFlashOn(){
    return (this.parameters.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_ON) || this.parameters.getFlashMode().equals(android.hardware.Camera.Parameters.FLASH_MODE_TORCH));
}

我也可以提供设置活动代码

1 个答案:

答案 0 :(得分:0)

似乎flashOnOff所需的两个变量在我从设置活动返回后未初始化。

Ι补充道:

interface Circle { type: "circle"; }
interface Rectangle { type: "rectangle"; }
type Shape = Circle | Rectangle;

interface Circle { type: "circle"; }
interface Rectangle { type: "rectangle"; }
type ShapeTemplate = {
  type: Shape["type"];
};

const fromTemplate = (template: ShapeTemplate): Shape => template;

在onActivityResult开始,它就像一个魅力。