Reputation: 9285
I have this piece of code in my app:
FloatingActionButton addBlogButton = (FloatingActionButton) findViewById(R.id.addBlog);
addBlogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog addBlog = new Dialog(BlogLister.this);
addBlog.setContentView(R.layout.dialog_addblog);
addBlog.setTitle("Enter blog details : ");
blogTitle = (EditText) findViewById(R.id.blogTitle);
blogURL = (EditText) findViewById(R.id.blogURL);
addIt = (Button) findViewById(R.id.addIt);
cancelButton = (Button) findViewById(R.id.cancel);
addBlog.show();
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addBlog.dismiss();
}
});
}
});
However, when I click the addBlogButton
in my app, it closes my activity instead of showing the dialog.
But when I remove:
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addBlog.dismiss();
}
});
from my code, it works properly.
LOGCAT error:
03-13 19:17:40.048 27760-27760/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.innovapp.blogreadr, PID: 27760
java.lang.NullPointerException
at com.innovapp.blogreadr.BlogLister$1.onClick(BlogLister.java:37)
at android.view.View.performClick(View.java:4444)
at android.view.View$PerformClick.run(View.java:18440)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5052)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Can anyone tell me what the issue is and how to fix it?
EDIT what I mean by closes is that is works as how the android BACK button should, it takes me back to the previous activity
Upvotes: 0
Views: 81
Reputation: 7050
Change:
cancelButton = (Button) findViewById(R.id.cancel);
to:
cancelButton = (Button) addBlog.findViewById(R.id.cancel);
You are calling findViewById(int id)
on your activity's root view. It is returning null as you don't have any view with this id. To make it non-null, give it a reference and call Dialog.findViewById()
Upvotes: 2
Reputation: 151
Can anyone tell me what the issue is and how to fix it?
It is difficult to tell what is causing the behaviour from the provided code snippet so the answer is "probably no".
Update:
As @Malwinder mentioned you should look for the Button
with ID of "cancel" in the Dialog
and not in the "hosting" Activity
.
Since your Dialog
instance seems to have a clearly defined purpose(add a blog entry ?) I suggest you move that code into a separate class so it is easier to manage and debug. The DialogFragment
class seems to be the best option for what you are trying to achieve. Have a read on the API Guide for DialogFragment.
Upvotes: 0