문제

the logcat shows something like button does not exist,but i couldn't find any mistake there. The app force closes when i click the flashlight button in the main activity page. I am new to android programming. please help.

LogCat:

 02-04 21:36:29.182: E/AndroidRuntime(798): FATAL EXCEPTION: main
02-04 21:36:29.182: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iamtheonewhoknocks.toolkit/com.iamtheonewhoknocks.toolkit.FlashlightActivity}: java.lang.IllegalArgumentException: Button does not exist
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.os.Looper.loop(Looper.java:123)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-04 21:36:29.182: E/AndroidRuntime(798):  at java.lang.reflect.Method.invokeNative(Native Method)
02-04 21:36:29.182: E/AndroidRuntime(798):  at java.lang.reflect.Method.invoke(Method.java:507)
02-04 21:36:29.182: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-04 21:36:29.182: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-04 21:36:29.182: E/AndroidRuntime(798):  at dalvik.system.NativeStart.main(Native Method)
02-04 21:36:29.182: E/AndroidRuntime(798): Caused by: java.lang.IllegalArgumentException: Button does not exist
02-04 21:36:29.182: E/AndroidRuntime(798):  at com.android.internal.app.AlertController.setButton(AlertController.java:288)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.AlertDialog.setButton(AlertDialog.java:161)
02-04 21:36:29.182: E/AndroidRuntime(798):  at com.iamtheonewhoknocks.toolkit.FlashlightActivity.onCreate(FlashlightActivity.java:42)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-04 21:36:29.182: E/AndroidRuntime(798):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-04 21:36:29.182: E/AndroidRuntime(798):  ... 11 more

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:background="#151515"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:focusable="true"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/flashButton"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:background="@drawable/flashlight"
        android:contentDescription="@string/flashlighticon"
        android:drawableTop="@drawable/flashlight" />

    <ImageButton
        android:id="@+id/convertorButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/flashButton"
        android:layout_centerHorizontal="true"
        android:background="@drawable/converter"
        android:contentDescription="@string/convertericon" />

    <ImageButton
        android:id="@+id/mglassButton"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_alignLeft="@+id/compassButton"
        android:layout_alignTop="@+id/mirrorButton"
        android:background="@drawable/mglass"
        android:contentDescription="@string/mglassicon" />

     <ImageButton
         android:id="@+id/bubbleButton"
         android:layout_width="60dip"
         android:layout_height="60dip"
         android:layout_alignLeft="@+id/convertorButton"
         android:layout_below="@+id/flashButton"
         android:layout_marginTop="87dp"
         android:background="@drawable/bubble"
         android:contentDescription="@string/bubbleicon" />

     <ImageButton
         android:id="@+id/compassButton"
         android:layout_width="60dip"
         android:layout_height="60dip"
         android:layout_above="@+id/mirrorButton"
         android:layout_alignLeft="@+id/flashButton"
         android:background="@drawable/compass"
         android:contentDescription="@string/compassicon" />

     <ImageButton
         android:id="@+id/mirrorButton"
         android:layout_width="60dip"
         android:layout_height="60dip"
         android:layout_alignLeft="@+id/bubbleButton"
         android:layout_below="@+id/bubbleButton"
         android:layout_marginTop="83dp"
         android:background="@drawable/mirror"
         android:contentDescription="@string/mirroricon" />

     <ImageButton
         android:id="@+id/timerButton"
         android:layout_width="60dip"
         android:layout_height="60dip"
         android:layout_above="@+id/bubbleButton"
         android:layout_alignLeft="@+id/calcButton"
         android:background="@drawable/timer"
         android:contentDescription="@string/timericon" />

     <ImageButton
         android:id="@+id/calcButton"
         android:layout_width="60dip"
         android:layout_height="60dip"
         android:layout_above="@+id/mirrorButton"
         android:layout_alignParentRight="true"
         android:background="@drawable/calculator"
         android:contentDescription="@string/calcicon" />

     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/flashButton"
         android:layout_below="@+id/flashButton"
         android:layout_marginTop="19dp"
         android:gravity="center"
         android:text="@string/flashlighticon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <TextView
         android:id="@+id/textView4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/compassButton"
         android:layout_below="@+id/compassButton"
         android:layout_marginTop="17dp"
         android:gravity="center"
         android:text="@string/compassicon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <TextView
         android:id="@+id/textView6"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/textView5"
         android:layout_alignBottom="@+id/textView5"
         android:layout_alignRight="@+id/calcButton"
         android:gravity="center"
         android:text="@string/calcicon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <TextView
         android:id="@+id/textView7"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/mglassButton"
         android:layout_below="@+id/mglassButton"
         android:layout_marginTop="16dp"
         android:gravity="center"
         android:text="@string/mglassicon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <TextView
         android:id="@+id/textView8"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/textView7"
         android:layout_alignBottom="@+id/textView7"
         android:layout_alignLeft="@+id/mirrorButton"
         android:layout_alignRight="@+id/textView5"
         android:gravity="center"
         android:text="@string/mirroricon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <TextView
         android:id="@+id/textView3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/textView2"
         android:layout_alignBottom="@+id/textView2"
         android:layout_alignLeft="@+id/timerButton"
         android:layout_alignRight="@+id/timerButton"
         android:gravity="center"
         android:text="@string/timericon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <TextView
         android:id="@+id/textView2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/textView1"
         android:layout_alignBottom="@+id/textView1"
         android:layout_centerHorizontal="true"
         android:gravity="center"
         android:text="@string/convertericon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <TextView
         android:id="@+id/textView5"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/textView4"
         android:layout_alignBottom="@+id/textView4"
         android:layout_alignRight="@+id/textView2"
         android:gravity="center"
         android:text="@string/bubbleicon"
         android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

