I am making an app on Android Studio and I ran into the NullPointerException in my onClick method. How do I fix it?
The debugger of my app gives me this:
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
at com.example.venttome.ventingPage.onClick(ventingPage.java:66)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
The code (the onClick() method) that is wrong:
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.record_btn:
if (isRecording){
//stop recording
recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_btn_stopped, null));
isRecording = false;
} else {
//start recording
recordButton.setImageDrawable(getResources().getDrawable(R.drawable.record_btn_recording, null));
isRecording = true;
}
break;
}
}
And this is my XML file for that class, and it is a constraint layout. This is what I use for my onClick method and I don't know what's wrong with it:
<ImageView
android:id="#+id/record_btn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginBottom="28dp"
android:background="#android:color/white"
android:clickable="true"
android:cropToPadding="true"
android:onClick="onClick"
android:src="#drawable/record_btn_stopped"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="#id/final_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputFeelings"
app:layout_constraintVertical_bias="1.0"
android:focusable="true"
tools:ignore="ContentDescription" />
you are not initializing the button in your method.Please first do this in your onCreate() method or if you are using fragment then in onCreateView():
ImageView record_btn= findViewById(R.id.record_btn);
Please initialize the view inside onCreate()
Here is the working code snippet
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
ImageView recordButton;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recordButton = findViewById(R.id.record_btn);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.record_btn:
Toast.makeText(this, "Called", Toast.LENGTH_SHORT).show();
recordButton.setImageResource(R.drawable.ic_launcher_foreground);
break;
}
}}
Related
I am beginner app dev trying to create chat app. The app starts smoothly but when I want to display all the registered users in a Recycler View app crashes . It doesnt show any error still the app crashes and when commented out the error just goes to the next line. Any help would be much appreciated.
Logcat
2020-11-23 10:38:39.697 4907-4907/com.example.decide_o E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.decide_o, PID: 4907
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.decide_o/com.example.decide_o.AllUsers}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.example.decide_o.AllUsers.onCreate(AllUsers.java:38)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
All Users
public class AllUsers extends AppCompatActivity {
private RecyclerView nuserlist;
private DatabaseReference nUserDatabase;
private LinearLayoutManager nlayoutmanager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users");
nlayoutmanager=new LinearLayoutManager(this);
nuserlist = (RecyclerView) findViewById(R.id.userlist);
nuserlist.setHasFixedSize(true);
nuserlist.setLayoutManager(nlayoutmanager);
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users,UsersViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_users,
UsersViewHolder.class,
nUserDatabase
) {
#Override
protected void populateViewHolder(UsersViewHolder usersViewHolder, Users users, int i) {
usersViewHolder.setName(users.getName());
}
};
nuserlist.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View nView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
nView=itemView;
}
public void setName(String name) {
TextView nameview=(TextView)nView.findViewById(R.id.single_username);
nameview.setText(name);
}
}
}
xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AllUsers">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/userlist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
the issue here is you haven't set your content view.
Add this line in you onCreate method :-
setContentView(R.layout.your_activity_xml);
after your super.oncreate is called.
If you don't assign your content view then it won't be identified that from where to fetch your recyclerView :- nuserlist = (RecyclerView) findViewById(R.id.userlist);.
I have learning about Fragment , when i try to change Fragment via clicks Button , and App is Force Stopped , its gives error like this
Error message in Logcat :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: mb.ganesh.fragment_practices, PID: 10451
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
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: Attempt to invoke virtual method 'androidx.fragment.app.FragmentTransaction androidx.fragment.app.FragmentManager.beginTransaction()' on a null object reference
at mb.ganesh.fragment_practices.MainActivity.changeFragemnet(MainActivity.java:29)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
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)
Actually i want to change Fragment via click Buttons , any Devs please fix this problem .
MainActivity:
package mb.ganesh.fragment_practices;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeFragemnet(View view){
Fragment fragment;
if (view == findViewById(R.id.b1)){
fragment = new FragmentOne();
FragmentManager fm = fragment.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.firstFM , fragment);
ft.commit();
}
}
}
The above code has one warning at FragmentTransaction ft = fm.beginTransaction();
activity_xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="changeFragemnet"
android:padding="20dp"
android:text="#string/fragment_1" />
<fragment
android:id="#+id/firstFM"
android:name="mb.ganesh.fragment_practices.FragmentOne"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" />
<Button
android:id="#+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:onClick="changeFragemnet"
android:text="#string/fragment_2" />
</LinearLayout>
In MainActivity the line:
FragmentManager fm = fragment.getFragmentManager();
Should be:
FragmentManager fm = getSupportFragmentManager();
Return the FragmentManager for interacting with fragments associated with this activity.
As you can read in the docs
Return the FragmentManager for interacting with fragments associated with this fragment's activity. Note that this will be non-null slightly before getActivity(), during the time from when the fragment is placed in a FragmentTransaction until it is committed and attached to its activity.
And is returning null for the just created Fragment.
I'm getting this error when I click on the button that executes a method.
Process: com.example.myapplication, PID: 7757
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.RadioButton
at com.example.myapplication.MockTest.submitbtn(MockTest.java:66)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6304)
at android.view.View$PerformClick.run(View.java:24803)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6635)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
The method is called upon by android:onClick through xml upon clicking the button.
This is the method,
public void submitbtn(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.q1r1: if (checked)
score++;
break;
case R.id.q2r4: if (checked)
score++;
break;
case R.id.q3r4: if (checked)
score++;
break;
case R.id.q4r2: if (checked)
score++;
break;
}
}
this is the xml entry for the button,
<Button
android:id="#+id/submitbtn"
android:layout_width="100dp"
android:layout_height="65dp"
android:layout_marginStart="150dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="150dp"
android:text="Submit"
android:onClick="submitbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/q4r" />
I looked at the error and noticed that it says it's caused because radiobutton widget cannot be cast to button widget, is that right? if so how can I implement this. What I'm trying to do is simply check the radio button's current state(checked or not) once the button is clicked. My previous approach, which worked, was this exact method but called from a radiobutton. I had put an android:onClick on the radiobutton's xml file. But this somehow does not work.
Found the solution, apparently I need to use radiogroups instead of directly using radiobuttons, the only change I made is in the code, here it is,
public void submitbtn(View view)
{
RadioGroup q4r = findViewById(R.id.q4r);
//Checking which radio button is checked, if any, and then executing tasks
if(q1r.getCheckedRadioButtonId() == R.id.q1r1)
{
//execute tasks
}
else if(q1r.getCheckedRadioButtonId() == -1)
{
//Nothing
}
else
{
//execute tasks
}
}
I faced this errors when i click the button to display the ProgressDialog
03-08 14:29:00.169 8564-8564/com.mohammedmoaayed.test2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mohammedmoaayed.test2, PID: 8564
Theme: themes:{}
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ProgressDialog.setTitle(java.lang.CharSequence)' on a null object reference
at com.mohammedmoaayed.test2.MainActivity.buclick(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5471)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
and this is my java code
package com.mohammedmoaayed.test2;
import android.app.Activity;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Activity myac;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myac=this;
}
mythread t1;
ProgressDialog bardown;
public void buclick(View view) {
bardown.setTitle("Downloaing ");
bardown.setMessage("Download is >>>>");
bardown.setProgressStyle(bardown.STYLE_HORIZONTAL);
bardown.setProgress(0);
bardown.setMax(20);
bardown.show();
t1 = new mythread();
t1.start();
}
class mythread extends Thread {
public void run (){
while (bardown.getProgress()<20) {
myac.runOnUiThread(new Runnable() {
#Override
public void run() {
bardown.incrementProgressBy(1);
}
});
try {
Thread.sleep(1000);
} catch (Exception ex) {}
}
myac.runOnUiThread(new Runnable() {
#Override
public void run() {
bardown.dismiss();
}
});
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mohammedmoaayed.test2.MainActivity">
<Button
android:id="#+id/button2"
android:layout_width="395dp"
android:layout_height="587dp"
android:onClick="buclick"
android:text="Button"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
You haven't inialiase your ProgressDialog so Initialize your ProgressDialog "bardown"
Initialize in Activity
bardown = new ProgressDialog(this);// write this line
Initialize in Fragment
bardown = new ProgressDialog(getActivity());// write this line
for e.g.
public void buclick(View view) {
bardown = new ProgressDialog(this);// write this line
bardown.setTitle("Downloaing ");
bardown.setMessage("Download is >>>>");
bardown.setProgressStyle(bardown.STYLE_HORIZONTAL);
bardown.setProgress(0);
bardown.setMax(20);
bardown.show();
t1 = new mythread();
t1.start();
}
you are calling setTitle() on null object.you have to instantiate it first;
bardown = (ProgressBar)findViewById(R.id.bardown);
or if you creating it dynamically use this
bardown - new ProgressBar(this);
You haven't initialized you ProgressDialog. Initialize it before using it. Add below line before calling the setTitle method.
bardown = new ProgressDialog(MainActivity.this);
use this
initialize dialog instant
ProgressDialog dialog = new ProgressDialog(Activity_Map_Advance_Tracking.this);dialog.setMessage("Loading....");
dialog.show();
After strictly following the Facebook developer website in order to create Sign in button, I've created everything that needed. However, I was creating an project without fragment,as it's provided in documentation and it seems something went wrong. Here is my Activity
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The other part is definitely correct. I get such log:
Log:
04-18 02:12:11.348 2781-2781/com.example.vlad.sign2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vlad.sign2, PID: 2781
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vlad.sign2/com.example.vlad.sign2.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class com.facebook.login.widget.LoginButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.facebook.login.widget.LoginButton
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.vlad.sign2.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.vlad.sign2.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
at com.facebook.internal.Validate.sdkInitialized(Validate.java:145)
at com.facebook.AccessTokenTracker.<init>(AccessTokenTracker.java:55)
at com.facebook.login.widget.LoginButton$2.<init>(LoginButton.java:563)
at com.facebook.login.widget.LoginButton.configureButton(LoginButton.java:563)
at com.facebook.FacebookButtonBase.<init>(FacebookButtonBase.java:69)
at com.facebook.login.widget.LoginButton.<init>(LoginButton.java:201)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.vlad.sign2.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.vlad.sign2.MainActivityFragment"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/textView" />
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView" />
UPDATE:
After adding I get this error:
4-18 02:35:45.721 3412-3441/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #5
Process: com.example.vlad.sign2, PID: 3412
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
at com.facebook.internal.Utility.queryAppSettings(Utility.java:846)
at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:491)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
UPDATE2:String.xml`
<resources>
<string name="app_name">Sign1</string>
<string name="action_settings">Settings</string>
<string name="facebook_app_id">277693549XXXXXX</string>
And Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Try this ......
It solve your problem..........
setContentView(R.layout.activity_fblogin_main);
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.content_main);
All FacebookSdk initialized in OnResume() method or before setContentView(R.layout.content_main) in OnCreate method;