Android制作简单的BMI应用程序崩溃

时间:2013-04-11 18:14:58

标签: android sdk crash

我正在开发一个简单的BMI应用程序,但我不知道当我启动应用程序崩溃时发生了什么。

文件名:MainActivity.java

package com.achkars.myownbmi;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

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

    public void calculate(View v){
        EditText number1text=(EditText)findViewById(R.id.num1text);
        EditText number2text=(EditText)findViewById(R.id.num2text);
        Integer num1=Integer.parseInt(number1text.getText().toString()),num2=Integer.parseInt(number2text.getText().toString());
        Integer ans=(num2/(num1*num1))*10000;

        TextView answer=(TextView)findViewById(R.id.answertxt);
        answer.setText("Your BMI is:"+ans.toString());
    }

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

}

文件名:activity_main.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:background="@style/AppTheme"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@android:drawable/screen_background_light" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@android:drawable/screen_background_dark_transparent"
         />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/wtw"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView2"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tocalc"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/num1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/yh"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/num1text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/Here"
            android:text="@string/text1" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ywer"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/num2text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/Here" />

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/calc"
            android:onClick="calculate" />

        <TextView
            android:id="@+id/answertxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/aaa"
            android:textColor="#000000" />

    </LinearLayout>

</RelativeLayout>

文件名:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.achkars.myownbmi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        >
        <activity
            android:name="com.achkars.myownbmi.MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:noHistory="true"
            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

感谢您的帮助。

logcat的:

04-11 18:28:16.713: W/Trace(799): Unexpected value from nativeGetEnabledTags: 0
04-11 18:28:16.733: W/Trace(799): Unexpected value from nativeGetEnabledTags: 0
04-11 18:28:16.943: D/AndroidRuntime(799): Shutting down VM
04-11 18:28:16.973: W/dalvikvm(799): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
04-11 18:28:17.023: E/AndroidRuntime(799): FATAL EXCEPTION: main
04-11 18:28:17.023: E/AndroidRuntime(799): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.achkars.myownbmi/com.achkars.myownbmi.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.os.Looper.loop(Looper.java:137)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-11 18:28:17.023: E/AndroidRuntime(799):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 18:28:17.023: E/AndroidRuntime(799):  at java.lang.reflect.Method.invoke(Method.java:511)
04-11 18:28:17.023: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-11 18:28:17.023: E/AndroidRuntime(799):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-11 18:28:17.023: E/AndroidRuntime(799):  at dalvik.system.NativeStart.main(Native Method)
04-11 18:28:17.023: E/AndroidRuntime(799): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.LayoutInflater.createView(LayoutInflater.java:613)
04-11 18:28:17.023: E/AndroidRuntime(799):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-11 18:28:17.023: E/AndroidRuntime(799):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.Activity.setContentView(Activity.java:1881)
04-11 18:28:17.023: E/AndroidRuntime(799):  at com.achkars.myownbmi.MainActivity.onCreate(MainActivity.java:15)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.Activity.performCreate(Activity.java:5104)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-11 18:28:17.023: E/AndroidRuntime(799):  ... 11 more
04-11 18:28:17.023: E/AndroidRuntime(799): Caused by: java.lang.reflect.InvocationTargetException
04-11 18:28:17.023: E/AndroidRuntime(799):  at java.lang.reflect.Constructor.constructNative(Native Method)
04-11 18:28:17.023: E/AndroidRuntime(799):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
04-11 18:28:17.023: E/AndroidRuntime(799):  ... 23 more
04-11 18:28:17.023: E/AndroidRuntime(799): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.content.res.Resources.loadDrawable(Resources.java:1927)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.View.<init>(View.java:3328)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.View.<init>(View.java:3257)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.view.ViewGroup.<init>(ViewGroup.java:425)
04-11 18:28:17.023: E/AndroidRuntime(799):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
04-11 18:28:17.023: E/AndroidRuntime(799):  ... 26 more

LogCat添加。

1 个答案:

答案 0 :(得分:0)

对于ImageView,请从

更改src
@android/drawable/...

@drawable/...