MainActivity.java

package com.iamtheonewhoknocks.toolkit;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;


public class MainActivity extends Activity {

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

        flashlightButton();
        calculatorButton();
        bubbleButton();
    }

    private void flashlightButton()
    {  ImageButton flButton= (ImageButton) findViewById(R.id.flashButton);
        flButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,FlashlightActivity.class));

            }
        });
    }

        private void calculatorButton()
        {  ImageButton flButton= (ImageButton) findViewById(R.id.calcButton);
            flButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this,CalculatorActivity.class));

                }
            });
       }

        private void bubbleButton()
        {  ImageButton flButton= (ImageButton) findViewById(R.id.bubbleButton);
            flButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this,BubbleActivity.class));

                }
            });
        }

    /*@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;
    } */


}

activity_flashlight.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:background="#000000"
    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=".FlashlightActivity" >

    <ImageButton
        android:id="@+id/btnSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/flashlight_off"
        android:contentDescription="@null" />

</RelativeLayout>

FlashlightActivity.java

package com.iamtheonewhoknocks.toolkit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
//import android.view.View.OnClickListener;

public class FlashlightActivity extends Activity  {
ImageButton switchButton;

private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flashlight);
    switchButton = (ImageButton) findViewById(R.id.btnSwitch);
    // First check if device is supporting flashlight or not        
    hasFlash = getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        // device doesn't support flash
        // Show alert message and close the application
        AlertDialog alert = new AlertDialog.Builder(FlashlightActivity.this)
                .create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device doesn't support flash light!");
        alert.setButton(0,"OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // closing the application
                finish();
            }
        });
        alert.show();
        return;
    }

    // get the camera
    getCamera();

    // displaying button image
    toggleButtonImage();


    // Switch button click event to toggle flash on/off
    switchButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isFlashOn) {
                // turn off flash
                turnOffFlash();
            } else {
                // turn on flash
                turnOnFlash();
            }
        }
    });
}


// Get the camera
private void getCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
        }
    }
}


 // Turning On flash
private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
       // playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;

        // changing button/switch image
        toggleButtonImage();
    }

}


// Turning Off flash
private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
   //     playSound();

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOn = false;

        // changing button/switch image
        toggleButtonImage();
    }
}
/*
 * Toggle switch button images
 * changing image states to on / off
 * */
private void toggleButtonImage(){
    if(isFlashOn){
        switchButton.setImageResource(R.drawable.flashlight_on);
    }else{
        switchButton.setImageResource(R.drawable.flashlight_off);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onPause() {
    super.onPause();

    // on pause turn off the flash
    turnOffFlash();
}

@Override
protected void onRestart() {
    super.onRestart();
}

@Override
protected void onResume() {
    super.onResume();

    // on resume turn on the flash
    if(hasFlash)
        turnOnFlash();
}

@Override
protected void onStart() {
    super.onStart();

    // on starting the app get the camera params
    getCamera();
}

@Override
protected void onStop() {
    super.onStop();

    // on stop release the camera
    if (camera != null) {
        camera.release();
        camera = null;
    }
}

/*

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.flashlight, menu);
    return true;
} */

}

manifest file

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

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

    <uses-permission android:name="android.permission.CAMERA" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.iamtheonewhoknocks.toolkit.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>
        <activity
            android:name="com.iamtheonewhoknocks.toolkit.FlashlightActivity"
            android:label="@string/title_activity_flashlight" >
        </activity>
        <activity
            android:name="com.iamtheonewhoknocks.toolkit.CalculatorActivity"
            android:label="@string/title_activity_calculator" >
        </activity>
        <activity
            android:name="com.iamtheonewhoknocks.toolkit.TimerActivity"
            android:label="@string/title_activity_timer" >
        </activity>
        <activity
            android:name="com.iamtheonewhoknocks.toolkit.BubbleActivity"
            android:label="@string/title_activity_bubble" >
        </activity>
    </application>

</manifest>
도움이 되었습니까?

해결책

You should change this section

alert.setButton(0,"OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // closing the application
            finish();
        }
    });

to

alert.setButton(DialogInterface.BUTTON_POSITIVE,"OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // closing the application
            finish();
        }
    });

The error appears because the value 0 is unknown to the method you're using.

다른 팁

Take a look at what the doc says,

This method was deprecated in API level 3. Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_POSITIVE

so instead of passing the int (0) directly, you should use DialogInterface.BUTTON_POSITIVE. Also, you can directly use

alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // closing the application
            finish();
        }
    });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top