Java 引用totalPriceEditText时的NullPointerException
我是Java和Android新手,我想制作一个Android程序,让用户输入标签价格,程序能够显示最终价格。(税后为8%)我被要求使用Netbean。没有红线,也没有错误消息。但每次我运行它时,它都会在模拟器中显示“不幸的是,税价计算器必须停止”。请帮帮我。我非常感谢大家的回答。谢谢 TaxCalculator.java:Java 引用totalPriceEditText时的NullPointerException,java,android,Java,Android,我是Java和Android新手,我想制作一个Android程序,让用户输入标签价格,程序能够显示最终价格。(税后为8%)我被要求使用Netbean。没有红线,也没有错误消息。但每次我运行它时,它都会在模拟器中显示“不幸的是,税价计算器必须停止”。请帮帮我。我非常感谢大家的回答。谢谢 TaxCalculator.java: package com.finalproject.taxcalculator; import android.app.Activity; import android.os
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>
似乎
onCreate()
中的变量赋值有错误,在引用totalPriceEditText
时导致NullPointerException
tagPriceEditText = (EditText)findViewById(R.id.tagPriceEditText);
tagPriceEditText = (EditText)findViewById(R.id.totalPriceEditText); // <- wrong?
您的
TaxCalculator
活动尚未在您的AndroidManifest.xml
1)更改应用程序入口点的名称(如果您没有MainActivity
),或2)添加新的
入口
将应用程序的入口点更改为TaxCalculator
(替换当前的
)的示例
那么,是否抛出了一些异常?除了告诉我它必须停止之外,什么都没有。我快疯了。你能告诉我你的电子邮件吗,这样我就可以给你发送文件了???NFW,但请随意扩展你的问题。你需要使用logcat并向我们显示报告的错误。Logcat内置于eclipse、intelliJ和android studio中,但可能不是netbeats,因此您需要执行命令行版本确定…仍然感谢…但不知道如何扩展问题,因为就个人而言,我没有发现任何错误…是的,这是一个错误,我错过了…修复了它,运行了它,但仍然是相同的结果…:(谢谢你的奖励,我想在我运行这个程序后,我会制作一个按钮,这样用户点击按钮后程序就会显示输出。当布局是线性的时候,可以添加一个按钮吗?对不起,我知道这个问题很愚蠢,但我真的是一个初学者…我只是在修复后测试了你的代码,没有得到任何错误。最后一件事是我猜错误在于,
TaxCalculator
活动尚未在AndroidManifest.xml
中注册。你能编辑你的帖子并添加你的AndroidManifest.xml
的内容吗?在LinearLayout
中添加按钮是可能的。真正的问题是你希望应用程序看起来如何,但uess在这个问题中它已经脱离主题。是否更改它取决于您自己。请注意,如果您这样做,您将需要更改类名+文件名,并更改清单文件中的活动名称。
tagPriceEditText = (EditText)findViewById(R.id.tagPriceEditText);
totalPriceEditText= (EditText)findViewById(R.id.totalPriceEditText);
<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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical"
android:padding="5dp" >
<!-- tagPriceInputRow -->
<EditText
android:id="@+id/tagPriceEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text="@string/tagPrice" >
</EditText>
<!-- totalPriceOutputRow -->
<EditText
android:id="@+id/totalPriceEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:text="@string/totalPrice" >
</EditText>
</LinearLayout>