0

一个简单的 android 应用程序来计算提示,我无法弄清楚这个错误。java 代码没有错误,xml 有几个警告。两者都发错了,但是当我运行它时,应用程序立即退出并说“应用程序tipConverter(process.com.example.tipconverter)已意外停止。请再试一次。” 我不知道为什么。

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Tip Calculator"/>
    <TextView
        android:id="@+id/widget28"
        android:layout_width="99dp"
        android:layout_height="17dp"
        android:text="Amount of Bill"
        android:layout_x="40dp"
        android:layout_y="32dp"/>
    <TextView
        android:id="@+id/widget29"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Percentage to Tip"
        android:layout_x="40dp"
        android:layout_y="82dp"/>
    <TextView
        android:id="@+id/widget30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Number of People"
        android:layout_x="40dp"
        android:layout_y="132dp"/>
    <TextView
        android:id="@+id/widget31"
        android:layout_width="wrap_content"
        android:layout_height="18dp"
        android:text="Tip Amout"
        android:layout_x="40dp"
        android:layout_y="262dp"/>
    <TextView
        android:id="@+id/widget32"
        android:layout_width="wrap_content"
        android:layout_height="18dp"
        android:text="Total Per Person"
        android:layout_x="40dp"
        android:layout_y="352dp"/>
    <TextView
        android:id="@+id/widget33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total To Pay"
        android:layout_x="40dp"
        android:layout_y="302dp"/>
    <Button
        android:id="@+id/btncalculate"
        android:layout_width="87dp"
        android:layout_height="wrap_content"
        android:text="Calculate"
        android:layout_x="40dp"
        android:layout_y="182dp"/>
    <Button
        android:id="@+id/btnreset"
        android:layout_width="86dp"
        android:layout_height="wrap_content"
        android:text="Reset"
        android:layout_x="140dp"
        android:layout_y="182dp"/>
    <EditText
        android:id="@+id/txtbillamount"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="100"
        android:textSize="18sp"
        android:layout_x="200dp"
        android:layout_y="22dp"/>
    <EditText
        android:id="@+id/txtpercentage"
        android:layout_width="51dp"
        android:layout_height="36dp"
        android:text="10"
        android:textSize="18sp"
        android:layout_x="200dp"
        android:layout_y="72dp"/>
    <EditText
        android:id="@+id/txtpeople"
        android:layout_width="wrap_content"
        android:layout_height="39dp"
        android:text="1         "
        android:textSize="18sp"
        android:layout_x="200dp"
        android:layout_y="122dp"/>
    <TextView
        android:id="@+id/txttipamount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_x="200dp"
        android:layout_y="262dp"/>
    <TextView
        android:id="@+id/txttotal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_x="200dp"
        android:layout_y="302dp"/>
    <TextView
        android:id="@+id/txtperperson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_x="200dp"
        android:layout_y="352dp"/>

</AbsoluteLayout>

这是java

package com.android;

import com.example.tipconverter.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;

public class tipcalc extends Activity {
    private EditText txtbillamount;
    private EditText txtpeople;
    private EditText txtpercentage;
    private TextView txtperperson;
    private TextView txttipamount;
    private TextView txttotal;
    private Button btncalculate;
    private Button btnreset;
    private double billamount = 0;
    private double percentage = 0;
    private double numofpeople=0;
    private double tipamount = 0;
    private double totaltopay = 0;
    private double perperson = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initControls();
    }

    private void initControls() {
        txtbillamount = (EditText)findViewById(R.id.txtbillamount);
        txtpeople = (EditText)findViewById(R.id.txtpeople);
        txtpercentage = (EditText)findViewById(R.id.txtpercentage);
        txtperperson=(TextView)findViewById(R.id.txtperperson);
        txttipamount=(TextView)findViewById(R.id.txttipamount);
        txttotal=(TextView)findViewById(R.id.txttotal);
        btncalculate = (Button)findViewById(R.id.btncalculate);
        btnreset = (Button)findViewById(R.id.btnreset);
        btncalculate.setOnClickListener(new Button.OnClickListener() { 
            public void onClick (View v)  { 
                calculate(); 
            }
        });

        btnreset.setOnClickListener(new Button.OnClickListener() { 
            public void onClick (View v){ 
                reset(); 
            }
        });
    }

    private void calculate() {
        billamount=Double.parseDouble(txtbillamount.getText().toString());
        percentage=Double.parseDouble(txtpercentage.getText().toString());
        numofpeople=Double.parseDouble(txtpeople.getText().toString());
        tipamount=(billamount*percentage)/100;
        totaltopay=billamount+tipamount;
        perperson=totaltopay/numofpeople;
        txttipamount.setText(Double.toString(tipamount));
        txttotal.setText(Double.toString(totaltopay));
        txtperperson.setText(Double.toString(perperson));
    }

    private void reset() {
        txtbillamount.setText("");
        txtpeople.setText("");
        txtpercentage.setText("");
        txtperperson.setText("");
        txttipamount.setText("");
        txttotal.setText("");
    }
}

