启动新活动时出错(android)

时间:2016-03-21 06:49:25

标签: java android xml

昨天我做了简单的应用程序;因此,当我按下按钮时,新活动(Activity2Main)应该开始。 昨天它工作,我很高兴。一切都很棒。 今天,我试图运行应用程序,我看到了什么?当我按下按钮时,我看到崩溃和关闭应用程序消息。 它能是什么? 这是控制台的日志:

E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dns.politicalscience, PID: 2076
java.lang.RuntimeException: Unable to start activity ComponentInfo {
        com.example.dns.politicalscience / com.example.dns.politicalscience.Main2Activity
    }: android.view.InflateException: Binary XML file line #2: Binary XML file line # 2: Error inflating class < unknown >
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2476)
at android.app.ActivityThread. - wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1344)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 148)
at android.app.ActivityThread.main(ActivityThread.java: 5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 616)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line # 2: Error inflating class < unknown >
    at android.view.LayoutInflater.inflate(LayoutInflater.java: 539)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 423)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java: 267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java: 129)
at com.example.dns.politicalscience.Main2Activity.onCreate(Main2Activity.java: 11)
at android.app.Activity.performCreate(Activity.java: 6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2476)
at android.app.ActivityThread. - wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1344)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 148)
at android.app.ActivityThread.main(ActivityThread.java: 5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 616)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java: 645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java: 58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java: 694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 704)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 492)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 423)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java: 267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java: 129)
at com.example.dns.politicalscience.Main2Activity.onCreate(Main2Activity.java: 11)
at android.app.Activity.performCreate(Activity.java: 6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2476)
at android.app.ActivityThread. - wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1344)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 148)
at android.app.ActivityThread.main(ActivityThread.java: 5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java: 619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java: 58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java: 694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 704)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 492)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 423)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java: 267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java: 129)
at com.example.dns.politicalscience.Main2Activity.onCreate(Main2Activity.java: 11)
at android.app.Activity.performCreate(Activity.java: 6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2476)
at android.app.ActivityThread. - wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1344)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 148)
at android.app.ActivityThread.main(ActivityThread.java: 5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 616)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 44728332 byte allocation with 4194304 free bytes and 9 MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java: 609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java: 444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java: 1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java: 2635)
at android.content.res.Resources.loadDrawable(Resources.java: 2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java: 870)
at android.view.View. < init > (View.java: 3948)
at android.view.ViewGroup. < init > (ViewGroup.java: 573)
at android.widget.RelativeLayout. < init > (RelativeLayout.java: 248)
at android.widget.RelativeLayout. < init > (RelativeLayout.java: 244)
at android.widget.RelativeLayout. < init > (RelativeLayout.java: 240)
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java: 619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java: 58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java: 694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java: 704)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 492)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 423)
at android.view.LayoutInflater.inflate(LayoutInflater.java: 374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java: 267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java: 129)
at com.example.dns.politicalscience.Main2Activity.onCreate(Main2Activity.java: 11)
at android.app.Activity.performCreate(Activity.java: 6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2476)
at android.app.ActivityThread. - wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1344)
at android.os.Handler.dispatchMessage(Handler.java: 102)
at android.os.Looper.loop(Looper.java: 148)
at android.app.ActivityThread.main(ActivityThread.java: 5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 616)

我的Manifest.xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Main2Activity"></activity>
</application>

这是我的activity1.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/canbe_backgroud"
    tools:context="com.example.dns.politicalscience.MainActivity"
    android:onClick="onClickBackground">

    <ImageButton
        android:layout_width="55mm"
        android:layout_height="20mm"
        android:id="@+id/imageButton"
        android:background="@drawable/start_button"
        android:cropToPadding="false"
        android:clickable="true"
        android:onClick="onClickObama"
        android:nestedScrollingEnabled="false" />

    <ImageButton
        android:layout_width="55mm"
        android:layout_height="20mm"
        android:id="@+id/imageButton2"
        android:layout_below="@+id/imageButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/idea_button"

        android:clickable="true" />

    <ImageButton
        android:layout_width="55mm"
        android:layout_height="20mm"
        android:id="@+id/imageButton3"
        android:layout_below="@+id/imageButton2"
        android:background="@drawable/about_botton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:clickable="true"
        android:onClick="onClickAbout" />
</RelativeLayout>

Here's second activityxml:

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="@drawable/canbe_backgroud"
        tools:context="com.example.dns.politicalscience.Main2Activity">

    </RelativeLayout>

我发现代码中没有错误,即使在java中也是如此 这是第一个活动中的java-code onClick:

 public void onClickAbout(View view) {
    Intent intent = new Intent(this, Main2Activity.class);
    startActivity(intent);
}

这是第二个:

public class Main2Activity extends AppCompatActivity {

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

}

发生什么事,伙计们?昨天它在模拟器上工作得很好,但今天它显示了崩溃信息。

3 个答案:

答案 0 :(得分:0)

正如评论所指出的,它是一个OutOfMemoryError。

尝试在yout manifest文件中使用largeHeap。

<Application
..............
android:largeHeap="true"
............../>

无论如何,它是由你试图加载的一些大位图引起的,检查你的资源。

答案 1 :(得分:0)

增加分配给模拟器的内存大小......

这是一种从Andriod studio 1.2.0中找到操作模拟器堆的选项的方法

  1. 菜单工具 - &gt; Android-&gt; AVD Manager

  2. 编辑选择的虚拟设备

  3. 点击高级设置并向下滚动

  4. enter image description here

答案 2 :(得分:0)

正如其他人提到的那样,并且正如您的logcat所示,您的内存不足。您在图像按钮中使用的绘图太大(或者一起分配大量内存)。

首先,尝试使用PC上的图像编辑器压缩图像。然后用新的压缩图像覆盖旧图像。

然后,确保如果您在虚拟机上运行它,请按照@Randhawa所说的步骤进行操作。

完成这些操作后,请执行以下操作:

按照此处的说明高效加载位图(在#34;将缩小后的版本加载到内存和#34中):http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

然后将位图转换为drawable,如下所示:

Drawable d = new BitmapDrawable(getResources(), bitmap);

在setContent(活动的xml)之后,在onCreate中以编程方式设置图像按钮背景:

ImageButton imgButton = (ImageButton) findViewById(R.id.imageButton); 
int sdk = android.os.Build.VERSION.SDK_INT;
 if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN){ 
    imgButton.setBackgroundDrawable(d); 
// where d is your Drawable 
} 
else{
 imgButton.setBackground(d);
 }

对图像按钮2和3执行相同的操作。