Flashlight ON/OFF App is crashing - java

I found a example for switching the light on the phone here :
http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/
So I snipped some code for my App and got a Error.
03-02 21:31:28.066: E/AndroidRuntime(1591): FATAL EXCEPTION: main
03-02 21:31:28.066: E/AndroidRuntime(1591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kaltech.led/com.kaltech.led.ActivityMAIN}: java.lang.NullPointerException
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.os.Looper.loop(Looper.java:137)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-02 21:31:28.066: E/AndroidRuntime(1591): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 21:31:28.066: E/AndroidRuntime(1591): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 21:31:28.066: E/AndroidRuntime(1591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-02 21:31:28.066: E/AndroidRuntime(1591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-02 21:31:28.066: E/AndroidRuntime(1591): at dalvik.system.NativeStart.main(Native Method)
03-02 21:31:28.066: E/AndroidRuntime(1591): Caused by: java.lang.NullPointerException
03-02 21:31:28.066: E/AndroidRuntime(1591): at com.kaltech.led.ActivityMAIN.onCreate(ActivityMAIN.java:40)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.Activity.performCreate(Activity.java:5008)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-02 21:31:28.066: E/AndroidRuntime(1591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-02 21:31:28.066: E/AndroidRuntime(1591): ... 11 more
Edit :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bat_status = (ImageView)findViewById(R.id.bat_stat);
mySwitch = (Switch) findViewById(R.id.switch_signal);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
mySwitch.setChecked(false);
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){ //EIN
bat_status.setImageResource(R.drawable.bat_signal_1);
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;
}
else{ //AUS
bat_status.setImageResource(R.drawable.bat_signal_2);
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
}
}
});
}
Edit 2:
final Parameters p = camera.getParameters(); //Line 40

You're getting NullPointerException on line 40, which is:
final Parameters p = camera.getParameters();
Most likely the camera object is null. You're initializing it earlier with:
camera = Camera.open();
and Camera.open():
Creates a new Camera object to access the first back-facing camera on the device. If the device does not have a back-facing camera, this returns null.
Check if your camera is not null and proceed only then, eg.:
camera = Camera.open();
if (camera != null) {
//normal code
}
else {
//ERROR, camera is null
}
You might be getting this because:
the device does not have back-facing camera
you forgot to add
<uses-permission android:name="android.permission.CAMERA" />
in your AndroidManifest.

Related

read txt file into 2d array force exit

