0

嗨,我是 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)
4

3 回答 3

1

检查以下内容:

  1. 按钮的 ID
  2. 同一布局中的相同 ID 多于一个按钮。
  3. 它的映射是否带有 xml 文件。
于 2013-01-14T09:39:30.443 回答
1

检查以下内容:

  1. 确保您的字段数组已正确初始化。
  2. Make sure that the length of itemName is same as that of fields.length and it should be properly initialized.
  3. Make sure that the length of labels is same as that of fields.length and it should be properly initialized.
  4. Make sure that the length of itemHour is same as that of fields.length and it should be properly initialized.
于 2013-01-14T09:50:07.337 回答
0

由于这是 NullPointerException,您要么有一些空字段,要么您忘记初始化一些字段,

查看,

  • 如果你做了aButton = (Button)findViewById(R.id.ButtonID);
  • 如果您已初始化itemName[]and itemHour[],则类似于itemHour[ctr] = new String[];

  • labels[]如果and中有任何值fields[]

如果它仍然没有帮助,那么你应该发布一些更相关的代码,然后我才能编辑我的答案。

于 2013-01-14T09:49:57.990 回答