2012-04-22 9 views
3

「Androidがこのような混乱を起こすのはなぜですか?質問。私はちょっとここの小さな問題に圧倒されています。私はチャーを作ろうとしている。カウンタの編集:AndroidでのmTextEditorWatcherの問題

final TextWatcher mTextEditorWatcher = new TextWatcher() { 
     EditText edit_text = (EditText) findViewById(R.id.editText1); 
     TextView text_view = (EditText) findViewById(R.id.textView1); 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    }; 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     //This sets a textview to the current length 
     text_view.setText(String.valueOf(s.length())); 
    }; 

    //public void afterTextChanged1(Editable s) { 
    //}; 

    public void afterTextChanged(Editable arg0) { 
     // TODO Auto-generated method stub 

    }; 
}; 

そしてクラッシュします。これが私が実際にEditText/TextViewの主張について以前に尋ねていた理由です。私はこのコードで何が間違っているのですか?

LogCatログ:

04-23 00:57:46.029: E/AndroidRuntime(347): Uncaught handler: thread main exiting due to uncaught exception 
04-23 00:57:46.109: E/AndroidRuntime(347): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dtype.writer/com.dtype.writer.DTypeActivity}: java.lang.NullPointerException 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.os.Looper.loop(Looper.java:123) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.main(ActivityThread.java:4363) 
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.reflect.Method.invokeNative(Native Method) 
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.reflect.Method.invoke(Method.java:521) 
04-23 00:57:46.109: E/AndroidRuntime(347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-23 00:57:46.109: E/AndroidRuntime(347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-23 00:57:46.109: E/AndroidRuntime(347): at dalvik.system.NativeStart.main(Native Method) 
04-23 00:57:46.109: E/AndroidRuntime(347): Caused by: java.lang.NullPointerException 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.Activity.findViewById(Activity.java:1612) 
04-23 00:57:46.109: E/AndroidRuntime(347): at com.dtype.writer.DTypeActivity$1.<init>(DTypeActivity.java:29) 
04-23 00:57:46.109: E/AndroidRuntime(347): at com.dtype.writer.DTypeActivity.<init>(DTypeActivity.java:28) 
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.Class.newInstanceImpl(Native Method) 
04-23 00:57:46.109: E/AndroidRuntime(347): at java.lang.Class.newInstance(Class.java:1479) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
04-23 00:57:46.109: E/AndroidRuntime(347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409) 
04-23 00:57:46.109: E/AndroidRuntime(347): ... 11 more 
04-23 00:57:46.128: E/dalvikvm(347): Unable to open stack trace file '/data/anr/traces.txt': Permission denied 

この溶液をhereから借用とキャメロンKetchamはのEditTextにTextWatcherを設定するmEditText.addTextChangedListener(mTextEditorWatcher);を使用することを提案しました。それが私の問題の中核になるかもしれませんか?

2012/04/24更新:まだこの解決策は見つかりませんでした。

+0

エラーログ(ログcat出力)を送信してください。 – Heinrisch

+0

投稿をログで更新しました。 – wswld

+0

リソースを再構築してみてください。 'textView1'が見つからないようです。これはあなたのリソースファイルが同期していないことを意味する可能性があります。 –

答えて

2

このようにして作業する必要があります。

private TextWatcher mTextEditorWatcher; 
private EditText mEditText; 
private TextView mTextView; 

public void onCreate() { 
    mEditText = (EditText) findViewById(R.id.editText1); 
    mTextView = (EditText) findViewById(R.id.textView1); 
    mTextEditorWatcher = new TextWatcher() { 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    }; 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     //This sets a textview to the current length 
     mTextView.setText(String.valueOf(s.length())); 
    } 

    //public void afterTextChanged1(Editable s) { 
    //} 

    public void afterTextChanged(Editable arg0) { 
     // TODO Auto-generated method stub 

    } 
    }; 
    mEditText.addTextChangedListener(mTextEditorWatcher); 
} 
+0

提案したコードを部分的に 'OnCreate'に移動しました。まだ打ち上げに運がない。 – wswld

+0

エラーがメッセージに変更されましたか?重要な部分は 'findViewById'を移動して' onCreate'が呼び出される前に実行されないようにすることです。 – zapl

+0

実際にはい。 android.app.Activity.findViewById(Activity.java:1612) 'がログから消えました。残りのエラーは残ります。 – wswld

4

私はあなたが置くので、それがあると思う:

mTextView =(のEditText)findViewById(R.id.textView1)。

それでなければならない:

mTextView =(のTextView)findViewById(R.id.textView1)。