Okay, this is my first post to stack so please bear with me. I have searched and have tried many things. I think another set of eyes might see something I don't. I get the NPE when i call the getText() function. I have included my scaled down version of my code, but the same error is happening. I have even looked that I have the right View any help will be appreciated.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<Task> taskList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater2 = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View customView2 = inflater2.inflate(R.layout.task_add_operations,null);
final PopupWindow mPopupWindow2 = new PopupWindow(customView2,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT, true);
Button closeButton2 = (Button)customView2.findViewById(R.id.closeButton2);
closeButton2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
mPopupWindow2.dismiss();
}
});
Button addButton = (Button)customView2.findViewById(R.id.addButton);
addButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View customView2) {
EditText text = customView2.findViewById(R.id.editText);
//This is where my java.lang.NullPointerException pops
String title = text.getText().toString();
taskList = Task.addTask(taskList, title);
mPopupWindow2.dismiss();
}
});
mPopupWindow2.showAtLocation(findViewById(R.id.activityMain),
CENTER,0, 0);
}
});
}
task_add_operations.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:text="Add Task"
android:textSize="18sp" />
<EditText
android:id="@+id/editText"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="text"
android:text="Title" />
<Button
android:id="@+id/closeButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_below="@+id/editText"
android:layout_marginTop="28dp"
android:text="Cancel" />
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/closeButton2"
android:layout_alignBottom="@+id/closeButton2"
android:layout_alignEnd="@+id/editText"
android:layout_alignRight="@+id/editText"
android:text="Add Task" />
</RelativeLayout>