嗨我刚接触到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)
答案 0 :(得分:1)
检查以下内容:
答案 1 :(得分:1)
查看以下内容:
itemName
的长度与fields.length
的长度相同,并且应该正确初始化。labels
的长度与fields.length
的长度相同,并且应该正确初始化。itemHour
的长度与fields.length
的长度相同,并且应该正确初始化。答案 2 :(得分:0)
由于这是NullPointerException,您要么有一些空字段,要么忘记初始化某些字段,
检查,
aButton = (Button)findViewById(R.id.ButtonID);
如果您已初始化itemName[]
和itemHour[]
,请itemHour[ctr] = new String[];
如果labels[]
和fields[]
如果它仍然没有帮助,那么你应该发布更多的相关代码,然后才能编辑我的答案。