firstly i am sorry for my broken english. I wanna read txt file and into 2d array. this code is succesfully running on java. but android emulator gives warning message: "Sorry! the application has stopped unexpectedly. please try again"
how can I fix that? thanks
here is my code:
package com.example.hocam;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Konular extends ActionBarActivity {
private ListView konuListe;
private List<KonuBilgileri> konuBilgileri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_konular);
Bundle dersadi = getIntent().getExtras();
String dersinadi= dersadi.getString("Ders");
switch(dersinadi) {
case "Turkce":
KonuGetir turkce=new KonuGetir("turkce.txt");
String[][] turkcekonular=turkce.getKonular();
System.out.println(turkcekonular[0][0]); // it is the problem and give error
konuListe = (ListView) findViewById(R.id.konu_listesi);
konuBilgileri = new ArrayList<KonuBilgileri>();
konuBilgileri.add(new KonuBilgileri(turkcekonular[0][0],R.drawable.turkce, turkcekonular[0][1])); // array is problem
MyAdapter adapter = new MyAdapter(Konular.this, R.layout.custom_list_item, konuBilgileri);
konuListe.setAdapter(adapter);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.konular, 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);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class KonuGetir {
final int maxLines = 10100;
String[][] resultArray = new String[maxLines][];
public KonuGetir(String Ders){
File file = new File(Ders);
Scanner scanner;
try {
scanner = new Scanner(file,"utf-8");
int linesCounter = 0;
while (scanner.hasNextLine() && linesCounter < maxLines) {
resultArray[linesCounter] = scanner.nextLine().split("#");
linesCounter++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[][] getKonular() {
return resultArray;
}
}
logfile:
> 09-04 16:18:22.262: E/AndroidRuntime(1164): FATAL EXCEPTION: main
09-04 16:18:22.262: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:123)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:18:22.262: E/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:18:22.262: E/AndroidRuntime(1164): at dalvik.system.NativeStart.main(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164): Caused by: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:18:22.262: E/AndroidRuntime(1164): ... 11 more
09-04 16:42:10.422: E/AndroidRuntime(1178): FATAL EXCEPTION: main
09-04 16:42:10.422: E/AndroidRuntime(1178): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.os.Looper.loop(Looper.java:123)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:42:10.422: E/AndroidRuntime(1178): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:42:10.422: E/AndroidRuntime(1178): at dalvik.system.NativeStart.main(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178): Caused by: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:42:10.422: E/AndroidRuntime(1178): ... 11 more
09-04 16:43:28.802: E/AndroidRuntime(1207): FATAL EXCEPTION: main
09-04 16:43:28.802: E/AndroidRuntime(1207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.os.Looper.loop(Looper.java:123)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:43:28.802: E/AndroidRuntime(1207): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:43:28.802: E/AndroidRuntime(1207): at dalvik.system.NativeStart.main(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207): Caused by: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:43:28.802: E/AndroidRuntime(1207): ... 11 more
android manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".YGS"
android:label="#string/title_activity_ygs" >
</activity>
<activity
android:name=".Konular"
android:label="#string/title_activity_konular" >
</activity>
<activity
android:name=".Video"
android:screenOrientation="landscape"
android:label="#string/title_activity_video" >
</activity>
</application>
You are using the following code:
String[][] turkcekonular=turkce.getKonular();
System.out.println(turkcekonular[0][0]);
But get java.lang.NullPointerException.
The reason for this because getKonular() returns an array initalised to size 10100, however scanner does not contain any lines so scanner.hasNextLine() is false.
This means that turkcekonular[0] is null, as it was never set.
Which means that turkcekonular[0][0] is trying to access a null object, so throws a NullPointerException
You should check if the object is null before trying to access it..
if(turkcekonular[0] != null)
System.out.println(turkcekonular[0][0]);
and else where that this issue occurs.
(and you should figure out why your file is not read. Is it named correctly and exists in the specified path?)

Null Pointer Exception in Bitmap.decodeResources() from xml resource

In my current android project I create two bitmaps from xml resources. Every time I build my project using the xml bitmaps as the resource for Bitmap.decodeResources() I get a null pointer exception when I try to access the bitmaps. But when I switch to using the straight .png the error goes away.
Why is decodeResources() returning null?
code where the bitmaps are created:
public OvertButton(float x, float y, float width, float height, int pressedId, int unPressedId, Resources res, boolean visible) {
\\these are null if xml bitmap is passed
pressed = BitmapFactory.decodeResource(res, pressedId);
unPressed = BitmapFactory.decodeResource(res, unPressedId);
this.visible = visible;
relocate(x, y);
resize(width, height); //throws NPE when I createScaledBitmap()
ClickableManager.registerClickable(this);
}
resource file
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/play_button_pressed"
android:filter="false"
android:antialias="false"
android:dither="false"/>
and the resize method
#Override
public void resize(float width, float height) {
box.set(box.left, box.top, box.left + width, box.top + height);
Log.d("resize",String.valueOf(height));
pressed = Bitmap.createScaledBitmap(pressed, (int)width, (int)height, true);
unPressed = Bitmap.createScaledBitmap(unPressed, (int)width, (int)height, true);
}
And the logcat
04-16 09:45:06.341 11824-11824/com.handmade.eed D/dalvikvm﹕ Late-enabling CheckJNI
04-16 09:45:06.450 11824-11824/com.handmade.eed D/MenuActivity﹕ 2130837594$$$2130837596
04-16 09:45:06.451 11824-11824/com.handmade.eed D/skia﹕ --- SkImageDecoder::Factory returned null
04-16 09:45:06.451 11824-11824/com.handmade.eed D/OvertButton﹕ true2130837591
04-16 09:45:06.457 11824-11824/com.handmade.eed D/OvertButton﹕ false2130837595
04-16 09:45:06.457 11824-11824/com.handmade.eed D/resize﹕ 252.0
04-16 09:45:06.458 11824-11824/com.handmade.eed D/AndroidRuntime﹕ Shutting down VM
04-16 09:45:06.459 11824-11824/com.handmade.eed W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41610d40)
04-16 09:45:06.461 11824-11824/com.handmade.eed E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.handmade.eed, PID: 11824
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.handmade.eed/com.handmade.eed.MenuActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
at com.handmade.overt.visible.OvertButton.resize(OvertButton.java:61)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:29)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:34)
at com.handmade.eed.MenuActivity.onCreate(MenuActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
04-16 09:45:09.872 11824-11824/com.handmade.eed I/Process﹕ Sending signal. PID: 11824 SIG: 9
This is a case of me being dumb but here is the answer for posterity.
Bitmap is not the same as BitmapDrawable
From the Drawable Resources link above:
COMPILED RESOURCE DATATYPE:
Resource pointer to a BitmapDrawable.
Thus I had to change this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed_res); //this is the id for the xml referring to the png
to either this:
Drawable pressed = res.getDrawable(R.drawable.play_button_pressed_res);
or this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed); //this is the id for the png
I decided to use bitmap because, as I understand, it is faster.

How can i remove nullPointerException

