Make request friend on Facebook by my app - java

I am use Facebook Developers to make request add friends from my app
this the link : https://developers.facebook.com/docs/graph-api/reference/v3.2/user/friendrequests
And this My code:
private void evenHandler() {
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent i = new Intent(LoginActivity.this, SignUpActivity.class);
// startActivity(i);
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/friendrequests",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
}
});
}
but i have error
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:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
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.befamous.MainActivity.evenHandler(MainActivity.java:185)
at com.befamous.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6944) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

Related

Android app crashes when I run it on API 30 Android Virtual Machine

I am trying to make something that converts the text i write into EditText to speech when i press button01.
It works fine on the virtual machine with Android API 23, but it crashes and makes a NullPointerException and crashes on the virtual machine with Android API 30. Here's the code:
public MainActivity() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button01 = (Button) findViewById(R.id.button01);
final TextView textView1 = (TextView) findViewById(R.id.text1) ;
textView1.setText("\n");
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != ERROR) {
// 언어를 선택한다.
tts.setLanguage(Locale.ENGLISH);
} else {
textView1.setText("TTS 작업에 오류가 생기거나 지원되지 않는 언어입니다.");
tts.speak("TTS 작업에 오류가 생기거나 지원되지 않는 언어입니다.", TextToSpeech.STOPPED, null);
}
}
});
button01.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// editText에 있는 문장을 읽는다.
tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
// TTS 객체가 남아있다면 실행을 중지하고 메모리에서 제거한다.
if(tts != null){
tts.stop();
tts.shutdown();
tts = null;
}
}
}
When i view Logcat, it showes this:
2020-07-09 02:10:56.036 7700-7700/com.example.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication2, PID: 7700
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication2/com.example.myapplication2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.String, int, java.util.HashMap)' 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 'int android.speech.tts.TextToSpeech.speak(java.lang.String, int, java.util.HashMap)' on a null object reference
at com.example.myapplication2.MainActivity$1.onInit(MainActivity.java:42)
at android.speech.tts.TextToSpeech.dispatchOnInit(TextToSpeech.java:836)
at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:814)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:745)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:724)
at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:708)
at com.example.myapplication2.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:7995)
at android.app.Activity.performCreate(Activity.java:7979)
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) 
It's happening in the else part in onInit of your listener. When there was an error in initializing the TextToSpeech instance, onInit will be called with ERROR as input while tts is not yet initialized and is null.
It may happen when your device doesn't support text to speech or the engine is missing necessary data.
Don't call anything on tts in case of an error. (There was an error in initializing text to speech. So, how can it speak your text?!)
Apps targeting Android 11 that use text-to-speech should declare TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE in the queries elements of their manifest:
<queries>
...
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>

Android App crashes when I call onDismissListner

I am trying to update text after a Dialog is dismissed. I am using an OnDismissListner, because the code in my onTextClicked runs only when the textView is clicked, and not when the dialog is dismissed.
public class AddReminder extends AppCompatActivity
{
}
#Override
protected void onCreate(Bundle savedInstanceState) {
//oncreate code
}
public void onDateClick(View view) {
loadMap();
DialogFragment datefragment = new DatePickerFragment();
datefragment.show(getFragmentManager(), "DatePicker");
Dialog dialog = datefragment.getDialog();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
updateText();
}
});
}
public void updateText(){
TextView date = findViewById(R.id.tv_date);
String text =AddReminder.monthmap.get(DatePickerFragment.month)+" "+DatePickerFragment.day+", "+DatePickerFragment.year;
date.setText(text);
TextView time = findViewById(R.id.tv_time);
String tvdate = TimePickerFragment.hour + ":" + TimePickerFragment.min;
time.setText(tvdate);
}
When I to click the textView with the onDateClicked methodcode, the app crashes with the error:
5-21 21:40:06.121 19350-19350/com.zbot473gmail.reminders E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zbot473gmail.reminders, PID: 19350
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: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 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
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.app.Dialog.setOnDismissListener(android.content.DialogInterface$OnDismissListener)' on a null object reference
at com.zbot473gmail.reminders.AddReminder.onDateClick(AddReminder.java:80)
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: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)
 
