Android Custom Dialog implies a Force Close - java

I'm calling a dialog function on my code, but everytime I test it it makes a force close...
Here's my code:
public void onLongPressFinished(MotionEvent arg0,
ManagedOverlay arg1, GeoPoint arg2, ManagedOverlayItem item) {
if (item!= null) // if the user longpresses a marker, it will pop up the route dialog
{
RouteDialog dialog = new RouteDialog();
dialog.showCustomDialog();
}
and the class I use to implement the dialog:
package com.BuStopTracker;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RouteDialog extends Activity{
public void showCustomDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.routetodialog);//loads layout from xml file
dialog.setTitle("Título do custom dialog");
final Button ok = (Button) dialog.findViewById(R.id.bt_ok);
final Button cancelar = (Button) dialog.findViewById(R.id.bt_cancel);
final EditText editText = (EditText) dialog.findViewById(R.id.inputText);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//OK button action
}
});
cancelar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
I know that my "LongPress detector" function is working, because I tested it with a toast, and it worked fine...
So, can someone help me? I'm pretty sure my fault is in the way I call the dialog, I just can't see why or where...
Thanks.
PS: As requested, here's the logcat:
11-13 20:13:54.789: D/dalvikvm(406): GC_CONCURRENT freed 1153K, 54% free 3159K/6727K, external 2002K/2137K, paused 4ms+4ms
11-13 20:13:55.109: D/dalvikvm(406): GC_CONCURRENT freed 616K, 51% free 3363K/6727K, external 2002K/2137K, paused 5ms+7ms
11-13 20:13:55.369: D/dalvikvm(406): GC_EXTERNAL_ALLOC freed 721K, 54% free 3116K/6727K, external 2121K/2137K, paused 52ms
11-13 20:13:55.829: I/MapActivity(406): Handling network change notification:CONNECTED
11-13 20:13:55.829: E/MapActivity(406): Couldn't get connection factory client
11-13 20:13:55.979: D/dalvikvm(406): GC_EXTERNAL_ALLOC freed 215K, 56% free 3020K/6727K, external 2440K/2692K, paused 77ms
11-13 20:14:17.679: D/AndroidRuntime(406): Shutting down VM
11-13 20:14:17.679: W/dalvikvm(406): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-13 20:14:17.719: E/AndroidRuntime(406): FATAL EXCEPTION: main
11-13 20:14:17.719: E/AndroidRuntime(406): java.lang.IllegalStateException: System services not available to Activities before onCreate()
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.Activity.getSystemService(Activity.java:3536)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.Dialog.<init>(Dialog.java:141)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.Dialog.<init>(Dialog.java:123)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.BuStopTracker.RouteDialog.showCustomDialog(RouteDialog.java:23)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.BuStopTracker.BusTrackerBetaActivity$7.onLongPressFinished(BusTrackerBetaActivity.java:278)
11-13 20:14:17.719: E/AndroidRuntime(406): at de.android1.overlaymanager.ManagedOverlayGestureDetector.invokeLongPressFinished(ManagedOverlayGestureDetector.java:76)
11-13 20:14:17.719: E/AndroidRuntime(406): at de.android1.overlaymanager.ManagedOverlay.draw(ManagedOverlay.java:79)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.google.android.maps.Overlay.draw(Overlay.java:179)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.google.android.maps.MapView.onDraw(MapView.java:530)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.View.draw(View.java:6880)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.View.draw(View.java:6883)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.View.draw(View.java:6883)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewRoot.draw(ViewRoot.java:1522)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.os.Looper.loop(Looper.java:130)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-13 20:14:17.719: E/AndroidRuntime(406): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 20:14:17.719: E/AndroidRuntime(406): at java.lang.reflect.Method.invoke(Method.java:507)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-13 20:14:17.719: E/AndroidRuntime(406): at dalvik.system.NativeStart.main(Native Method)
11-13 20:14:20.059: I/Process(406): Sending signal. PID: 406 SIG: 9

