私はAndroidアプリを作成していますが、私はのsetText()Androidアプリのクラッシュは
にクラッシュを持っているが、これは私のjavaのです:
EditText eText;
TextView chat;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hitch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
chat = (TextView) findViewById(R.id.chat);
eText = (EditText) findViewById(R.id.edittext);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
chat.setText(eText.getText().toString());
}
});
}
、ここでXMLは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_hitch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.mohamad.hitch.Hitch"
tools:showIn="@layout/activity_hitch">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editText"
android:layout_marginBottom="21dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/edittext" />
<TextView
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/chat" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/input"
android:elevation="0dp"
android:layout_marginEnd="13dp"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentEnd="true" />
</RelativeLayout>
、ここではエラーです:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohamad.hitch, PID: 2415
java.lang.NullPointerException: Attempt to invoke virtualm ethod 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.mohamad.hitch.Hitch$1.onClick(Hitch.java:41)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
私は、他の多くの人々が同じ問題を抱えているのを見ました。
お願いします。
ヘルプがまだ必要な場合は、スタックトレースを確認し、ここに投稿してください。しかし、ほとんどの場合、これらのビューの1つ以上がactivity_hitch.xmlに存在しません – codeMagic
xmlで表示できますか、あなたの 'edittext'はどこですか? –