Media Player sample app crash - java

I am writing an app and When I try to load the class/layout by pressing a button on my main menu, I get the following logcat errors and I do not know what they mean, can anyone tell me why my app is crashing?
03-11 16:40:06.955: E/AndroidRuntime(18456): FATAL EXCEPTION: main
03-11 16:40:06.955: E/AndroidRuntime(18456): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.famouspeople/com.example.famouspeople.Music}: java.lang.InstantiationException: can't instantiate class com.example.famouspeople.Music; no empty constructor
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.os.Looper.loop(Looper.java:137)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 16:40:06.955: E/AndroidRuntime(18456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-11 16:40:06.955: E/AndroidRuntime(18456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-11 16:40:06.955: E/AndroidRuntime(18456): at dalvik.system.NativeStart.main(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): Caused by: java.lang.InstantiationException: can't instantiate class com.example.famouspeople.Music; no empty constructor
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.Class.newInstanceImpl(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.Class.newInstance(Class.java:1319)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
03-11 16:40:06.955: E/AndroidRuntime(18456): ... 11 more
03-11 16:40:16.560: I/Process(18456): Sending signal. PID: 18456 SIG: 9
Code:
public class Music implements OnCompletionListener{
MediaPlayer mediaPlayer;
boolean isPrepared = false;
public Music(AssetFileDescriptor assetDescriptor){
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
mediaPlayer.prepare();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
} catch(Exception ex){
throw new RuntimeException("Couldn't load music, uh oh!");
}
}
public Music(FileDescriptor fileDescriptor){
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(fileDescriptor);
mediaPlayer.prepare();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
} catch(Exception ex){
throw new RuntimeException("Couldn't load music, uh oh!");
}
}
public void onCompletion(MediaPlayer mediaPlayer) {
synchronized(this){
isPrepared = false;
}
}
public void play() {
if(mediaPlayer.isPlaying()){
return;
}
try{
synchronized(this){
if(!isPrepared){
mediaPlayer.prepare();
}
mediaPlayer.start();
}
} catch(IllegalStateException ex){
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}
public void stop() {
mediaPlayer.stop();
synchronized(this){
isPrepared = false;
}
}
public void switchTracks(){
mediaPlayer.seekTo(0);
mediaPlayer.pause();
}
public void pause() {
mediaPlayer.pause();
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public boolean isLooping() {
return mediaPlayer.isLooping();
}
public void setLooping(boolean isLooping) {
mediaPlayer.setLooping(isLooping);
}
public void setVolume(float volumeLeft, float volumeRight) {
mediaPlayer.setVolume(volumeLeft, volumeRight);
}
public void dispose() {
if(mediaPlayer.isPlaying()){
stop();
}
mediaPlayer.release();
}
}

You need to define the no argument constructor otherwise the systems doesn't know how to instantiate it. Dalvik VM looking for a zero-argument constructor. If you define one argument or more than one in a service.
public Music() {
....
}

Android instrumentation is trying to create an instance of com.example.famouspeople.Music and for that it needs a default constructor. That is why it has failed. Creating a default constructor, public com.example.famouspeople.Music(){} in the Music class could help.
public class Music implements OnCompletionListener{
public Music ()
{
}
public boolean setFileDescriptor(FileDescriptor fileDescriptor){
//set file descriptor for the current instance
}
public boolean init(){
//do initialisation of player
}
}
call init() after setting file descriptor. But the whole naming conventions seems confusing for your package/classes.

Related

why component == null in dagger 2?

It seems that it just created AppComponent and then delete it. In other my projects it's works correctly. Why appComponent become null and how to make it working ?
App.class
public class App extends Application {
private static AppComponent appComponent;
#Override
public void onCreate() {
super.onCreate();
buildGraphAndInject();
}
public static AppComponent getAppComponent() {
if(appComponent == null) {
Log.d("getter", "null");
}
return appComponent;
}
public void buildGraphAndInject() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent.inject(this);
if(appComponent == null) {
Log.d("build", "null");
}
}
}
call from Activity
#Override
public void setupComponent(AppComponent appComponent) {
mComponent = DaggerMainComponent.builder()
.appComponent(App.getAppComponent())
.build();
mComponent.inject(this);
}
AppComponent
#Component(modules = {AppModule.class})
public interface AppComponent {
void inject(App app);
}
result
04-02 02:16:23.544 32016-32016/exp.privatebank D/getter: null
04-02 02:16:23.569 32016-32016/exp.privatebank E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{exp.privatebank/exp.privatebank.view.activity.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at dagger.internal.Preconditions.checkNotNull(Preconditions.java:32)
at exp.privatebank.di.DaggerMainComponent$Builder.appComponent(DaggerMainComponent.java:110)
at exp.privatebank.view.activity.MainActivity.setupComponent(MainActivity.java:58)
at exp.privatebank.common.BaseActivity.onCreate(BaseActivity.java:19)
at exp.privatebank.view.activity.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
at android.app.ActivityThread.access$700(ActivityThread.java:159) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:176) 
at android.app.ActivityThread.main(ActivityThread.java:5419) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
at dalvik.system.NativeStart.main(Native Method) 

How to check if android.hardware.Camera is released?

I got an exception while releasing camera object "java.lang.RuntimeException: Method called after release"
following is my code and exception stack trace.
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
Exception stack trace -
java.lang.RuntimeException: Method called after release()
Thread[main,5,main] android.hardware.Camera._stopPreview(Native Method)
android.hardware.Camera.stopPreview(Camera.java:626)
com.s5.selfiemonkey1.activity.Preview.surfaceDestroyed(Preview.java:152)
android.view.SurfaceView.updateWindow(SurfaceView.java:601)
android.view.SurfaceView.access$000(SurfaceView.java:88)
android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183)
android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
android.view.Choreographer.doCallbacks(Choreographer.java:579)
android.view.Choreographer.doFrame(Choreographer.java:548)
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
android.os.Handler.handleCallback(Handler.java:800)
android.os.Handler.dispatchMessage(Handler.java:100)
android.os.Looper.loop(Looper.java:194)
android.app.ActivityThread.main(ActivityThread.java:5371)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:525)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
it look like stopPreview() is called on released object.
I had made a method which would return me boolean depending of whether camera is in use or not. Below is a small snipet:
public static Camera checkForCamera(){
Camera camera = null;
try {
camera = Camera.open(); // this line will throw exception if camera is not in use.
}
catch (Exception e){
// if exception is thrown, return your boolean value here...
}
return camera; // if instance of camera, if it is not available it will return null.
}

