我将在这里发布代码。
我在手机上使用 AIDE 开发了一个简单的应用程序,而不是在 PC 上使用 Android Studio。它是 Udemy 课程的一部分。
当我单击以在 AIDE 上运行时,它一直在崩溃。
当我点击运行时,会出现一个弹出窗口,上面写着“此应用可能有害并可能损坏您的设备和数据。我们不建议您安装此应用”
我点击安装。
片刻之后,应用程序关闭,然后出现任何消息“不幸的是,MyApp 必须停止”。
代码有没有问题。我将在下面发布所有 java、XML 和清单代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:clipChildren="true"
android:padding="13dp">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="Bill Total"
android:visibility="visible"
android:alpha="1.0"
android:layout_gravity="center"
android:textColor="#6CE6E0"
android:textStyle="bold"
android:id="@+id/BillTotal"/>
<EditText
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_width="wrap_content"
android:ems="10"
android:layout_gravity="center"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:text="Tip Percentage"
android:layout_gravity="center"
android:paddingTop="12dp"
android:textStyle="bold"
android:textColor="#63E4D6"
android:id="@+id/TipPercentage"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center">
</RelativeLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="10"
android:gravity="center"
android:id="@+id/Percent10"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="15"
android:gravity="center"
android:id="@+id/Percent15"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="20"
android:gravity="center"
android:id="@+id/Percent20"/>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="Tip: $0.00"
android:layout_gravity="center"
android:textStyle="bold"
android:textColor="#86DFE0"
android:id="@+id/TipTotal"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="Total: $0.00"
android:layout_gravity="center"
android:textStyle="bold"
android:textColor="#87DFD6"
android:id="@+id/Total"/>
</LinearLayout>
主要的java代码
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.graphics.*;
import android.widget.*;
import android.view.View.*;
public class MainActivity extends Activity
{
TextView tipTotalTextView;
TextView totalTextView;
EditText billTotalEditView;
Button TenPercentButton;
Button FifteenPercentButton;
Button TwentyPercentButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().getDecorView().setBackgroundColor(Color.BLUE);
tipTotalTextView = (TextView)findViewById(R.id.TipTotal);
totalTextView = (TextView)findViewById(R.id.Total);
billTotalEditView = (EditText)findViewById(R.id.BillTotal);
TenPercentButton = (Button)findViewById(R.id.Percent10);
FifteenPercentButton = (Button)findViewById(R.id.Percent15);
TwentyPercentButton = (Button)findViewById(R.id.Percent20);
TenPercentButton.setOnClickListener(new android.view.View.OnClickListener(){
public void onClick(android.view.View v){
Double TotalOfBill = Double.valueOf(billTotalEditView.getText().toString());
Double TotalTip = TotalOfBill * 0.1;
tipTotalTextView.setText("Tip: $"+TotalTip.toString());
Double total = TotalOfBill + TotalTip;
totalTextView.setText("Tip: $"+total.toString());
}
});
FifteenPercentButton.setOnClickListener(new android.view.View.OnClickListener(){
public void onClick(android.view.View v){
Double TotalOfBill = Double.valueOf(billTotalEditView.getText().toString());
Double TotalTip = TotalOfBill * 0.15;
tipTotalTextView.setText("Tip: $"+TotalTip.toString());
Double total = TotalOfBill + TotalTip;
totalTextView.setText("Tip: $"+total.toString());
}
});
TwentyPercentButton.setOnClickListener(new android.view.View.OnClickListener(){
public void onClick(android.view.View v){
Double TotalOfBill = Double.valueOf(billTotalEditView.getText().toString());
Double TotalTip = TotalOfBill * 0.2;
tipTotalTextView.setText("Tip: $"+TotalTip.toString());
Double total = TotalOfBill + TotalTip;
totalTextView.setText("Tip: $"+total.toString());
}
});
}
}
和清单代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:resizeableActivity = "true">
<activity
android:name=".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>