我是Java和Android的新手,我想创建一个Android程序,让用户输入标签价格,程序能够显示最终价格。 (税后8%)我需要使用Netbean。没有红线也没有错误信息。但每次我运行它,最终都会在模拟器中显示“不幸的是,税价计算器必须停止”。请帮我。我非常感谢大家的回答。感谢!!!!!!
TaxCalculator.java:
package com.finalproject.taxcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.text.TextWatcher;
import android.text.Editable;
public class TaxCalculator extends Activity{
private static final String TAG_PRICE = "TAG_PRICE";
private static final String TOTAL_PRICE = "TOTAL_PRICE";
private static final double TAX_RATE = 0.08;//Tax rate in Philadelphia
private double tagPrice;//Tag price entered by the user
private double totalPrice;//Total prices calculated by the program
private EditText tagPriceEditText;//accepts input for tag prices
private EditText totalPriceEditText;//displays total prices after tax
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
// constants used when saving/restoring state
super.onCreate(savedInstanceState);// call superclass's version
setContentView(R.layout.main);// inflate the GUI
// check if app just started or is being restored from memory
if ( savedInstanceState == null ) // the app just started running
{
tagPrice = 0.0; // initialize the tag price to zero
} // end if
else // app is being restored from memory, not executed from scratch
{
// initialize the tag price to saved amount
tagPrice = savedInstanceState.getDouble(TAG_PRICE);
} // end else
// get references to tag and total price edit text
tagPriceEditText = (EditText)findViewById(R.id.tagPriceEditText);
tagPriceEditText = (EditText)findViewById(R.id.totalPriceEditText);
// tagPriceEditTextWatcher handles tagPriceEditText's onTextChanged event
tagPriceEditText.addTextChangedListener(tagPriceEditTextWatcher);
}// end method onCreate
private void updateStandard()
{
// calculate the total price after the tax
totalPrice = tagPrice * (1 + TAX_RATE);
// set totalPriceEditText's text to total price
totalPriceEditText.setText(String.format("%.02f", totalPrice));
} // end method updateStandard
// save values of tagPriceEditText
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putDouble(TAG_PRICE, tagPrice);
} // end method onSaveInstanceState
// event-handling object that responds to tagPriceEditText's events
private TextWatcher tagPriceEditTextWatcher = new TextWatcher()
{
// called when the user enters a number
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
// convert billEditText's text to a double
try
{
tagPrice = Double.parseDouble(s.toString());
} // end try
catch (NumberFormatException e)
{
tagPrice = 0.0; // default if an exception occurs
} // end catch
// update the tagPriceEditText
updateStandard();
} // end method onTextChanged
@Override
public void afterTextChanged(Editable s)
{
} // end method afterTextChanged
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
} // end method beforeTextChanged
}; // end tagPriceEditTextWatcher
} // end class TaxCaculator
来自main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#FFF" android:id="@+id/tableLayout"
android:stretchColumns="1,2,3" android:padding="5dp">
<!-- tagPriceInputRow -->
<TableRow android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/tagPriceInputRow">
<EditText android:layout_width="wrap_content"
android:id="@+id/tagPriceEditText"
android:inputType="number"
android:text="@string/tagPrice"
android:layout_height="wrap_content" android:layout_span="3"
android:layout_weight="1">
</EditText>
</TableRow>
<!-- totalPriceOutputRow -->
<TableRow android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/totalPriceOutputRow">
<EditText android:layout_width="wrap_content"
android:id="@+id/totalPriceEditText"
android:text="@string/totalPrice"
android:layout_height="wrap_content" android:layout_span="3"
android:inputType="numberDecimal" android:layout_weight="1">
</EditText>
</TableRow>
</TableLayout>
来自strings.xml的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Tax Price Calculator</string>
<string name="tagPrice">Tag Price</string>
<string name="totalPrice">Total Price</string>
</resources>
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.finalproject.taxcalculator"
android:versionCode="1"
android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<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>
答案 0 :(得分:2)
似乎onCreate()
内的变量赋值存在错误,导致NullPointerException
被引用{/ 1}}。
totalPriceEditText
应改为
tagPriceEditText = (EditText)findViewById(R.id.tagPriceEditText);
tagPriceEditText = (EditText)findViewById(R.id.totalPriceEditText); // <- wrong?
您的tagPriceEditText = (EditText)findViewById(R.id.tagPriceEditText);
totalPriceEditText= (EditText)findViewById(R.id.totalPriceEditText);
活动尚未在TaxCalculator
中注册
1)更改应用程序入口点的名称(如果您没有AndroidManifest.xml
),或者2)添加新的MainActivity
条目。
将应用的切入点更改为<activity>
的示例(替换当前的TaxCalculator
)
<activity>
奖励:如果您的布局已经是最终版,则可以清除代码。这只是您当前布局的建议。我仍然不知道你是否需要<activity android:name="TaxCalculator"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
以供将来使用。
TableLayout