Firebase NullPointerException Error While have user id - java

intent = getIntent();
final String userid = intent.getStringExtra("userid");
I have Checked and verified that user id is coming but can not use in this child but before 2-3 days the same code working for me, from to now i have not changed anything
I have userid from Adapter position and i can use it in this activity and its working but in the given line of code error occur
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
that can not pass java.lang.NullPointerException:
Can't pass null for argument 'pathString' in child()
Error is given as below:
2020-02-27 21:46:53.567 3248-3248/com.example.chat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chat, PID: 3248
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chat/com.example.chat.MessageActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database##19.2.0:96)
at com.example.chat.MessageActivity.onCreate(MessageActivity.java:175)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Code part is given as below
fuser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
Last_seen.setText(user.getStatus());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

Related

Error- Attempt to invoke Virtual Method___________setOnClickListener(android.view.View$OnClickListener)' on a null object reference

I couldn't find anything that was not initialized, and can't work my way up further. The same bit works for another piece of code that I've written, both are totally identical, and I can't figure out the problem. The complete error code is -
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
The log file-
2021-07-30 21:29:33.613 17989-17989/com.elc.hostel E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elc.hostel, PID: 17989
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elc.hostel/com.elc.hostel.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.elc.hostel.LoginActivity.onCreate(LoginActivity.java:50)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2021-07-30 21:29:35.004 17989-18005/com.elc.hostel W/System: A resource failed to call close.
The code-
public class LoginActivity extends AppCompatActivity {
EditText mEmail, mPassword;
Button mLogin;
ProgressBar mProgressBar;
FirebaseAuth fAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Objects.requireNonNull(getSupportActionBar()).hide();
mEmail = findViewById(R.id.LoginEmail);
mPassword = findViewById(R.id.LoginPassword);
mLogin = findViewById(R.id.LoginButton);
mProgressBar = findViewById(R.id.progressBarLog);
fAuth = FirebaseAuth.getInstance();
mLogin.setOnClickListener(new View.OnClickListener() {------------------Error
#Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if (TextUtils.isEmpty(email)){
mEmail.setError("Email is required");
return;
}
if (TextUtils.isEmpty(password)){
mPassword.setError("Password is required");
return;
}
if (password.length() < 6){
mPassword.setError("Password must be at least 6 characters");
return;
}
mProgressBar.setVisibility(View.VISIBLE);
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.elc.hostel.LoginActivity.onCreate(LoginActivity.java:50)
Above stack indicates that mLogin is null.
Few cases in which findViewById() returns null are :
findViewById() is called before setContentView().
id passed in findViewById() is not present in layout set in setContentView().
In this case, setContentView() is not called.
Add setContentView() before accessing the views will solve the problem.

app crashes when starting an activity with final intent

I am programming an app that records phone calls and in this moment I am programming a menu and I want that when you click on an object in the menu you go to another activity; I tried to do this on both activities but when I click on a menu item the app crashes. Can you help me solve this crash? I leave you the code and the logcat of the crash
my code:
//menu
final Intent go = new Intent(this, records.class);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_start:
Toast.makeText(MainActivity.this, "Start", Toast.LENGTH_SHORT).show();
break;
case R.id.action_records:
Toast.makeText(MainActivity.this, "Records", Toast.LENGTH_SHORT).show();
startRec();
break;
}
return true;
}
private void startRec() {
startActivity(go);
}
});
//menu code end
crash logcat:
2021-01-12 16:30:09.637 5101-5101/com.example.ngoctri.introsliderexam E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ngoctri.introsliderexam, PID: 5101
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ngoctri.introsliderexam/com.conta.app.spyear.records}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.conta.app.spyear.records.<init>(records.java:13)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.conta.app.spyear.records.<init>(records.java:13)
The problem is in your records activity on row 13. You're calling findViewById() too early at instance init time. Move the findViewById() to onCreate() or later, after setContentView().

Problem saving data and transfering to another activity (using FireBase)

