我的应用程序无效。.完成调试,但未启动

时间:2018-11-26 22:30:03

标签: android xml android-studio android-layout

我不知道我的应用程序有什么问题。我刚刚完成布局,但是在我的设备上安装它之后却没有启动。有人可以帮我吗?

这是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Please Enter All Data In TON and METER CUBED ONLY" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="12dp">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <EditText
                    android:id="@+id/H"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Height Of The Wall" />

                <EditText
                    android:id="@+id/gamma_stat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Saturated Specific Weight " />

                <EditText
                    android:id="@+id/gamma_dry"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Dry Specific Weight " />

                <EditText
                    android:id="@+id/gamma_water"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Water Specific Weight " />

                <EditText
                    android:id="@+id/gamma_concrete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Concrete Specific Weight " />

                <EditText
                    android:id="@+id/live_load"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Value Of Live Load " />

                <EditText
                    android:id="@+id/fai"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Angle Of Internal Fraction " />

                <EditText
                    android:id="@+id/water_high_level"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter High Water Level" />

                <EditText
                    android:id="@+id/street_level"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Street Level" />

                <EditText
                    android:id="@+id/bottom_level"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter bottom Level" />

                <EditText
                    android:id="@+id/fraction_factor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Fraction Factor" />

                <EditText
                    android:id="@+id/allowable_stress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="Enter Allowable Stress" />

            </LinearLayout>


            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

        </LinearLayout>

        <Button
            android:id="@+id/calculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:text="calculate"
            android:textAllCaps="true" />

    </RelativeLayout>

</ScrollView>

这很容易,但是我不知道这是怎么回事? 这是代码摘要和所需的布局:

代码图像摘要: enter image description here

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.gravitywall">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".Results"></activity>
    </application>

</manifest>

这也是我的Java代码:

package com.example.android.gravitywall;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText heightEditText = findViewById(R.id.H);
    double H = Double.parseDouble(String.valueOf(heightEditText.getText()));

    EditText gammaStatEditText = findViewById(R.id.gamma_stat);
    double gammaStat = Double.parseDouble(String.valueOf(gammaStatEditText.getText()));

    EditText gammaDryEditText = findViewById(R.id.gamma_dry);
    double gammaDry = Double.parseDouble(String.valueOf(gammaDryEditText.getText()));

    EditText gammaWaterEditText = findViewById(R.id.gamma_water);
    double gammaWater = Double.parseDouble(String.valueOf(gammaWaterEditText.getText()));

    EditText gammaConcreteEditText = findViewById(R.id.gamma_concrete);
    double gammaConcrete = Double.parseDouble(String.valueOf(gammaConcreteEditText.getText()));

    EditText liveLoadEditText = findViewById(R.id.live_load);
    double liveLoad = Double.parseDouble(String.valueOf(liveLoadEditText.getText()));

    EditText faiEditText = findViewById(R.id.fai);
    double sinfai = Math.sin(Double.parseDouble(String.valueOf(faiEditText.getText())));

    EditText highWaterLevelEditText = findViewById(R.id.water_high_level);
    double H_W_L = Double.parseDouble(String.valueOf(highWaterLevelEditText.getText()));

    EditText streetLevelEditText = findViewById(R.id.street_level);
    double S_L = Double.parseDouble(String.valueOf(streetLevelEditText.getText()));

    EditText bottomLevelEditText = findViewById(R.id.bottom_level);
    double B_L = Double.parseDouble(String.valueOf(bottomLevelEditText.getText()));

    EditText fractionFactorEditText = findViewById(R.id.fraction_factor);
    double F_F = Double.parseDouble(String.valueOf(fractionFactorEditText.getText()));

    EditText allowableStressEditText = findViewById(R.id.allowable_stress);
    double muo = Double.parseDouble(String.valueOf(allowableStressEditText.getText()));

    double h1 = S_L - H_W_L;

    double h2 = H_W_L - B_L;

    double K = (1-sinfai)/(1+sinfai);

    double gammaSubmerged = gammaStat - gammaWater;

    double gammaAvrg = (gammaStat + gammaDry)/2;

    double b1 = b1Calculation(H);

    double b2 = b1 + (H/4);

    double Tf = .2*H;

    double B = b2 + (2*Tf);

    double Ht = H + Tf;

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

        Button calculateButton =  findViewById(R.id.calculate);
        calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent resultsActivity = new Intent(MainActivity.this, Results.class);
                startActivity(resultsActivity);
            }
        });
    }

    public double b1Calculation(double h) {
        double bTrial = 0;
        if (0.2*h > .5){
            bTrial = .5;
        }
        return bTrial;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您应该将所有计算都放在实际分配之后。

第二,Q需要显式转换为实际的View类型。(我没有运行代码,只是将其粘贴到Android Studio中。那里似乎没有警告。)

第三,在所有情况下,before Android P都可以调用findViewById()而不是EditText.getText().toString()(其余情况为when this EditText is displaying。)

然后将代码修改为:

String.valueOf(EditText.getText())

此外,最好放置logcat。应用程序崩溃时logcat中没有理由吗? “没有开始”吗?大概。