-2
のTextViewを追加しようとしているが、それはのsetText()ラインにクラッシュ続ける:私はコード内で動的のTextViewウィジェットを追加しようとしています動的
はTextView tv = new TextView(MainActivity.this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setText("YOU: " + ET_message.getText().toString()); //this line crashes
ll = (LinearLayout) findViewById(R.id.LL);
ll.addView(tv);
は、誰もがなぜ知っているのですか?それはここで最も簡単な線であるはずです。
EDIT:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.guy_sh.messenger.MainActivity.onClick(MainActivity.java:30)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
EDIT#2:コードの変更
if(!TextUtils.isEmpty(ET_message.getText())) {
TextView tv = new TextView(MainActivity.this);
tv.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setText("YOU: " + ET_message.getText().toString());
ll = (LinearLayout) findViewById(R.id.LL);
ll.addView(tv);
}
また、 'ET_message'は' Crash Log'の投稿です –
ET_messsageはテキストを取得するEditTextです。私はクラッシュログを取得する方法を知りません。 –
あなたのlogcatをチェックしてください...あなたのET_messageがヌルかもしれませんか? – Yoni