Check if Internet Connection Is Active In Android

i am trying to check for internet connection in my application.
first, i am checking if wifi or mobile data is on, then i am checking if there is an active internet connection.
what i am currently doing:
public class ConnectivityReceiver extends BroadcastReceiver {
String TAG="ConnectivityReceiver";
public static boolean hasInternetAccess() {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
}
return false;
}
#Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);
if(noConnectivity){
try{
TaskListAndOptionsActivity.item3.setIcon(R.drawable.ic_action_network_nowifi);
}
catch(Exception e)
{
}
}
else
{
Boolean status = hasInternetAccess();
if(status==true)
{
try{
TaskListAndOptionsActivity.item3.setIcon(R.drawable.ic_action_network_wifi);
}
catch(Exception e)
{
}
}
else
{
try{
TaskListAndOptionsActivity.item3.setIcon(R.drawable.ic_action_network_nowifi);
}
catch(Exception e)
{
}
}
}
}
but i am getting the following error:
java.lang.RuntimeException: Unable to start receiver connectionchecker.ConnectivityReceiver: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2668)
at android.app.ActivityThread.access$1800(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:390)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:343)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at connectionchecker.NetworkUtil.hasInternetAccess(NetworkUtil.java:21)
at connectionchecker.ConnectivityReceiver.onReceive(ConnectivityReceiver.java:46)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2653)
            at android.app.ActivityThread.access$1800(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            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:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
You are trying to connect to the network in UI thread which is not allowed. to check for active internet connection use following method.
public boolean isConnected() {
ConnectivityManager manager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
return true;
} else {
return false;
}
}
Once you receive the value, if it is true proceed with network operation. Else display the user a proper error message.
Caused by: android.os.NetworkOnMainThreadException
You need to perform network operation not on UIthread.
Sounds like you might need to use the getActiveNetworkInfo() call. There is a few posts on stackoverflow already that document it extensively so rather than repeat those have a look at these:
Detect whether there is an Internet connection available on Android

code with thread crashes at startup (android beginner)

