Zwiebel
Zwiebel

Reputation: 1615

Is there any problem with this code?

It produce the stopped unexpectedly problem when I push the search button.

public class beautiful extends Activity {
    ImageView radar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
      this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.beautiful);

    radar = (ImageView) findViewById(R.id.radar);
    Button search = (Button) findViewById(R.id.magnifier);
    ImageView text = (ImageView) findViewById(R.id.text);
    MediaPlayer siren = MediaPlayer.create(this, R.raw.siren);

    search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Thread counter = new Thread(new Runnable(){

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try{
                        Thread.sleep(2000);
                        radar.setImageResource(R.drawable.radar_new_full);
                        Thread.sleep(3000);
                        radar.setImageResource(R.drawable.radar_new_50);
                        Thread.sleep(2000);
                        radar.setImageResource(R.drawable.radar_new_found);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    finally{

                    }
                }

            });
            counter.start();
        }
    });
}

Here are the LogCat warnings:

07-07 17:36:17.298: WARN/dalvikvm(274): threadid=15: thread exiting with uncaught exception (group=0x4001b188) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): Uncaught handler: thread Thread-8 exiting due to uncaught exception 07-07 17:36:17.306: ERROR/AndroidRuntime(274): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at android.view.ViewRoot.checkThread(ViewRoot.java:2683) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at android.view.ViewRoot.requestLayout(ViewRoot.java:557) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at android.view.View.requestLayout(View.java:7918) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at android.widget.ImageView.setImageResource(ImageView.java:271) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at example.beautiful$1$1.run(beautiful.java:46) 07-07 17:36:17.306: ERROR/AndroidRuntime(274): at java.lang.Thread.run(Thread.java:1096)

Upvotes: 1

Views: 241

Answers (4)

Mr.James
Mr.James

Reputation: 386

You can try this:

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//do sth...
}

Upvotes: 1

Eric Cartman
Eric Cartman

Reputation: 114

Put some print statements here and there or breakpoints, whichever you prefer. Trace down which line is the one which causes the error, try putting try-catch statements around it to have a clear idea of what the error is - then it would be easier to solve the error (Shortly debug)

  • just an advice

Upvotes: 1

Gopal
Gopal

Reputation: 1719

Read about ANR Dialog link and how to update UI from other thread Painless Threading

Upvotes: 1

k1c0
k1c0

Reputation: 43

Try to remove @Override above the public void run().

Upvotes: 1

Related Questions