为什么这段代码产生“抱歉!应用程序意外停止。请再试一次。”问题?

时间:2011-07-07 14:25:43

标签: android

这是代码: public class beautiful extends Activity {     ImageView radar =(ImageView)findViewById(R.id.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);

    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();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()){
    case R.id.new_search:
        startActivity(new Intent("com.zwiebel.MENU"));

    }
    return super.onMenuItemSelected(featureId, item);
}




}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#fccea8">

  <Button
        android:id="@+id/magnifier"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_gravity="right"
        android:background="@drawable/searchmagnificer"
  />

  <ImageView
        android:id="@+id/radar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="5dp"
         />

  <ImageView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
</LinearLayout>

我在代码中没有看到任何问题,所以我不知道错误是什么。

1 个答案:

答案 0 :(得分:6)

很明显。您正在尝试在设置视图之前查找视图。

ImageView radar = (ImageView) findViewById(R.id.radar);

将其更改为

ImageView radar;

并放

radar = (ImageView) findViewById(R.id.radar);

在setContentView函数

下面