简体   繁体   中英

Android app closes on settings pressed

I've getting some troubles while learning Android, I've created an app, it is supossed to open the menu/settings when I hit the menu/settings button in my cell phone, instead my app get closed.

Also, when I click the only button I have in my app and actually do something then hit the menu/settings button it doesn't get closed and show the menu/settings.

Any ideas why is this happening?

In resumen;

  1. Open app.
  2. Hit menu/settings button.
  3. My app gets closed.

  1. Open app.
  2. Turn on the camera led in my cell phone (hitting a button to turn it on).
  3. Hit menu/settings button.
  4. My app don't get closed.
  5. Shows menu/settings options.
  6. Turn off the camera led in my cell phone (hitting a button to turn it off).
  7. Hit menu/settings button.
  8. My app gets closed.

Hope anyone can help me with this problem or give some kind of guideline or hints or what do I need to read.

Later.

Ps. Here is the code;

MainActivity.java

package com.example.test;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    Camera camera = null;
    Parameters parameters;
    Button FlashLightControl;
    boolean isOn = true;

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

        //Creamos un boton con el texto adecuado
        FlashLightControl = (Button)findViewById(R.id.btOnOff);
        FlashLightControl.setText("ENCENDER LED CAMARA");
    }

    /**
     * Control LED Camera.
     * Esta funcion se ejecuta al clickar el boton que hemos incluido
     * @param arg0
     */
    public void onClick(View arg0) {
       try{
          // Al pulsar, si el Led estaba encendido se apaga y viceversa
          if(camera == null){
             camera = Camera.open();
             parameters = camera.getParameters();
             parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
             camera.setParameters(parameters);
             FlashLightControl.setText(R.string.btOff);
          }else{
             parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
             camera.setParameters(parameters);
             camera.release();
             camera = null;
             FlashLightControl.setText(R.string.btOn);
          }
       }catch(Exception e){
          //Control errores
       }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        System.out.println("Se presiono el boton: " + keyCode);
        Log.e("--", "-----");
        if (camera == null){
            finish();
        }else{
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                moveTaskToBack(true);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btOnOff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/encender"
        android:onClick="onClick" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

UPDATE:

This is what the Catlog prints when open the app and turn on the led;

01-07 19:03:40.671: D/dalvikvm(5085): GC_CONCURRENT freed 397K, 8% free 6686K/7239K, paused 4ms+5ms
01-07 19:03:43.832: I/System.out(5085): Se presiono el boton: 82
01-07 19:03:43.832: E/--(5085): -----

boton: 82 is the button of settings and the app doesn't get closed.

And this is what happend when the settings buttons is pressed and the led isn't on;

01-07 19:06:37.334: I/System.out(5085): Se presiono el boton: 82
01-07 19:06:37.334: E/--(5085): -----
01-07 19:06:37.391: D/OpenGLRenderer(5085): Flushing caches (mode 0)
01-07 19:06:37.411: D/OpenGLRenderer(5085): Flushing caches (mode 1)

Don't know how this help but hope it does.

Your code did exactly what you described. To stop close your app, comment this out:

if (camera == null){
        //finish();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM