从android中的自定义方法引用textview

时间:2014-04-19 11:38:59

标签: java android eclipse

我从Java开始,所以请耐心等待。因此,这段代码的主要内容是当用户输入每月预算时,它应该调用TextWatcher,然后将每日预算返回到BudgetSettingActivity,在那里它将值设置为DailyBudgetView TextView。现在,问题是,我如何获得对该视图的引用?使用当前代码我的应用程序崩溃,一旦我尝试调试它,我看到,当我尝试获取DailyBudgetView的引用时崩溃。

这是BudgetSettingActivity类:

package nick.mirosh.budgetplanner;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class BudgetSettingsActivity extends Activity {

    double dailyBudget;
    String[] monthNames = { "January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November",
            "December" };
    TextView dailyBudgetView;

    @SuppressLint("SimpleDateFormat")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_budget_settings);

        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);

        TextView monthlyBudget = (EditText) findViewById(R.id.monthlyBudget);
        monthlyBudget.setHint("Set the budget for " + monthNames[month]);
        monthlyBudget.setFilters(new InputFilter[] { new DecimalDigits(9, 2) });
        monthlyBudget.addTextChangedListener(new GenericTextWatcher(
                monthlyBudget));

        TextView dailyBudgetView = (TextView) findViewById(R.id.dailyBudget);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.budget_settings, menu);
        return true;
    }

    public void setDailyBudget(double amount) {

        dailyBudget = amount;

        TextView dailyBudgetView = (TextView) findViewById(R.id.dailyBudget);
        dailyBudgetView.setText(dailyBudget + "");
    }

}

这是GenericTextWatcher类

package nick.mirosh.budgetplanner;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

public class GenericTextWatcher implements TextWatcher {

    BudgetSettingsActivity activity = new BudgetSettingsActivity();

    private View view;

    public GenericTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1,
            int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void afterTextChanged(Editable editable) {

        String text = editable.toString();
        double dailyBudget = Double.parseDouble(text) / 30.0;

        switch (view.getId()) {
        case R.id.monthlyBudget:

            activity.setDailyBudget(dailyBudget);
        }
    }
}

这是我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".BudgetSettingsActivity" >

    <EditText
        android:id="@+id/monthlyBudget"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/dailyBudget"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这是log cat:

04-19 07:53:46.398: E/InputEventSender(3111): Exception dispatching finished signal.
04-19 07:53:46.398: E/MessageQueue-JNI(3111): Exception in MessageQueue callback: handleReceiveCallback
04-19 07:53:46.398: E/MessageQueue-JNI(3111): java.lang.NullPointerException
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.app.Activity.findViewById(Activity.java:1853)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at nick.mirosh.budgetplanner.BudgetSettingsActivity.setDailyBudget(BudgetSettingsActivity.java:48)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at nick.mirosh.budgetplanner.GenericTextWatcher.afterTextChanged(GenericTextWatcher.java:32)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.widget.TextView.sendAfterTextChanged(TextView.java:7334)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9087)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.widget.TextView.doKeyDown(TextView.java:5464)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.widget.TextView.onKeyDown(TextView.java:5283)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.KeyEvent.dispatch(KeyEvent.java:2623)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.View.dispatchKeyEvent(View.java:7343)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1933)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1408)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.app.Activity.dispatchKeyEvent(Activity.java:2384)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.os.MessageQueue.nativePollOnce(Native Method)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.os.MessageQueue.next(MessageQueue.java:132)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.os.Looper.loop(Looper.java:124)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at android.app.ActivityThread.main(ActivityThread.java:5103)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at java.lang.reflect.Method.invokeNative(Native Method)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at java.lang.reflect.Method.invoke(Method.java:525)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-19 07:53:46.398: E/MessageQueue-JNI(3111):   at dalvik.system.NativeStart.main(Native Method)
04-19 07:53:46.398: D/AndroidRuntime(3111): Shutting down VM
04-19 07:53:46.398: W/dalvikvm(3111): threadid=1: thread exiting with uncaught exception (group=0xb2ed7648)
04-19 07:53:46.398: E/AndroidRuntime(3111): FATAL EXCEPTION: main
04-19 07:53:46.398: E/AndroidRuntime(3111): java.lang.NullPointerException
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.app.Activity.findViewById(Activity.java:1853)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at nick.mirosh.budgetplanner.BudgetSettingsActivity.setDailyBudget(BudgetSettingsActivity.java:48)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at nick.mirosh.budgetplanner.GenericTextWatcher.afterTextChanged(GenericTextWatcher.java:32)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7334)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9087)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.widget.TextView.doKeyDown(TextView.java:5464)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.widget.TextView.onKeyDown(TextView.java:5283)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.KeyEvent.dispatch(KeyEvent.java:2623)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.View.dispatchKeyEvent(View.java:7343)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1393)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1933)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1408)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.app.Activity.dispatchKeyEvent(Activity.java:2384)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.os.MessageQueue.nativePollOnce(Native Method)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.os.MessageQueue.next(MessageQueue.java:132)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.os.Looper.loop(Looper.java:124)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at java.lang.reflect.Method.invoke(Method.java:525)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-19 07:53:46.398: E/AndroidRuntime(3111):     at dalvik.system.NativeStart.main
04-19 07:53:46.418: D/dalvikvm(3111): GC_FOR_ALLOC freed 252K, 13% free 2715K/3096K, paused 12ms, total 17ms

1 个答案:

答案 0 :(得分:0)

TextView monthlyBudget = (EditText) findViewById(R.id.monthlyBudget);

您正在将TextView转换为EditText。 将其更改为:

TextView monthlyBudget = (TextView) findViewById(R.id.monthlyBudget);

您还定义了

TextView dailyBudgetView = (TextView) findViewById(R.id.dailyBudget);两次。 一个在onCreate()中,另一个在setDailyBudget(double amount)中。删除第二个。它应该可以工作。