Is there a better way to run code when the dialog is dismissed? I have tried this, but I really don't understand how to solve it.

How can i insert a string into editText.setText()

Hello i have a question that is the one which i put in title.
I have this example that i extract from my code
String text = "Test";
EditText editText = (EditText) findViewById(R.id.idField);
editText.setText(text);
And I can't convert int o charSequence
My objective is put a string into the editText.setText();
Here is my onCreate:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
try {
fileExist();
sessionTimeOut();
passwordValidation(getLocalPassword());
} catch (FileNotExistException e) {
setLoginActivity();
} catch (SessionTimeOutException e) {
String text="Sometext";
EditText editText = (EditText) findViewById(R.id.usernameField);
setLoginActivity(R.string.sessionOut);
editText.setText(text);
} catch (PasswordInvalidException e) {
setLoginActivity(R.string.criterios);
}
setWelcomeBackLayout();
}
The error is located where editText.setText(text); is located
There is my error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pt.edu.es_loule, PID: 3498
java.lang.RuntimeException: Unable to start activity ComponentInfo{pt.edu.es_loule/pt.edu.es_loule.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' 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.EditText.setText(java.lang.CharSequence)' on a null object reference
at pt.edu.es_loule.LoginActivity.onCreate(LoginActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
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) 
Check your layout xml file, you editText variable is null because your reference "idField" is incorrect.
Don't forget to call setContentView(R.layout.yourlayout) before you get the reference of your view.
UPDATE
You forgot to call to setContentView method
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_file);
...
String implements CharSequence so String.valueOf(aInt) does works as argument.

Attempt to invoke virtual method in onClickListener

everytime I'm trying to start my app it crashes immediately on this part of code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isOnline()) {
// textViewTemperature.setText(Data);
Toast.makeText(getApplicationContext(), "Cannot connect to network, data will be restore from file with last downloaded data...", Toast.LENGTH_LONG).show();
} else {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
weather = parser.getWeatherForLocation(editText.getText() + "");
runOnUiThread(new Runnable() {
#Override
public void run() {
editText.setText(weather.getCity() + ", " + weather.getCountry()); }
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
});
This is the full logcat from this error:
12-29 10:06:36.376 3524-3524/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.piotr.pogoda, PID: 3524
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.piotr.pogoda/com.example.piotr.pogoda.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.piotr.pogoda.MainActivity.onCreate(MainActivity.java:136)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
I can see the error but I don't get what does it mean that I'm trying to use a method on a null object reference. The button does not exist or what?
Initialize your button, either by findViewById(R.id.btn) or programatically button = new Button(getApplicationContext())
From your crash log, button view is null. May be your view initialization has some issues. Please check the code.
You need to initialize the button
Button btn = (Button) findViewById()

Android Studio, Google Maps Search Button is not working

i worte an Android Application with google Maps integration.
I found a tutorial in youtube and did it in that way like in the video to search a location with button click.
I have just simple plaintext and a button for search. The Button is set to the onclick method onSearch() you can find code bellow.
When i press on that button, the app is crashing. What is wrong in the Code??
Thank you in advance:
public void onSearch(View v){
//Textfield in google maps activity
EditText location_tf=(EditText) findViewById(R.id.TFadress);
//Put the written input into a string to work with
String location = location_tf.getText().toString();
//List for Geocoder
List<Address> addressList= null;
if(location!= null || !location.equals("")){
Geocoder geocoder = new Geocoder(this);
try {
addressList=geocoder.getFromLocationName(location,1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng= new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Searched Location"));
//MovetheCamera to the searched location with 18 zoom
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
}
}
Error Log:
08-18 13:25:36.182 2619-2619/com.group6.travlhoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.group6.travlhoe, PID: 2619
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5336)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
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 android.view.View$DeclaredOnClickListener.onClick(View.java:5331)
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24697) 
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 interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.group6.TakeOff.MapsActivity.onMapSearch(MapsActivity.java:84)
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:5331) 
at android.view.View.performClick(View.java:6256) 
at android.view.View$PerformClick.run(View.java:24697) 
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) 

Categories

Resources