嗨,我是 android 编程新手,单击按钮时此代码会产生错误...不幸的是,它显示应用程序已停止,任何人都可以检查吗?:(
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.compute);
aButton = (Button) this.findViewById(R.id.Button06);
//get intent
Intent i = getIntent();
Bundle extras = i.getExtras(); // get the parameters
density = getBaseContext().getResources().getDisplayMetrics().density;
int textViewSizeDp = (int)(200 * density);
int textFieldSizeDp = (int)(50 * density);
FieldsLayout = (LinearLayout) findViewById(R.id.FieldsLayout);
if(extras!=null){
items = (extras.getString("items")).split(",");
rows = new LinearLayout[items.length];
labels = new TextView[items.length];
fields = new EditText[items.length];
for (int ctr=0;ctr<items.length;ctr++){
rows[ctr] = new LinearLayout(Compute.this);
rows[ctr].setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
rows[ctr].setOrientation(LinearLayout.HORIZONTAL);
rows[ctr].setLayoutParams(new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
FieldsLayout.addView(rows[ctr]);
labels[ctr] = new TextView(Compute.this);
labels[ctr].setText(items[ctr]);
labels[ctr].setMinimumWidth(textViewSizeDp);
rows[ctr].addView(labels[ctr]);
fields[ctr] = new EditText(Compute.this);
fields[ctr].setMinimumWidth(textFieldSizeDp);
rows[ctr].addView(fields[ctr]);
}
}
else{
finish();
}
aButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Compute.this, Total.class);
//I believe this causes the error
for(int ctr=0;ctr<fields.length;ctr++){
itemName[ctr] = labels[ctr].getText().toString();
}
for(int ctr=0;ctr<fields.length;ctr++){
itemHour[ctr] = Integer.parseInt(fields[ctr].getText().toString());
}
//until here
i.putExtra("hours",itemHour);
i.putExtra("items",itemName);
startActivity(i);
}});
}
这是错误日志:(或者是这个吗?)
01-14 09:37:16.047: W/Trace(768): Unexpected value from nativeGetEnabledTags: 0
01-14 09:37:16.187: E/AndroidRuntime(768): FATAL EXCEPTION: main
01-14 09:37:16.187: E/AndroidRuntime(768): java.lang.NullPointerException
01-14 09:37:16.187: E/AndroidRuntime(768): at com.example.mpc.Compute$1.onClick(Compute.java:82)
01-14 09:37:16.187: E/AndroidRuntime(768): at android.view.View.performClick(View.java:4202)
01-14 09:37:16.187: E/AndroidRuntime(768): at android.view.View$PerformClick.run(View.java:17340)
01-14 09:37:16.187: E/AndroidRuntime(768): at android.os.Handler.handleCallback(Handler.java:725)
01-14 09:37:16.187: E/AndroidRuntime(768): at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 09:37:16.187: E/AndroidRuntime(768): at android.os.Looper.loop(Looper.java:137)
01-14 09:37:16.187: E/AndroidRuntime(768): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-14 09:37:16.187: E/AndroidRuntime(768): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 09:37:16.187: E/AndroidRuntime(768): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 09:37:16.187: E/AndroidRuntime(768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-14 09:37:16.187: E/AndroidRuntime(768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-14 09:37:16.187: E/AndroidRuntime(768): at dalvik.system.NativeStart.main(Native Method)