android setcontentview错误

时间:2013-02-18 06:52:57

标签: android android-layout memory background

我有三个布局。各背景图片 但是,我遇到了一个问题。

当我使用setcontentview切换到第三个布局时。

会导致。

java.lang.RuntimeException: Unable to start activity

我不得不认为可能是因为背景图片的问题。

我将两个布局文件的背景更改为相同。

确实能够解决这个问题。

但当我在ImageView底部时。

并放置在原始的第三个布局背景之上 将导致out of memory error

最后怎么做 要实现这一点,我需要三个布局背景图像吗?

我想到的压力只有10k仍然有这个错误。

内存消耗是基于图像大小不是基于绘图的容量?

第一项活动:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.op);
        Runnable runnable=new Runnable(){
            public void run(){
                Intent intent = new Intent(MainActivity.this, dm.newbie.myhoust.indexx.class);
                MainActivity.this.startActivity(intent);
              }

            };
            Handler handler=new Handler();
            handler.postDelayed(runnable,3000);
    }


}

indexx.java

public class indexx extends Activity {
int num;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    Button boy=(Button)this.findViewById(R.id.button1);
    boy.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            num=2;
dr();
        }

        private void dr() {
            Bundle bData = new Bundle();
             bData.putDouble( "pag",num);
             bData.putDouble( "secc", Integer.parseInt(
                   ((EditText)findViewById( R.id.editText1 ))
                   .getText()
                   .toString()
                   )
         );

            Intent intent2 = new Intent(indexx.this, dm.myhoust.run.wait.class);
            intent2.putExtras(bData);
            indexx.this.startActivity(intent2);

        }
    });
}

}

wait.java

public class wait extends Activity {
     Double num2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.infd);
        Bundle bData = this.getIntent().getExtras();
         Double num1 = bData.getDouble( "secc" );
         num2 = bData.getDouble( "pag" );

        Runnable runnable=new Runnable(){
            public void run(){
                Intent intent = new Intent(wait.this, dm.myhoust.run.ringring.class);
                Bundle bData2 = new Bundle();
                bData2.putDouble( "pag",num2);
                intent.putExtras(bData2);
                wait.this.startActivity(intent);
              } 
            };
            Handler handler=new Handler();
            handler.postDelayed(runnable,(long) (num1*1000));
    }

}

ringring.java

public class ringring extends Activity {
     Timer timer = new Timer(true);
     MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ring);       
         ImageView callman = (ImageView)this.findViewById(R.id.imageView1);


  //If the background is not replaced before use this instead   
         //ImageView callman2 = (ImageView)this.findViewById(R.id.imageView2);
//callman.setImageResource(R.drawable.bg);


         callman2.setImageResource(R.drawable.sf);
        Bundle bData2 = this.getIntent().getExtras();
         Double num1 = bData2.getDouble( "pag" );
         if(num1>1){
             callman.setImageResource(R.drawable.s1);
         }
         Timer timer = new Timer();  
         timer.schedule(new timerTask(), 5000, 5000);
         mp = MediaPlayer.create(ringring.this, R.raw.skype);

    }
      public class timerTask extends TimerTask
      {
        public void run()
        {

        mp.start();    
        Vibrator myVibrator = (Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
        myVibrator.vibrate(3000);
        }
      };

}

由于背景不同,导致此错误:

02-18 14:57:56.452: E/AndroidRuntime(26662): FATAL EXCEPTION: main
02-18 14:57:56.452: E/AndroidRuntime(26662): java.lang.RuntimeException: Unable to start activity ComponentInfo{dm.newbie.myhoust/dm.myhoust.run.ringring}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.os.Looper.loop(Looper.java:137)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.ActivityThread.main(ActivityThread.java:4511)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at java.lang.reflect.Method.invokeNative(Native Method)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at java.lang.reflect.Method.invoke(Method.java:511)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at dalvik.system.NativeStart.main(Native Method)
02-18 14:57:56.452: E/AndroidRuntime(26662): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:272)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.Activity.setContentView(Activity.java:1835)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at dm.myhoust.run.ringring.onCreate(ringring.java:30)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.Activity.performCreate(Activity.java:4470)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
02-18 14:57:56.452: E/AndroidRuntime(26662):    ... 11 more
02-18 14:57:56.452: E/AndroidRuntime(26662): Caused by: java.lang.reflect.InvocationTargetException
02-18 14:57:56.452: E/AndroidRuntime(26662):    at java.lang.reflect.Constructor.constructNative(Native Method)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
02-18 14:57:56.452: E/AndroidRuntime(26662):    ... 23 more
02-18 14:57:56.452: E/AndroidRuntime(26662): Caused by: java.lang.OutOfMemoryError
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.Bitmap.nativeCreate(Native Method)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.content.res.Resources.loadDrawable(Resources.java:1935)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.View.<init>(View.java:2841)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.View.<init>(View.java:2778)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.view.ViewGroup.<init>(ViewGroup.java:380)
02-18 14:57:56.452: E/AndroidRuntime(26662):    at android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
02-18 14:57:56.452: E/AndroidRuntime(26662):    ... 26 more

这是使用imageview方式替代背景错误:

02-18 15:02:48.177: E/AndroidRuntime(1537): FATAL EXCEPTION: main
02-18 15:02:48.177: E/AndroidRuntime(1537): java.lang.OutOfMemoryError
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.Bitmap.nativeCreate(Native Method)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.content.res.Resources.loadDrawable(Resources.java:1935)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.content.res.Resources.getDrawable(Resources.java:664)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.widget.ImageView.resolveUri(ImageView.java:542)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.widget.ImageView.setImageResource(ImageView.java:315)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at dm.myhoust.run.ringring.onCreate(ringring.java:33)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.app.Activity.performCreate(Activity.java:4470)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.os.Looper.loop(Looper.java:137)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at android.app.ActivityThread.main(ActivityThread.java:4511)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-18 15:02:48.177: E/AndroidRuntime(1537):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案