You should not extend Activity in your RouteDialog. Pass a Context into showCustomDialog or make a contructor taking it and store it in your class.
To fix is easy change this, in RouteDialog:
public class RouteDialog {
public void showCustomDialog(Context context){
final Dialog dialog = new Dialog(context);
/* leave rest as is */
And then change your code to show the dialog to:
RouteDialog dialog = new RouteDialog();
dialog.showCustomDialog(this);

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?)

Android Radio Player m3u8 error

04-24 21:51:06.092: I/global(406): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-24 21:51:06.296: D/PlsParser(406): Playlist Url = http:/ / 85 dot 204 dot 233 dot 122/radio-01/stream-high-02/playlist.m3u8?wowzasessionid=1094958256
04-24 21:51:06.296: I/System.out(406): Playlist: Selected url - http:/ / 85 dot 204 dot 233 dot 122/radio-01/stream-high-02/playlist.m3u8?wowzasessionid=1094958256
04-24 21:51:06.616: E/MediaPlayer(406): error (1, -2147483648)
04-24 21:51:06.622: E/MediaPlayer(406): Error (1,-2147483648)
04-24 21:51:06.622: I/System.out(406): RadioService: stop
04-24 21:51:06.632: E/MediaPlayer(406): stop called in state 0
04-24 21:51:06.632: E/MediaPlayer(406): error (-38, 0)
04-24 21:51:06.652: V/ERROR(406): MEDIA ERROR UNKNOWN -2147483648
04-24 21:51:06.672: E/MediaPlayer(406): stop called in state 1
04-24 21:51:06.672: E/MediaPlayer(406): error (-38, 0)
04-24 21:51:06.692: D/PlsParser(406): Setting next stream from playlist: 0
04-24 21:51:06.692: D/PlsParser(406): URL = http:/ / 85 dot 204 dot 233 dot 122/radio-01/stream-high-02/playlist.m3u8?wowzasessionid=1094958256
04-24 21:51:06.822: I/global(406): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-24 21:51:07.002: D/PlsParser(406): Playlist Url = http:/ / 85 dot 204 dot 233 dot 122:1935/radio-01/_definst_/stream-high-02/key.m3u8key?wowzasessionid=1094958256"
04-24 21:51:07.002: W/dalvikvm(406): threadid=18: thread exiting with uncaught exception (group=0x4001d800)
04-24 21:51:07.002: E/AndroidRuntime(406): FATAL EXCEPTION: Thread-21
04-24 21:51:07.002: E/AndroidRuntime(406): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
04-24 21:51:07.002: E/AndroidRuntime(406): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
04-24 21:51:07.002: E/AndroidRuntime(406): at java.util.ArrayList.get(ArrayList.java:311)
04-24 21:51:07.002: E/AndroidRuntime(406): at com.my.radio.RadioService$3.run(RadioService.java:294)
04-24 21:51:07.002: E/AndroidRuntime(406): at java.lang.Thread.run(Thread.java:1096)
04-24 21:51:07.432: D/webviewglue(406): nativeDestroy view: 0x3037c0
04-24 21:51:07.442: D/webviewglue(406): nativeDestroy view: 0x303450
04-24 21:51:08.022: D/dalvikvm(406): GC_EXPLICIT freed 5811 objects / 296264 bytes in 501ms
04-24 21:51:08.412: E/ActivityThread(406): Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.BaseActivity$1#45fd26a8 that was originally bound here
04-24 21:51:08.412: E/ActivityThread(406): android.app.ServiceConnectionLeaked: Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.BaseActivity$1#45fd26a8 that was originally bound here
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ContextImpl.bindService(ContextImpl.java:863)
04-24 21:51:08.412: E/ActivityThread(406): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
04-24 21:51:08.412: E/ActivityThread(406): at com.my.radio.BaseActivity.onCreate(BaseActivity.java:39)
04-24 21:51:08.412: E/ActivityThread(406): at com.my.radio.MainActivity.onCreate(MainActivity.java:79)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-24 21:51:08.412: E/ActivityThread(406): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:51:08.412: E/ActivityThread(406): at android.os.Looper.loop(Looper.java:123)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 21:51:08.412: E/ActivityThread(406): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:51:08.412: E/ActivityThread(406): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 21:51:08.412: E/ActivityThread(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 21:51:08.412: E/ActivityThread(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 21:51:08.412: E/ActivityThread(406): at dalvik.system.NativeStart.main(Native Method)
04-24 21:51:08.463: E/ActivityThread(406): Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.MainActivity$2#45fd30d8 that was originally bound here
04-24 21:51:08.463: E/ActivityThread(406): android.app.ServiceConnectionLeaked: Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.MainActivity$2#45fd30d8 that was originally bound here
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ContextImpl.bindService(ContextImpl.java:863)
04-24 21:51:08.463: E/ActivityThread(406): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
04-24 21:51:08.463: E/ActivityThread(406): at com.my.radio.MainActivity.onCreate(MainActivity.java:85)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-24 21:51:08.463: E/ActivityThread(406): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:51:08.463: E/ActivityThread(406): at android.os.Looper.loop(Looper.java:123)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 21:51:08.463: E/ActivityThread(406): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:51:08.463: E/ActivityThread(406): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 21:51:08.463: E/ActivityThread(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 21:51:08.463: E/ActivityThread(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 21:51:08.463: E/ActivityThread(406): at dalvik.system.NativeStart.main(Native Method)
04-24 21:51:09.452: I/Process(406): Sending signal. PID: 406 SIG: 9
welcome to Stackoverflow.com :), i think you have an IndexOutofBounds Exception: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
this is an example of how i play an .M3U8 using a VideoView:
private VideoView myvideoview;
private String urlStream;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String streamV = "http://media.jorgesys.com/reforma.m3u8";
new playM3U8().execute(streamV);
myvideoview = (VideoView)this.findViewById(R.id.myvideoview);
myvideoview.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
myvideoview.start();
}
});
myvideoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.i("Exception", "url: " + url);
return false;
}
});
}
This class is an AyncTask to play the M3U8:
class playM3U8 extends AsyncTask<String, Void, Void> {
private Exception exception;
protected Void doInBackground(String... urls) {
urlStream = urls[0];
runOnUiThread(new Runnable() {
#Override
public void run() {
myvideoview.setVideoURI(Uri.parse(urlStream));
}
});
return null;
}
protected void onPostExecute(String feed) {
Log.i("*** Streaming M3U8", "url: " + urlStream );
}
}

