0

这是我的方法

private TextView monthTotal;
    private TextView yearTotal;
    private ExpenseDal expenseDal;

    private EditText amountText;
    private EditText descText;
    private EditText categoryText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        expenseDal = new ExpenseDal(this);
        amountText = (EditText) findViewById(R.id.amountText);
        descText = (EditText) findViewById(R.id.descText);
        categoryText = (EditText) findViewById(R.id.categoryText);

        setContentView(R.layout.activity_expense_list);
        monthTotal = (TextView) findViewById(R.id.monthTotal);
        yearTotal = (TextView) findViewById(R.id.yearTotal);
        monthTotal.setText(expenseDal.getCurrentMonthTotal());
        yearTotal.setText(expenseDal.getCurrentYearTotal());

        //Event For Save Button
        Button saveButton = (Button) findViewById(R.id.saveBtn);
        saveButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (amountText.getText().length() != 0) {
                    AsyncTask<Object, Object, Object> saveExpenseTask = new AsyncTask<Object, Object, Object>() {
                        @Override
                        protected Object doInBackground(Object... params) {
                            saveExpense();
                            return null;
                        }
                        @Override
                        protected void onPostExecute(Object result) {
                            finish();
                        }
                    };

                    saveExpenseTask.execute((Object[]) null);
                }

                else {
                    AlertDialog.Builder alert = new AlertDialog.Builder(
                            ExpenseList.this);
                    alert.setTitle(R.string.errorTitle);
                    alert.setMessage(R.string.errorMessage);
                    alert.setPositiveButton(R.string.errorButton, null);
                    alert.show();
                }
            }
        });

    }

    private void saveExpense() { }

我的踪迹在下面

12-17 04:53:53.003: D/AndroidRuntime(2484): Shutting down VM
12-17 04:53:53.003: W/dalvikvm(2484): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-17 04:53:53.003: E/AndroidRuntime(2484): FATAL EXCEPTION: main
12-17 04:53:53.003: E/AndroidRuntime(2484): java.lang.NullPointerException
12-17 04:53:53.003: E/AndroidRuntime(2484):     at com.technowizsol.easyexpensemanager.ExpenseList$1.onClick(ExpenseList.java:45)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at android.view.View.performClick(View.java:4084)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at android.view.View$PerformClick.run(View.java:16966)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at android.os.Handler.handleCallback(Handler.java:615)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at android.os.Looper.loop(Looper.java:137)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at java.lang.reflect.Method.invoke(Method.java:511)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-17 04:53:53.003: E/AndroidRuntime(2484):     at dalvik.system.NativeStart.main(Native Method)

错误出现在我检查金额是否为空的行上。任何想法为什么我得到这个。

4

1 回答 1

1

走线

setContentView(R.layout.activity_expense_list);

移动它,使其在正下方

super.onCreate(savedInstanceState);

您现在设置它的方式是在实际设置布局之前尝试在布局中找到一些视图。因此,findViewById()没有为这些视图找到任何东西(尚不存在),因此它返回null

此外,您不是在检查是否amountText为空,而是在检查其文本长度是否为 0 - 两个不同的东西。由于amountText实际上没有引用任何内容,因此您将获得 NPE。

于 2012-12-16T23:32:28.410 回答