0
Good morning, I was doing a generic class to use Alertdialog, but I came across the following problem:
05-28 10:11:16.323 19359-19359/com.example.vitordalmolin.abllock E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vitordalmolin.abllock, PID: 19359
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at com.example.vitordalmolin.abllock.additional.SystemStatus.alertDialog(SystemStatus.java:40)
at com.example.vitordalmolin.abllock.activities.Main2Activity$2.onItemClick(Main2Activity.java:156)
at android.widget.AdapterView.performItemClick(AdapterView.java:346)
at android.widget.AbsListView.performItemClick(AbsListView.java:1577)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3970)
at android.widget.AbsListView.onTouchUp(AbsListView.java:6080)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:5839)
at android.view.View.dispatchTouchEvent(View.java:10018)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2833)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2504)
at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5771)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.widget.ScrollView.dispatchTouchEvent(ScrollView.java:733)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2519)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2869)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1861)
at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2830)
at android.view.View.dispatchPointerEvent(View.java:10253)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5523)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5359)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4804)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4857)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4823)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4964)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4831)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5021)
This is my method:
public void alertDialog(int title,int mensage, int textPositiveButton, int textNegativeButton, int textNeutralButton){
Resources res = getResources();
String localTitle = String.format(res.getString(title));
String localmensage = String.format(res.getString(mensage));
String localPositiveButton = "";
String localNegativeButton = "";
String localNeutralButton = "";
if(textPositiveButton != 0){
localPositiveButton = String.format(res.getString(textPositiveButton));
}
if(textNegativeButton != 0){
localNegativeButton = String.format(res.getString(textNegativeButton));
}
if(textNeutralButton != 0){
localNeutralButton = String.format(res.getString(textNeutralButton));
}
//Cria o gerador do AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.DialogStyle);
//define o titulo
builder.setTitle(localTitle);
//define a mensagem
builder.setMessage(localmensage);
//define um botão como positivo
builder.setPositiveButton(localPositiveButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
alertDialogOption(1);
}
});
//define um botão como negativo.
builder.setNegativeButton(localNegativeButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//Toast.makeText(BluetoothActivity.this, "negativo=" + arg1, Toast.LENGTH_SHORT).show();
alertDialogOption(0);
}
});
builder.setNeutralButton(localNeutralButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialogOption(-1);
}
});
//cria o AlertDialog
alerta = builder.create();
//Exibe
alerta.show();
}
And these are the libraries used:
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
Post the stacktrace of Exception also.
– StatelessDev
I believe it’s because the variable
res
has not been initialized, I have no way to test now to include as a response– Gaspar