Clicking back button causes force close / NullPointerException

I'm experiencing a NullPointerException and ForceClose issue when clicking the back button after launching either the gallery or the default audio player using the android application I've built.
I've seen a few examples that state I'll need to finish the activity or check for a back button press - but I'm not sure how exactly that would fit into this scenario.
It's crashing on line 84 which is: Uri selectedImage = data.getData();
Source:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
{
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
}
} else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage, "image/*");
startActivity(intent);
}
LogCat:
04-24 21:04:26.441: D/AndroidRuntime(4491): Shutting down VM
04-24 21:04:26.441: W/dalvikvm(4491): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:26.451: E/AndroidRuntime(4491): FATAL EXCEPTION: main
04-24 21:04:26.451: E/AndroidRuntime(4491): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.os.Looper.loop(Looper.java:137)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:26.451: E/AndroidRuntime(4491): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:26.451: E/AndroidRuntime(4491): at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491): Caused by: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:26.451: E/AndroidRuntime(4491): ... 11 more
04-24 21:04:28.721: I/Process(4491): Sending signal. PID: 4491 SIG: 9
04-24 21:04:39.681: D/AndroidRuntime(4543): Shutting down VM
04-24 21:04:39.681: W/dalvikvm(4543): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:39.691: E/AndroidRuntime(4543): FATAL EXCEPTION: main
04-24 21:04:39.691: E/AndroidRuntime(4543): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.os.Looper.loop(Looper.java:137)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:39.691: E/AndroidRuntime(4543): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:39.691: E/AndroidRuntime(4543): at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543): Caused by: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.tablet.hairmax.UI.onActivityResult(UI.java:76)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:39.691: E/AndroidRuntime(4543): ... 11 more
04-24 21:05:14.191: D/AndroidRuntime(4586): Shutting down VM
04-24 21:05:14.191: W/dalvikvm(4586): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:05:14.191: E/AndroidRuntime(4586): FATAL EXCEPTION: main
04-24 21:05:14.191: E/AndroidRuntime(4586): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.os.Looper.loop(Looper.java:137)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:05:14.191: E/AndroidRuntime(4586): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:05:14.191: E/AndroidRuntime(4586): at dalvik.system.NativeStart.main(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586): Caused by: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:05:14.191: E/AndroidRuntime(4586): ... 11 more
data is null. You need to check before that line if data is null. Since you are pressing the Back button, you aren't sending an Intent back with setResult() like would normally be done.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (data != null)
{
if (requestCode == SELECT_VIDEO) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
} else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage, "image/*");
startActivity(intent);
}
}
See this answer about getting an image from gallery

java.lang.IllegalArgumentException: No view found for id 0x7f0600cc for fragment

I have implemented the ViewPager in Fragment in my application which contains the Custom layout. Now in Custom layout of ViewPager there is a TextView and onClick of the TextView i would like to replace the current fragment with other fragment. Lets say with Fragment2.
I already tried to replace but its giving error for the Resource id of the FrameLayout where i want to load the fragment. I already checked the relevant solutions in SO but none helped me.
Please any one can guide me for this error. It would be really grateful and appreciable.
Logcat:
11-13 11:17:54.480: E/AndroidRuntime(3982): FATAL EXCEPTION: main
11-13 11:17:54.480: E/AndroidRuntime(3982): java.lang.IllegalArgumentException: No view found for id 0x7f0600cc for fragment OtherProfileFragment{40ceba28 #4 id=0x7f0600cc}
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:865)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.os.Handler.handleCallback(Handler.java:725)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.os.Looper.loop(Looper.java:137)
11-13 11:17:54.480: E/AndroidRuntime(3982): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-13 11:17:54.480: E/AndroidRuntime(3982): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 11:17:54.480: E/AndroidRuntime(3982): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 11:17:54.480: E/AndroidRuntime(3982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-13 11:17:54.480: E/AndroidRuntime(3982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-13 11:17:54.480: E/AndroidRuntime(3982): at dalvik.system.NativeStart.main(Native Method)
Here is my code where onclick of TextView i am trying to load the Fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_feed_detail, container,
false);
m_tvProfileName = (TextView) v.findViewById(R.id.afd_tvProfileName);
m_tvProfileName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager m_mange = m_context.getSupportFragmentManager();
FragmentTransaction fm = m_mange.beginTransaction();
Fragment llf = new OtherFragment();
Bundle m_bundle = new Bundle();
m_bundle.putInt("Id", Id);
llf.setArguments(m_bundle);
fm.addToBackStack("Other");
fm.replace(R.id.fragmentswitcherframe, llf); //This line throwing error
fm.commit();
getActivity().finish();
}
});
return v;
}

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" />

Categories