2015-05-02 76 views
1

制作了百分比计算器,但它不会显示在模拟器中,并且在启动前它只是说“不幸的是,停止“ 有人可以告诉我这里做错了什么吗?Android Studio:应用程序无法在模拟器中启动“不幸的是,应用程序已停止”

Java代码:

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity { 

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

} 
public void ButtonClick(View v) 
{ 

    EditText a = (EditText) findViewById(R.id.amount); 
    EditText p = (EditText) findViewById(R.id.percent); 

    TextView r = (TextView) findViewById(R.id.result); 

    double num1, num2, ans; 
    num1 = Double.parseDouble(a.getText().toString()); 
    num2 = Double.parseDouble(p.getText().toString()); 
    ans = 0; 
    if (v.getId() == R.id.bt1) 
     ans = num1 * num2/100; 
    if (v.getId() == R.id.bt2) 
     ans = (num1 * num2/100) + num1; 
    if (v.getId() == R.id.bt3) 
     ans = (num1 * num2/100) - num1; 

    r.setText(ans + ""); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

XML:

<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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" 
android:background="@drawable/perccal2"> 

<ImageButton 
    android:layout_width="110dp" 
    android:layout_height="70dp" 
    android:id="@+id/bt1" 
    android:layout_marginTop="260dp" 
    android:background="@drawable/bt1" 
    android:onClick="ButtonClick" /> 

<ImageButton 
    android:layout_width="110dp" 
    android:layout_height="70dp" 
    android:id="@+id/bt2" 
    android:layout_marginTop="260dp" 
    android:layout_marginLeft="120dp" 
    android:background="@drawable/bt2" 
    android:onClick="ButtonClick" /> 
<ImageButton 
    android:layout_width="110dp" 
    android:layout_height="70dp" 
    android:id="@+id/bt3" 
    android:layout_marginTop="260dp" 
    android:layout_marginLeft="240dp" 
    android:background="@drawable/bt3" 
    android:onClick="ButtonClick" /> 






<EditText 
    android:layout_width="110dp" 
    android:layout_height="50dp" 
    android:id="@+id/amount" 
    android:layout_marginTop="50dp" 
    android:layout_marginLeft="210dp" /> 
<EditText 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:id="@+id/percent" 
    android:layout_marginTop="170dp" 
    android:layout_marginLeft="247dp" 
    /> 

<TextView 
    android:layout_width="165dp" 
    android:layout_height="70dp" 
    android:id="@+id/result" 
    android:layout_marginTop="420dp" 
    android:layout_marginLeft="95dp" 
    /> 

的logcat:

05-02 13:50:26.690 2300-2300/? E/art﹕ Throwing OutOfMemoryError 
     "Failed to allocate a 138883260 byte allocation with 1048576 
     free bytes and 63MB until OOM" 
     05-02 13:50:26.691 2300-2300/? D/skia﹕ --- 
    allocation failed for scaled  bitmap 
     05-02 13:50:26.694 2300-2300/? D/AndroidRuntime﹕ Shutting down VM 
     --------- beginning of crash 
     05-02 13:50:26.717 2300-2300/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
     Process: com.bluewhale.percentagecalculator, PID: 2300 
     java.lang.RuntimeException: Unable to start activity 
     ComponentInfo{com.bluewhale.percentagecalculator/ 
     com.bluewhale.percentagecalculator.MainActivity}: 
    android.view.InflateException: Binary XML file line #1: 
    Error inflating class android.widget.RelativeLayout 
      at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
      at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at 
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at 
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
    (ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
     Caused by: android.view.InflateException: Binary XML file line #1: Error 
    inflating class android.widget.RelativeLayout 
      at android.view.LayoutInflater.createView(LayoutInflater.java:633) 
      at 
     com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView 
    (PhoneLayoutInflater.java:55) 
      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
      at 
     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
      at android.support.v7.app.AppCompatDelegateImplV7.setContentView 
      (AppCompatDelegateImplV7.java:249) 
      at android.support.v7.app.AppCompatActivity.setContentView 
      (AppCompatActivity.java:106) 
      at com.bluewhale.percentagecalculator.MainActivity.onCreate 
      (MainActivity.java:17) 
      at android.app.Activity.performCreate(Activity.java:5937) 
      at 
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
      at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
             
     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
             at android.app.ActivityThread.access$800(ActivityThread.java:144) 
              at 
     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
             at android.os.Handler.dispatchMessage(Handler.java:102) 
             at android.os.Looper.loop(Looper.java:135) 
             at android.app.ActivityThread.main(ActivityThread.java:5221) 
             at java.lang.reflect.Method.invoke(Native Method) 
             at java.lang.reflect.Method.invoke(Method.java:372) 
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
       (ZygoteInit.java:899) 
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
     Caused by: java.lang.reflect.InvocationTargetException 
       at java.lang.reflect.Constructor.newInstance(Native Method) 
       at java.lang.reflect.Constructor.newInstance 
    (Constructor.java:288) 
       at 
    android.view.LayoutInflater.createView(LayoutInflater.java:607) 
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView 
    (PhoneLayoutInflater.java:55) 
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
            at 
    android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView 
     (AppCompatDelegateImplV7.java:249) 
            at android.support.v7.app.AppCompatActivity.setContentView 
     (AppCompatActivity.java:106) 
             at com.bluewhale.percentagecalculator.MainActivity.onCreate 
     (MainActivity.java:17) 
            at android.app.Activity.performCreate(Activity.java:5937) 
            at 
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
            at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
            at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at 
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
     (ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 138883260 
     byte allocation with 1048576 free bytes and 63MB 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:973) 
      at 
    android.content.res.Resources.loadDrawableForCookie(Resources.java:2423) 
      at android.content.res.Resources.loadDrawable(Resources.java:2330) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
      at android.view.View.<init>(View.java:3730) 
      at android.view.ViewGroup.<init>(ViewGroup.java:491) 
      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 java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
            at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView 
     (PhoneLayoutInflater.java:55) 
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
            at 
    android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView 
    (AppCompatDelegateImplV7.java:249) 
            at android.support.v7.app.AppCompatActivity.setContentView 
    (AppCompatActivity.java:106) 
            at 
    com.bluewhale.percentagecalculator.MainActivity.onCreate 
    (MainActivity.java:17) 
            at android.app.Activity.performCreate(Activity.java:5937) 
            at android.app.Instrumentation.callActivityOnCreate 
    (Instrumentation.java:1105) 
            at android.app.ActivityThread.performLaunchActivity 
    (ActivityThread.java:2251) 
            at android.app.ActivityThread.handleLaunchActivity 
    (ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800 
    (ActivityThread.java:144) 
            at  
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
     (ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
     05-02 13:50:26.728 1241-1289/? W/ActivityManager﹕ 
    Force finishing activity com.bluewhale.percentagecalculator/.MainActivity 

预先感谢您!

+1

您是否正确地关闭了XML文件中的RelativeLayout标签?因为从我在这里看到它没有正确关闭。在文件末尾使用。或者它只是一个复制粘贴错误? – Jerry

回答

0
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 138883260 byte allocation with 1048576 free bytes and 63MB until OOM 

您正试图分配为您要使用的RelativeLayoutandroid:background图像138883260个字节(132MB〜)。你没有接近堆空间的地方。请使用较小的图像,或者使用RelativeLayout背景以外的其他位图,或者不要在此RelativeLayout上使用背景。

+0

但它自己的形象只有160 KB .. – mrBlue

+1

@mrBlue:这是磁盘上的大小。重要的是图像解码时的大小。 – CommonsWare

相关问题