Android应用在启动时崩溃

时间:2014-01-18 19:48:27

标签: java android

我是编程新手,过了一段时间我终于解决了所有错误,但现在应用程序在启动时崩溃了!我没有自己编写所有这些代码,但我编辑了它。此外,当我仍然收到错误时,应用程序可以启动(但显然不能正常工作)。 清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tk.iWeld.iweld"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="tk.iWeld.iweld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Activity_Main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
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"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText2"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText2"
    android:layout_alignBottom="@+id/editText2"
    android:layout_alignParentRight="true"
    android:layout_marginRight="20dp"
    android:ems="10"
    android:hint="Text2"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:text="X"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/holo_orange_dark" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:text="_________________________"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/holo_orange_dark" />

<EditText
    android:id="@+id/Text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:hint="Text3"
    android:inputType="number" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/Text3"
    android:layout_marginTop="22dp"
    android:text="="
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:textSize="30sp" />

<TextView
    android:id="@+id/textRes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignParentRight="true"
    android:clickable="false"
    android:longClickable="false"
    android:text="Result"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/resultbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignTop="@+id/textView3"
    android:text="Result" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="89dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignLeft="@+id/textView2"
    android:ems="10"
    android:hint="Text1"
    android:inputType="number" />

 和MainActivity

    package tk.iWeld.iweld;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;

import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

        /** Called when the activity is first created. */

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.menu.main);
        }


        public void calculateClickHandler(View view) {
         // make sure we handle the click of the calculator button

         if (view.getId() == R.id.resultbutton) {

          // get the references to the widgets
          EditText text1Text = (EditText)findViewById(R.id.editText1);
          EditText text2Text = (EditText)findViewById(R.id.editText2);
          EditText text3Text = (EditText)findViewById(R.id.Text3);
          TextView resultText = (TextView)findViewById(R.id.textRes);

          // get the users values from the widget references

          float text1 = Float.parseFloat(text1Text.getText().toString());
          float text2 = Float.parseFloat(text2Text.getText().toString());
          float text3 = Float.parseFloat(text3Text.getText().toString());

          // calculate the result value

          float totalresult = calculateRESULT(text1, text2, text3);


          // now set the value in the result text

          resultText.setText("" + totalresult);
         }
        }

        // the formula to calculate the result index

        private float calculateRESULT (float text1, float text2, float text3) {

         return (float) (text1 * text2 / text3);
        }
    }

2 个答案:

答案 0 :(得分:8)

onCreate()的第二行有一个错误:

setContentView(R.menu.main);

您无法将菜单设置为视图。如果要使用主布局,请使用:

setContentView(R.layout.main);

答案 1 :(得分:6)

此...

android:targetSdkVersion="22" />

是科幻小说!

今天的最高API级别 19