2011-12-17 99 views
0

編輯:如果硬骨素要當onConfigurationChanged叫我只是intialize我的onCreate所做的所有變量的答案ImageView的背景不顯示圖像

你好,我有一個應用程序中顯示一個進度對話框,然後在從相機拍攝的圖像視圖中顯示圖像。問題是當我在橫向拍攝照片時,然後等待對話框在任何圖像視圖中都沒有顯示出來,並且沒有任何按鈕可以工作。當對話即將顯示時它曾經崩潰,但我修復了這個問題。下面是我的一些代碼:

public class AnnoyingMeterActivity extends Activity { 

    Random r = new Random(System.currentTimeMillis()); 


    class ProgressThread extends Thread { 
     Handler mHandler; 
     final static int STATE_DONE = 0; 
     final static int STATE_RUNNING = 1; 
     int mState; 
     int total; 

     ProgressThread(Handler h) { 
      mHandler = h; 
     } 

     public void run() { 
      mState = STATE_RUNNING; 
      total = 0; 
      while (mState == STATE_RUNNING) { 
       try { 
        Thread.sleep(55); 
       } catch (InterruptedException e) { 

       } 
       Message msg = mHandler.obtainMessage(); 
       msg.arg1 = total; 
       mHandler.sendMessage(msg); 
       total++; 
      } 
     } 

     /* 
     * sets the current state for the thread, used to stop the thread 
     */ 
     public void setState(int state) { 
      mState = state; 
     } 
    } 

    TextView tv; 
    ImageView iv; 
    TextView tv1; 
    static final int PROGRESS_DIALOG = 0; 
    ProgressThread progressThread; 
    ProgressDialog progressDialog; 
    Bitmap person; 
    ImageView tiv; 
    ImageView iv2; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button scanPersonButton = (Button) findViewById(R.id.scanPerson); 
     scanPersonButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 
       scanPerson(); 

      } 
     }); 
     tv = (TextView) findViewById(R.id.tv); 
     iv = (ImageView) findViewById(R.id.annoyingMeterImage); 
     iv.setBackgroundResource(R.drawable.finalmeter); 
     tv1 = (TextView) findViewById(R.id.textView1); 
     tiv = (ImageView) findViewById(R.id.imageView1); 


    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     setContentView(R.layout.main); 
    } 


    protected void scanPerson() { 

     Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
     startActivityForResult(intent, 0); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      person = (Bitmap) data.getExtras().get("data"); 
      showDialog(PROGRESS_DIALOG); 
     } 

    } 

    protected void randomImageDisplayThing() { 
       tiv.setBackgroundDrawable(new BitmapDrawable(person)); 
     switch (annoyingRating) { 
     case 0: 
      iv.setBackgroundResource(R.drawable.finalmeter0); 
      break; 
     case 1: 
      iv.setBackgroundResource(R.drawable.finalmeter1); 
      break; 
     case 2: 
      iv.setBackgroundResource(R.drawable.finalmeter2); 
      break; 
     case 3: 
      iv.setBackgroundResource(R.drawable.finalmeter3); 
      break; 
     case 4: 
      iv.setBackgroundResource(R.drawable.finalmeter4); 
      break; 
     case 5: 
      iv.setBackgroundResource(R.drawable.finalmeter5); 
      break; 
     case 6: 
      iv.setBackgroundResource(R.drawable.finalmeter6); 
      break; 
     case 7: 
      iv.setBackgroundResource(R.drawable.finalmeter7); 
      break; 
     case 8: 
      iv.setBackgroundResource(R.drawable.finalmeter8); 
      break; 
     case 9: 
      iv.setBackgroundResource(R.drawable.finalmeter9); 
      break; 
     case 10: 
      iv.setBackgroundResource(R.drawable.finalmeter10); 
      break; 
     case 11: 
      iv.setBackgroundResource(R.drawable.finalmeter11); 
      break; 
     } 

    } 

    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case PROGRESS_DIALOG: 
      progressDialog = new ProgressDialog(this); 
      progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      progressDialog.setMessage("Scanning..."); 
      return progressDialog; 
     default: 
      return null; 
     } 
    } 

    final Handler handler = new Handler() { 
     public void handleMessage(Message msg) { 
      int total = msg.arg1; 
      progressDialog.setProgress(total); 
      if (total >= 100) { 
       dismissDialog(PROGRESS_DIALOG); 
       progressThread.setState(ProgressThread.STATE_DONE); 
       randomImageDisplayThing(); 
      } 
     } 
    }; 

    @Override 
    protected void onPrepareDialog(int id, Dialog dialog) { 
     switch (id) { 
     case PROGRESS_DIALOG: 
      progressDialog.setProgress(0); 
      progressThread = new ProgressThread(handler); 
      progressThread.start(); 
     } 



    } 
} 

現在我真的不知道該怎麼辦。我認爲這個問題可能與方向改變有關。任何幫助表示讚賞!

回答