I am getting Null Pointer Exception in my code in on click method.Please suggest me how can i remove it.
package co.sds.iitr.bullsandcows;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
EditText Num;
Button done;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Num = (EditText) findViewById(R.id.etNum);
done = (Button) findViewById(R.id.btDone);
done.setOnClickListener(this);
}
#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 void onClick(View v)
{
// TODO Auto-generated method stub
if(v.getId()== R.id.btDone)
{
String num = Num.getText().toString();
int n = Integer.getInteger(num);
Toast.makeText(getApplicationContext(),"Your number is saved",
Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(),GuessActivtiy.class);
i.putExtra("num", n);
startActivity(i);
}
else
Toast.makeText(getApplicationContext(),"Not Found",
Toast.LENGTH_LONG).show();
}
}
GussActivity is my another activity class in which i am trying to pass my values through intent.
My log cat is looking like this.
03-02 02:01:22.080: D/gralloc_goldfish(901): Emulator without GPU emulation detected.
03-02 02:01:27.570: D/AndroidRuntime(901): Shutting down VM
03-02 02:01:27.570: W/dalvikvm(901): threadid=1: thread exiting with uncaught exception (group=0xb3aaeba8)
03-02 02:01:27.650: E/AndroidRuntime(901): FATAL EXCEPTION: main
03-02 02:01:27.650: E/AndroidRuntime(901): Process: co.sds.iitr.bullsandcows, PID: 901
03-02 02:01:27.650: E/AndroidRuntime(901): java.lang.NullPointerException
03-02 02:01:27.650: E/AndroidRuntime(901): at co.sds.iitr.bullsandcows.MainActivity.onClick(MainActivity.java:45)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.view.View.performClick(View.java:4438)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.view.View$PerformClick.run(View.java:18422)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.os.Handler.handleCallback(Handler.java:733)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.os.Handler.dispatchMessage(Handler.java:95)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.os.Looper.loop(Looper.java:136)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-02 02:01:27.650: E/AndroidRuntime(901): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:01:27.650: E/AndroidRuntime(901): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 02:01:27.650: E/AndroidRuntime(901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-02 02:01:27.650: E/AndroidRuntime(901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-02 02:01:27.650: E/AndroidRuntime(901): at dalvik.system.NativeStart.main(Native Method)
Please help me to resolve this.
Replace getInteger method with parseInt, to cast the String to Integer use parseInt method
As per the java doc
getInteger(String nm)
Determines the integer value of the system property with the specified name.If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.
You just try like this inside the MainActivity....
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent (MainActivity.this , GuessActivtiy.class);
intent.putExtra("num", n);
startActivity(intent);
}
});

App using camera flash as a torch using Eclipse not working