任何提示或帮助将不胜感激。谢谢!

编辑

这是 logcat 打印的内容

07-09 02:49:02.208: D/AndroidRuntime(283): >>>>>>>>>>>>>> AndroidRuntime START     <<<<<<<<<<<<<<
07-09 02:49:02.208: D/AndroidRuntime(283): CheckJNI is ON
07-09 02:49:02.278: D/AndroidRuntime(283): --- registering native functions ---
07-09 02:49:02.598: D/AndroidRuntime(283): Shutting down VM
07-09 02:49:02.598: D/dalvikvm(283): Debugger has detached; object registry had 1 entries
07-09 02:49:02.608: I/AndroidRuntime(283): NOTE: attach of thread 'Binder Thread #3'  failed
07-09 02:49:02.878: D/AndroidRuntime(291): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
07-09 02:49:02.878: D/AndroidRuntime(291): CheckJNI is ON
07-09 02:49:02.958: D/AndroidRuntime(291): --- registering native functions ---
07-09 02:49:03.267: I/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.tipconverter/com.example.tipconverter }
07-09 02:49:03.277: I/ActivityManager(58): Start proc com.example.tipconverter for activity com.example.tipconverter/com.example.tipconverter: pid=297 uid=10036 gids={}
07-09 02:49:03.317: D/AndroidRuntime(291): Shutting down VM
07-09 02:49:03.317: D/dalvikvm(291): Debugger has detached; object registry had 1 entries
07-09 02:49:03.577: D/AndroidRuntime(297): Shutting down VM
07-09 02:49:03.577: W/dalvikvm(297): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-09 02:49:03.597: I/ARMAssembler(58): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x381c90:0x381d9c] in 289122 ns
07-09 02:49:03.597: E/AndroidRuntime(297): FATAL EXCEPTION: main
07-09 02:49:03.597: E/AndroidRuntime(297): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tipconverter/com.example.tipconverter}: java.lang.ClassNotFoundException: com.example.tipconverter in loader dalvik.system.PathClassLoader[/data/app/com.example.tipconverter-1.apk]
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-09 02:49:03.597: E/AndroidRuntime(297):  at   android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.os.Looper.loop(Looper.java:123)
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 02:49:03.597: E/AndroidRuntime(297):  at java.lang.reflect.Method.invokeNative(Native Method)
07-09 02:49:03.597: E/AndroidRuntime(297):  at java.lang.reflect.Method.invoke(Method.java:521)
07-09 02:49:03.597: E/AndroidRuntime(297):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-09 02:49:03.597: E/AndroidRuntime(297):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-09 02:49:03.597: E/AndroidRuntime(297):  at dalvik.system.NativeStart.main(Native Method)
07-09 02:49:03.597: E/AndroidRuntime(297): Caused by: java.lang.ClassNotFoundException: com.example.tipconverter in loader dalvik.system.PathClassLoader[/data/app/com.example.tipconverter-1.apk]
07-09 02:49:03.597: E/AndroidRuntime(297):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-09 02:49:03.597: E/AndroidRuntime(297):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-09 02:49:03.597: E/AndroidRuntime(297):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-09 02:49:03.597: E/AndroidRuntime(297):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-09 02:49:03.597: E/AndroidRuntime(297):  ... 11 more
07-09 02:49:03.617: W/ActivityManager(58):   Force finishing activity com.example.tipconverter/com.example.tipconverter
07-09 02:49:04.119: W/ActivityManager(58): Activity pause timeout for HistoryRecord{45075650 com.example.tipconverter/com.example.tipconverter}
07-09 02:49:14.293: W/ActivityManager(58): Activity destroy timeout for     HistoryRecord{45075650 com.example.tipconverter/com.example.tipconverter}
4

2 回答 2

0

您是否在 Android 清单中声明了您的活动?

于 2012-07-09T01:06:37.300 回答
0

您可以查看日志:

[2012-07-08 18:05:33 - tipConverter] Starting activity    com.example.tipconverter.MainActivity on device emulator-5554

它正在寻找 Android 清单文件中定义的 MainActivity.java。您可能已将其重命名为tipcalc.java。您需要在 Android 清单文件中进行调整。

更新:

这似乎是一个损坏的 Android 项目设置。结果,生成了一个错误的 APK。您可以检查 Android 项目的 .properties 和 .project 文件。如有疑问,请尝试从头开始创建一个新项目并比较所提及文件的差异。还要确保您拥有正确的目标 SDK 版本。

于 2012-07-09T01:45:15.457 回答