I'm having problems while attempting to save data input (from the user) via two EditText and transferring it to another activity where it should be displayed as a ViewText.
Code from activity/data input
public class DatosBasicos extends AppCompatActivity {
EditText presion;
EditText ritmo;
Button atriaje;
String valorritmo;
String valorpresion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_basicos);
atriaje = (Button) findViewById(R.id.triaj);
presion = (EditText) findViewById(R.id.presion);
ritmo = (EditText) findViewById(R.id.ritmo);
atriaje.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
valorritmo = ritmo.getText().toString();
valorpresion = presion.getText().toString();
Intent i = new Intent(DatosBasicos.this, Triaje.class);
i.putExtra("PRESION",valorpresion);
i.putExtra("RITMO",valorritmo);
startActivity(i);
finish();
}
});
}
}
Code from activity/data output
public class Triaje extends AppCompatActivity {
TextView rit, pres;
String ritS, presS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_triaje);
rit = (TextView) findViewById(R.id.ritmoval);
pres = (TextView) findViewById(R.id.presionval);
ritS = getIntent().getExtras().getString("PRESION");
presS = getIntent().getExtras().getString("RITMO");
rit.setText(ritS);
pres.setText(presS);
}
}
ERROR LOG
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aplicaciontriaje, PID: 2569
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplicaciontriaje/com.example.aplicaciontriaje.Triaje}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
at com.example.aplicaciontriaje.Triaje.onCreate(Triaje.java:19)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Disconnected from the target VM, address: 'localhost:8619', transport: 'socket'
WHAT SHOULD THE APP DO
I expect to get my data input into my Triaje activity and displayed in the two corresponding ViewText.
I get the following error: "App keeps stopping"
Is it possible to debug the system ? Then you clearly understand exactly where the it gives you error! Then tell exactly in which line it gives error!

Application was crashed in managedQuery line in android studio

I have coded an application that contains an list that present contact information.
Application code is as follow:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor mCursor=getContacts();
startManagingCursor(mCursor);
ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.two_line_list_item,
mCursor, new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME},
new int[]{android.R.id.text1, android.R.id.text2});
setListAdapter(adapter);
}
private Cursor getContacts(){
Uri uri= ContactsContract.Contacts.CONTENT_URI;
String[] projection=new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection=ContactsContract.Contacts.IN_VISIBLE_GROUP + "='" + ("1") + "'";
String[] selectionArgs = null;
String sortOrder=ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor out=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
return out;
}
}
but application was crashed at managedQuery line.
I add also READ_CONTACTS permission to the manifest file.
What is wrong?
Edited:
Here is logcat data:
--------- beginning of crash
08-18 17:36:45.093 6464-6464/com.example.morteza.listactivitycursor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.morteza.listactivitycursor, PID: 6464
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.morteza.listactivitycursor/com.example.morteza.listactivitycursor.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{afac265 6464:com.example.morteza.listactivitycursor/u0a98} (pid=6464, uid=10098) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{afac265 6464:com.example.morteza.listactivitycursor/u0a98} (pid=6464, uid=10098) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4181)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5970)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2592)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1828)
at android.content.ContentResolver.query(ContentResolver.java:786)
at android.content.ContentResolver.query(ContentResolver.java:752)
at android.content.ContentResolver.query(ContentResolver.java:710)
at com.example.morteza.listactivitycursor.MainActivity.getContacts(MainActivity.java:35)
at com.example.morteza.listactivitycursor.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:12185)
at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:12582)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:357)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3288)
at android.os.Binder.execTransact(Binder.java:731)
08-18 17:36:45.112 6464-6464/com.example.morteza.listactivitycursor I/Process: Sending signal. PID: 6464 SIG: 9
What is wrong?
You must get the user's permission. The manifest is not enough.
Check this: https://developer.android.com/training/permissions/requesting
Change to this:
private Cursor getContacts(){
Uri uri= ContactsContract.Contacts.CONTENT_URI;
String[] projection=new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection=ContactsContract.Contacts.IN_VISIBLE_GROUP + "= ?";
String[] selectionArgs = new String[] { "1" };
String sortOrder=ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor out=getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
return out;
}

Unable to set onClickListener [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
findViewById returns null
(4 answers)
Closed 4 years ago.
I'm trying to set the onClickListener on an ImageButton but I get the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bordi.hotel, PID: 18420
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bordi.hotel/com.example.bordi.hotel.DetaiImage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.bordi.hotel.DetaiImage.onCreate(DetaiImage.java:28)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
Below is the code I'm using:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
close = (ImageButton) findViewById(R.id.closeDetail);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DetaiImage.super.onBackPressed();
}
});
}
Thanks in advance for your help!
You forgot setContentView(R.layout.activity_main);
after super.onCreate(savedInstanceState);
and if you have not a prior global declaration in your activity of close, do it like this:
ImageButton close = (ImageButton) findViewById(R.id.closeDetail);

Categories