I have been working on an app to use a phone's/tablet's camera flash as a flashlight. Everything seemed to be working fine but when I tested it on my Droid Bionic running Android 4.1.2, the app failed to turn on the flash even though it said it did. Here is the java code I used:
package com.example.flash;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean isFlashOn = false;
private Camera camera;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
if(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
Toast.makeText(getApplicationContext(),
"Your device doesn't have camera!",Toast.LENGTH_SHORT).show();
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isFlashOn = false;
button.setText("Tap to turn flashlight on.");
}
else {
Log.i("info", "torch is turned on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
button.setText("Tap to turn flashlight off.");
}
}
});
}
#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
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}}
Is this code correct or did I miss something?
Logcat:
07-03 18:48:29.064: E/Trace(773): error opening trace file: No such file or directory (2)
07-03 18:48:30.535: D/Camera(773): app passed NULL surface
07-03 18:48:31.023: D/libEGL(773): loaded /system/lib/egl/libEGL_emulation.so
07-03 18:48:31.073: D/(773): HostConnection::get() New Host Connection established 0x2a13c3c0, tid 773
07-03 18:48:31.123: D/libEGL(773): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-03 18:48:31.173: D/libEGL(773): loaded /system/lib/egl/libGLESv2_emulation.so
07-03 18:48:31.406: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:48:31.433: D/OpenGLRenderer(773): Enabling debug mode 0
07-03 18:48:31.723: I/Choreographer(773): Skipped 58 frames! The application may be doing too much work on its main thread.
07-03 18:49:05.923: D/dalvikvm(773): GC_CONCURRENT freed 202K, 12% free 2623K/2956K, paused 74ms+25ms, total 234ms
07-03 18:49:06.216: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:09.584: D/Camera(773): app passed NULL surface
07-03 18:49:09.853: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:11.813: I/info(773): torch is turned on!
07-03 18:49:13.467: I/info(773): torch is turned off!
07-03 18:49:16.263: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:16.713: D/AndroidRuntime(773): Shutting down VM
07-03 18:49:16.713: W/dalvikvm(773): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-03 18:49:16.936: E/AndroidRuntime(773): FATAL EXCEPTION: main
07-03 18:49:16.936: E/AndroidRuntime(773): java.lang.RuntimeException: Method called after release()
07-03 18:49:16.936: E/AndroidRuntime(773): at android.hardware.Camera._stopPreview(Native Method)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.hardware.Camera.stopPreview(Camera.java:543)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.example.flash.MainActivity.surfaceDestroyed(MainActivity.java:140)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.SurfaceView.updateWindow(SurfaceView.java:553)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:231)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.View.dispatchWindowVisibilityChanged(View.java:7544)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1211)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Handler.handleCallback(Handler.java:725)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Looper.loop(Looper.java:137)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-03 18:49:16.936: E/AndroidRuntime(773): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:49:16.936: E/AndroidRuntime(773): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 18:49:16.936: E/AndroidRuntime(773): at dalvik.system.NativeStart.main(Native Method)
07-03 18:49:24.854: E/Trace(811): error opening trace file: No such file or directory (2)
07-03 18:49:25.413: D/libEGL(811): loaded /system/lib/egl/libEGL_emulation.so
07-03 18:49:25.567: D/(811): HostConnection::get() New Host Connection established 0x2a15f570, tid 811
07-03 18:49:25.643: D/libEGL(811): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-03 18:49:25.663: D/libEGL(811): loaded /system/lib/egl/libGLESv2_emulation.so
07-03 18:49:25.934: W/EGL_emulation(811): eglSurfaceAttrib not implemented
07-03 18:49:25.963: D/OpenGLRenderer(811): Enabling debug mode 0
07-03 18:53:12.298: D/Camera(811): app passed NULL surface
07-03 18:53:12.723: D/dalvikvm(811): GC_CONCURRENT freed 172K, 11% free 2600K/2904K, paused 9ms+165ms, total 421ms
07-03 18:53:12.934: E/EGL_emulation(811): rcCreateWindowSurface returned 0
07-03 18:53:12.934: E/EGL_emulation(811): tid 811: eglCreateWindowSurface(631): error 0x3003 (EGL_BAD_ALLOC)
07-03 18:53:12.943: D/AndroidRuntime(811): Shutting down VM
07-03 18:53:12.943: W/dalvikvm(811): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-03 18:53:13.033: E/AndroidRuntime(811): FATAL EXCEPTION: main
07-03 18:53:13.033: E/AndroidRuntime(811): java.lang.RuntimeException: createWindowSurface failed EGL_BAD_ALLOC
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.createSurface(HardwareRenderer.java:1064)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.createEglSurface(HardwareRenderer.java:961)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.initialize(HardwareRenderer.java:787)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1502)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Handler.handleCallback(Handler.java:725)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Looper.loop(Looper.java:137)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-03 18:53:13.033: E/AndroidRuntime(811): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:53:13.033: E/AndroidRuntime(811): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 18:53:13.033: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 18:53:13.033: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 18:53:13.033: E/AndroidRuntime(811): at dalvik.system.NativeStart.main(Native Method)
UPDATE
I think the key is you are running Android 4.1.2. Since Android 4.0, if you want to use the Camera Device, even if you only want to use the flash, you are forced to use a SurfaceView.
In the previous answer (below), I gave you a link to a Torch app which uses SurfaceView. Try it or adapt it to your code.
PREVIOUS ANSWER:
As stated in many other cases (like this one), you may be facing a Device-Specific issue that is quite common in the Android world.
Although getSupportedFlashModes() may return FLASH_MODE_TORCH on nearly every device, many of them don't actually support it.
Anyway, you could try these:
Use camera.startPreview(); after camera = Camera.open();
Try setting FLASH_MODE_OFF initially (before camera.startPreview();).
Check if this Torch app works in your device. In case it does, you have the source code to compare it to yours.
Download a Torch app from the Play Store to test if it's a device issue or not.
Post the issue in a Droid Bionic support forum.
UPDATE: I would say the final keyword is a problem in your code. Try changing it to:
//camera = Camera.open();
//final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
cam.stopPreview();
cam.release();
isFlashOn = false;
button.setText("Tap to turn flashlight on.");
}
else {
Log.i("info", "torch is turned on!");
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.startPreview();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
button.setText("Tap to turn flashlight off.");
}
}
});
user the permission "android.permission.FLASHLIGHT" in the manifest
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />

Unfortunately app has stopped working

I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display is supposed to reference a TextView instance, but you're explictly casting into a Button, and these are not compatible types.
As you got the answer from A-C this time, but for next time in android application development a important suggestion:
Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error.
And try to find what can be wrong with that line of code.
For example: in your logcat it is showing-
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button. So you can easily remove your errors without any help.
Hope that helps not only you current problem but prevented your future time and efforts.

Categories