I am implementing a fragment in a navigation drawer menu. I want to read the content of the edit text on clicking a button and display the read content in a text view. But, I am getting the following error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View
Fragment class:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
editText = (EditText) getView().findViewById(R.id.id_editText);
button = (Button) getView().findViewById(R.id.id_button);
textView = (TextView) getView().findViewById(R.id.id_textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Name = editText.getText().toString();
textView.setText(Name);
}
});
return inflater.inflate(R.layout.fragment_chat, container, false);
}
Fragment xml
<RelativeLayout 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:background="#566D7E"
tools:context="com.example.jr.mybluetoothchat.ChatFragment">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter Text"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:id="@+id/id_editText" />
<Button
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/id_editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:id="@+id/id_button" />
<TextView
android:text="Entered Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/id_button"
android:layout_alignStart="@+id/id_editText"
android:layout_marginStart="43dp"
android:layout_marginTop="91dp"
android:id="@+id/id_textView" />
Content Main 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/id_content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BCC6CC"
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.jr.mybluetoothchat.MainActivity"
tools:showIn="@layout/app_bar_main">
</RelativeLayout>
Main Activity
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home)
{
Toast.makeText(this,"Home",Toast.LENGTH_SHORT).show();
} else if (id == R.id.nav_chat)
{
Toast.makeText(this,"Chat",Toast.LENGTH_SHORT).show();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ChatFragment chatFragment = new ChatFragment();
fragmentTransaction.replace(R.id.id_content_main,chatFragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_invite)
{
Toast.makeText(this,"Invite",Toast.LENGTH_SHORT).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}