设置新内容视图时应用程序崩溃

时间:2014-01-09 10:31:21

标签: java android

编辑:

似乎我的按钮样式导致了问题。

编辑代码

buttonL.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttonL.setBackgroundResource(R.drawable.lights_over);
            if(event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)
                buttonL.setBackgroundResource(R.drawable.lights);
            return false;
        }
    });

上面的代码适用于4.2及以上的原因。

将它放在未来的人们身上。


当我尝试将setContentView()方法调用到布局时,我的应用程序崩溃了。

我尝试了一些解决方案,当我删除按钮的样式时,错误就消失了。

似乎我用于按钮的样式导致了这种情况。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/lights_over" />
<item android:state_focused="true" android:drawable="@drawable/lights_over" />
<item android:state_selected = "true" android:drawable = "@drawable/lights_over" />
<item android:drawable = "@drawable/lights" android:id="@+id/button_L"/>
</selector>

我的LogCat

01-09 12:04:24.161: D/dalvikvm(20682): GC_BEFORE_OOM freed 9K, 5% free 122880K/129056K, paused 20ms, total 20ms
01-09 12:04:24.161: E/dalvikvm-heap(20682): Out of memory on a 9027940-byte allocation.
01-09 12:04:24.171: E/AndroidRuntime(20682): FATAL EXCEPTION: main
01-09 12:04:24.171: E/AndroidRuntime(20682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.broadcastreceiver/com.androidexample.broadcastreceiver.StartMenu}: android.view.InflateException: Binary XML file line #79: Error inflating class <unknown>
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.os.Looper.loop(Looper.java:137)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.ActivityThread.main(ActivityThread.java:5293)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at java.lang.reflect.Method.invoke(Method.java:511)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at dalvik.system.NativeStart.main(Native Method)
01-09 12:04:24.171: E/AndroidRuntime(20682): Caused by: android.view.InflateException: Binary XML file line #79: Error inflating class <unknown>
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.Activity.setContentView(Activity.java:1928)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at com.androidexample.broadcastreceiver.StartMenu.onCreate(StartMenu.java:20)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.Activity.performCreate(Activity.java:5250)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
01-09 12:04:24.171: E/AndroidRuntime(20682):    ... 11 more
01-09 12:04:24.171: E/AndroidRuntime(20682): Caused by: java.lang.reflect.InvocationTargetException
01-09 12:04:24.171: E/AndroidRuntime(20682):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.LayoutInflater.createView(LayoutInflater.java:593)
01-09 12:04:24.171: E/AndroidRuntime(20682):    ... 25 more
01-09 12:04:24.171: E/AndroidRuntime(20682): Caused by: java.lang.OutOfMemoryError
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.content.res.Resources.loadDrawable(Resources.java:2854)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.content.res.Resources.getDrawable(Resources.java:1521)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:921)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:858)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.content.res.Resources.loadDrawable(Resources.java:2839)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.view.View.<init>(View.java:3460)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.widget.TextView.<init>(TextView.java:791)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.widget.Button.<init>(Button.java:107)
01-09 12:04:24.171: E/AndroidRuntime(20682):    at android.widget.Button.<init>(Button.java:103)
01-09 12:04:24.171: E/AndroidRuntime(20682):    ... 28 more

3 个答案:

答案 0 :(得分:2)

从logcat开始,问题就是Button的背景。我认为这是一个很大的形象,所以它会溢出你的记忆。

<强>第一
尝试删除menu_system.xml中的任何@drawable,特别是按钮
中的android:background 运行你的工作,然后知道在哪里看。

第二(解决方法)
将背景图像文件移动到更大的文件夹
从drawable到drawable-xhdpi

答案 1 :(得分:0)

试试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    Thread splash=new Thread(){
        public void run(){
            try{
                sleep(2000);
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                startActivity(new Intent(getApplicationContext(),StartMenu.class));
                finish();
            }
        }
    };

    splash.start();
}

你的Manifest.xml:

 <activity
 android:name="com.example.mapcard.Splash"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

答案 2 :(得分:0)

我尝试了你的代码。它运行正常。我认为问题是因为内存不足。问题是图像资源,尝试以压缩格式设置图像以避免内存泄漏。