簡體   English   中英

Android應用在啟動時崩潰

[英]Android app crashes upon startup

我是編程新手,一段時間后,我終於解決了所有錯誤,但現在該應用在啟動時崩潰! 我沒有自己編寫所有這些代碼,但已對其進行了編輯。 另外,當我仍然收到錯誤消息時,該應用程序可以啟動(但無法正常運行)。 表現

    <?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);
        }
    }

在onCreate()的第二行中有一個錯誤:

setContentView(R.menu.main);

您不能將菜單設置為視圖。 如果要使用主布局,請使用以下命令:

setContentView(R.layout.main);

這個...

android:targetSdkVersion="22" />

是科幻小說!

今天的最大API級別是19

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM