我在按钮点击时为计算器创建一个弹出窗口。我创建了,当运行应用程序时,应用程序崩溃,我得到nullPointerException。我点击按钮时想要弹出计算器。有人可以帮我。这是我的代码。谢谢高级。
这是我的计算器活动代码
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.damduppatyojna);
imgCalculator = (ImageView)findViewById(R.id.buttonCalculator);
imgCalculator.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_calculator, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
interestRatesSpinner = (Spinner) findViewById(R.id.interestSpinner);
periodSpinner = (Spinner) findViewById(R.id.periodSpinner);
amountEditText = (EditText) findViewById(R.id.amountEditText);
toBeCalculatedTextView = (TextView) findViewById(R.id.toBeCalculatedTextView);
//
//ArrayAdapter<CharSequence> ir_adapter = ArrayAdapter.createFromResource(this, R.array.interest_rate_array,android.R.layout.simple_spinner_item);
//ArrayAdapter<CharSequence> ir_adapter = ArrayAdapter.createFromResource(this, R.array.interest_rate_array,android.R.layout.simple_spinner_item);
ArrayAdapter<?> ir_adapter = ArrayAdapter.createFromResource(DamDuppatYojna.this, R.array.interest_rate_array, R.id.interestSpinner);
ir_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
interestRatesSpinner.setAdapter(ir_adapter);
ir_adapter = ArrayAdapter.createFromResource(DamDuppatYojna.this,R.array.period_array, android.R.layout.simple_spinner_item);
ir_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
periodSpinner.setAdapter(ir_adapter);
Button btnCalculator = (Button)popupView.findViewById(R.id.calculatorButton);
btnCalculator.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.lokmanya.ResultActivity");
Bundle extras = new Bundle();
extras.putString("amount", amountEditText.getText().toString());
extras.putString("interestrate", interestRatesSpinner.getSelectedItem().toString());
extras.putString("period", periodSpinner.getSelectedItem()
.toString());
intent.putExtras(extras);
startActivityForResult(intent, 1);
}
});
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(imgCalculator, 50, -30);
}});
这是结果活动
public class ResultActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d(this.getClass().getName(), "Now in On Create");
this.setContentView(R.layout.result);
Bundle bundle = getIntent().getExtras();
Log.d(this.getClass().getName(),"Interest Rate: "+bundle.getString("interestrate"));
float interestrate = Float.parseFloat(bundle.getString("interestrate"));
float period = Float.parseFloat(bundle.getString("period"));
float amount = Float.parseFloat(bundle.getString("amount"));
float monthly = ((amount * (interestrate/100)*period)+amount)/(period * 12);
Intent data = new Intent();
data.setData(Uri.parse("" + monthly));
setResult(1,data); // first param to indicate resultCode
this.finish();
}
}
这是xml文件
<?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="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout_Form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/amountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="right"
android:text="@string/amountString" >
</TextView>
<EditText
android:id="@+id/amountEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/interestRateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:text="@string/interestRateString" >
</TextView>
<Spinner
android:id="@+id/interestSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/loadPeriodTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:text="@string/loanPeriodString" >
</TextView>
<Spinner
android:id="@+id/periodSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Spinner>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/monthlyInstallmentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/monthlyInstallmentString" >
</TextView>
<TextView
android:id="@+id/toBeCalculatedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/toBeCalculatedString" >
</TextView>
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/calculatorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save Settings" >
</Button>
<Button
android:id="@+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Load Settings" >
</Button>
</LinearLayout>
</LinearLayout>
这是Log cat text Information。
06-05 14:08:13.162: E/AndroidRuntime(751): FATAL EXCEPTION: main
06-05 14:08:13.162: E/AndroidRuntime(751): java.lang.NullPointerException
06-05 14:08:13.162: E/AndroidRuntime(751): at com.lokmanya.DamDuppatYojna$1.onClick(DamDuppatYojna.java:62)
06-05 14:08:13.162: E/AndroidRuntime(751): at android.view.View.performClick(View.java:2485)
06-05 14:08:13.162: E/AndroidRuntime(751): at android.view.View$PerformClick.run(View.java:9080)
06-05 14:08:13.162: E/AndroidRuntime(751): at android.os.Handler.handleCallback(Handler.java:587)
06-05 14:08:13.162: E/AndroidRuntime(751): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 14:08:13.162: E/AndroidRuntime(751): at android.os.Looper.loop(Looper.java:123)
06-05 14:08:13.162: E/AndroidRuntime(751): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-05 14:08:13.162: E/AndroidRuntime(751): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 14:08:13.162: E/AndroidRuntime(751): at java.lang.reflect.Method.invoke(Method.java:507)
06-05 14:08:13.162: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 14:08:13.162: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-05 14:08:13.162: E/AndroidRuntime(751): at dalvik.system.NativeStart.main(Native Method)
06-05 14:08:15.503: I/Process(751): Sending signal. PID: 751 SIG: 9
答案 0 :(得分:-1)
根据提供的代码,interestRatesSpinner
为空或ir_adapter
为空。我怀疑interestRatesSpinner是否为null。