I am trying to make an application that passes through the audio samples obtained at the microphone to the speaker. This is the source code:
public class MainActivity extends Activity {
AudioManager am = null;
AudioRecord record =null;
AudioTrack track =null;
final int SAMPLE_FREQUENCY = 44100;
final int SIZE_OF_RECORD_ARRAY = 1024;
boolean isPlaying = false;
class MyThread extends Thread{
#Override
public void run(){
recordAndPlay();
}
}
MyThread newThread;
private void init() {
int min = AudioRecord.getMinBufferSize(SAMPLE_FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_FREQUENCY, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, min);
int maxJitter = AudioTrack.getMinBufferSize(SAMPLE_FREQUENCY, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, SAMPLE_FREQUENCY, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
init();
newThread.start();
}
#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;
}
private void recordAndPlay() {
short[] lin = new short[SIZE_OF_RECORD_ARRAY];
int num = 0;
am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
record.startRecording();
track.play();
while (true) {
num = record.read(lin, 0, SIZE_OF_RECORD_ARRAY);
track.write(lin, 0, num);
}
}
public void passStop(View view){
Button playBtn = (Button) findViewById(R.id.playBtn);
// /*
if(!isPlaying){
record.startRecording();
track.play();
isPlaying = true;
playBtn.setText("Pause");
}
if(isPlaying){
record.stop();
track.pause();
isPlaying=false;
playBtn.setText("Pass through");
}
// */
}
#SuppressWarnings("deprecation")
#Override
public void onDestroy(){
newThread.stop();
}
}
Unfortunately, this program stops as soon as I try to run it through eclipse. This is wht I get in the logcat but I am not sure what it all means:
08-19 18:58:43.365: D/AndroidRuntime(27915): Shutting down VM
08-19 18:58:43.365: W/dalvikvm(27915): threadid=1: thread exiting with uncaught exception (group=0x4161f700)
08-19 18:58:43.365: E/AndroidRuntime(27915): FATAL EXCEPTION: main
08-19 18:58:43.365: E/AndroidRuntime(27915): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypassthrough/com.example.mypassthrough.MainActivity}: java.lang.NullPointerException
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.os.Looper.loop(Looper.java:137)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-19 18:58:43.365: E/AndroidRuntime(27915): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 18:58:43.365: E/AndroidRuntime(27915): at java.lang.reflect.Method.invoke(Method.java:525)
08-19 18:58:43.365: E/AndroidRuntime(27915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-19 18:58:43.365: E/AndroidRuntime(27915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-19 18:58:43.365: E/AndroidRuntime(27915): at dalvik.system.NativeStart.main(Native Method)
08-19 18:58:43.365: E/AndroidRuntime(27915): Caused by: java.lang.NullPointerException
08-19 18:58:43.365: E/AndroidRuntime(27915): at com.example.mypassthrough.MainActivity.onCreate(MainActivity.java:46)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.Activity.performCreate(Activity.java:5133)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-19 18:58:43.365: E/AndroidRuntime(27915): ... 11 more
What could be the reason for this code crashing, and how can it be debugged? I am pretty sure it has something to do with Thread, because my other versions of this code where I have not included Thread at all do not crash on startup.
Variable newThread is not initialized in oncreate() method that's why it is giving nullpointer exception
add this in your init() method it will work
newThread=new Thread();
You are getting a null pointer exception because newThread has not been initialized yet and it is null when you try to start it. The thread doesn't have anything to do also. You should look at implementing a runnable, handler, and timer into your activity instead of a thread.
Use sub class with extending AsyncTask Class
class TestActivity extends AsyncTask <String, Void, String > {
protected String doInBackground(String... urls) {
// execution
}
protected void onPostExecute(String result) {
// post execution
}
protected void onPreExecute() {
super.onPreExecute();
// initial activities
}
}

Worker thread in Android

Hello im trying to simulate a worker thread in Android for testing.
The activity has a button that calls notify() over the worker thread, and I want it go to sleep after 10 iterations of the bucle.
Thats my code and the stacktrace of the error. How can I solve it? Thanks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t= new Thread(){
public void run(){
int i=0;
while(true){
if (i%10==9){
//Simulated end of data until notified again
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d("Test",""+i);
SystemClock.sleep(1000);
}
}
};
t.start();
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.notify();
}
});
}
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): FATAL EXCEPTION: main
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): java.lang.IllegalMonitorStateException: object not locked by thread before notify()
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at java.lang.Object.notify(Native Method)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at com.example.memorycrash.MemoryCrashTestActivity$2.onClick(MemoryCrashTestActivity.java:51)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at android.view.View.performClick(View.java:2538)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at android.view.View$PerformClick.run(View.java:9152)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at android.os.Handler.handleCallback(Handler.java:587)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at android.os.Looper.loop(Looper.java:130)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at java.lang.reflect.Method.invoke(Method.java:507)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-06 05:39:45.320: ERROR/AndroidRuntime(4480): at dalvik.system.NativeStart.main(Native Method)
So here is your solution.
Answer was already there on StackOverflow.
In order to call wait(), notify(), or notifyAll() on an object you must first own the monitor of the object you wish to call the method on, so in you case within the runnable this would be how you would need to do it:
Runnable runnable = new Runnable() {
public void run() {
// wait(); This call wouldn't work
syncronized (this) {
wait(); // This call will work
}
}
};
To notify that runnable you would also have to have the monitor
// runnable.notifyAll(); this call will not work
syncronized (runnable) {
runnable.notifyAll(); // this call will work
}

Categories