-2

While creating the android app in user profile when user click on password edit text the pop should be displayed I have implemented it but when i click on edit text It is giving me the below error. I have googled it but not found any useful solution. Please can you tell I am attaching the code :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:222) at android.app.AlertDialog$Builder.(AlertDialog.java:452) at com.example.admin.avisar.UserProfile$2.onClick(UserProfile.java:71) at android.view.View.performClick(View.java:5612) at android.view.View$PerformClick.run(View.java:22285) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 06-01 12:35:00.558 1563-5452/? E/ActivityManager: Sending non-protected broadcast com.motorola.motocare.INTENT_TRIGGER from system 4237:com.motorola.process.system/1000 pkg com.motorola.motgeofencesvc java.lang.Throwable at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:18226) at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:18826) at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:512) at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2906) at android.os.Binder.execTransact(Binder.java:565)

code here







    package com.example.admin.avisar;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.admin.avisar.network.CompletionListener;
import com.example.admin.avisar.network.NetworkTask;
import com.example.admin.avisar.network.Post_URL;
import com.example.admin.avisar.network.Responce;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserProfile extends AppCompatActivity implements CompletionListener{
    EditText name,mobno,email,add,accid,pass,yeared;
    Button svebtn;
    ProgressDialog progressDialog;
    private NetworkTask networkTask;
    Context context;
    String uid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("My Profile");
        actionBar.setDisplayHomeAsUpEnabled(true);

        name = (EditText)findViewById(R.id.name);
        mobno = (EditText)findViewById(R.id.mobno);
        email = (EditText)findViewById(R.id.email);
        add = (EditText)findViewById(R.id.add);
        accid = (EditText)findViewById(R.id.id);
        pass = (EditText)findViewById(R.id.pass);
        yeared = (EditText)findViewById(R.id.year);
        svebtn = (Button)findViewById(R.id.svebtn);
        userProfileWebService();
        svebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userProfileUpdate();
            }
        });

        pass.setOnClickListener(new View.OnClickListener() {
            String newpass="";
            String confirmpass="";
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alert = new AlertDialog.Builder(UserProfile.this);
                alert.setTitle("change password"); //Set Alert dialog title here
                final EditText newpassed = new EditText(context);
                newpassed.setHint("New password");
                alert.setView(newpassed);

                final EditText confirmpassed = new EditText(context);
                confirmpassed.setHint("Confirm password");
                alert.setView(confirmpassed);

                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        if(newpassed.length()==0){
                            newpassed.requestFocus();
                            newpassed.setError("FIELD CANNOT BE EMPTY");
                             newpass = confirmpassed.getEditableText().toString();
                        }else if (confirmpassed.length()==0) {
                            confirmpassed.requestFocus();
                            confirmpassed.setError("FIELD CANNOT BE EMPTY");
                             confirmpass = confirmpassed.getEditableText().toString();
                        }else if(confirmpass==newpass){
                            passwordwebservice(newpassed);

                        }else{
                            confirmpassed.setError("confirm password doesnot match");
                        }



                            // Toast.makeText(context, srt, Toast.LENGTH_LONG).show();

                    } // End of onClick(DialogInterface dialog, int whichButton)
                }); //End of alert.setPositiveButton
                alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                        dialog.cancel();
                    }
                }); //End of alert.setNegativeButton
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
            }
        });

    }

    private void passwordwebservice(EditText newpassed){
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(true);
        progressDialog.setProgress(0);
        progressDialog.show();
        progressDialog.setCanceledOnTouchOutside(false);
        networkTask = new NetworkTask(this,this,false);
        String newpass = newpassed.getEditableText().toString();
        List<NameValuePair> params =new ArrayList<>();
        params.add(new BasicNameValuePair("userid",uid));
        params.add(new BasicNameValuePair("password",newpass));
        params.add(new BasicNameValuePair("userid",uid));
        networkTask.execute(params, Post_URL.URL_update_password, 3);
    }
    //userprofile view webservice
    private void userProfileWebService(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(true);
        progressDialog.setProgress(0);
        progressDialog.show();
        progressDialog.setCanceledOnTouchOutside(false);
        networkTask = new NetworkTask(this,this,false);

        List<NameValuePair> params =getRequestParams();
        networkTask.execute(params, Post_URL.URL_Profile, 1);
    }
    //parameter passing for userprofile webservice
    private List<NameValuePair> getRequestParams() {

        SharedPreferences sharedPreferences = getSharedPreferences("A", Context.MODE_PRIVATE);
        uid = sharedPreferences.getString("user_id","abc");
        Log.e("TAG",uid);
        List<NameValuePair> param1 = new ArrayList<NameValuePair>();
        param1.add(new BasicNameValuePair("userid", uid));
        return param1;
    }

    private void userProfileUpdate(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(true);
        progressDialog.setProgress(0);
        progressDialog.show();
        progressDialog.setCanceledOnTouchOutside(false);
        networkTask = new NetworkTask(this,this,false);

        List<NameValuePair> params =getRequestParamsupdate();
        networkTask.execute(params, Post_URL.URL_update_profile, 2);
    }

private List<NameValuePair> getRequestParamsupdate(){
    String username = name.getText().toString();
    String mno = mobno.getText().toString();
    String mail = email.getText().toString();
    String address = add.getText().toString();
    String accout_id = accid.getText().toString();
  //  String  password= pass.getText().toString();
    String yearString = yeared.getText().toString();
    List<NameValuePair> pairs = new ArrayList<>();
    pairs.add(new BasicNameValuePair("username",username));
    pairs.add(new BasicNameValuePair("mobno",mno));
    pairs.add(new BasicNameValuePair("email",mail));
    pairs.add(new BasicNameValuePair("add",address));
    pairs.add(new BasicNameValuePair("userid",accout_id));
  //  pairs.add(new BasicNameValuePair("password",password));
    pairs.add(new BasicNameValuePair("year",yearString));
    return  pairs;
}
//user profile response handle
    private void handleUserProfileResponse(JSONObject serverResponse){
        int success=0;
        try{
            success = serverResponse.getInt(Responce.TAG_SUCCESS);
            if(success==1){
                progressDialog.dismiss();

                //parsing the data

                JSONArray user_profile = serverResponse.getJSONArray("user_details");
                for(int i =0 ;i<user_profile.length();i++){
                    String  name1 = user_profile.getJSONObject(i).getString("username");
                    String  mno = user_profile.getJSONObject(i).getString("mobno");
                    //Log.e("TAG",name1);
                    String  mail = user_profile.getJSONObject(i).getString("email");
                    String  address = user_profile.getJSONObject(i).getString("add");
                    String  accountid = user_profile.getJSONObject(i).getString("userid");
                    String  password = user_profile.getJSONObject(i).getString("password");
                    String  year = user_profile.getJSONObject(i).getString("year");
                    //set data to textview
                    name.setText(name1);
                    mobno.setText(mno);
                    email.setText(mail);
                    add.setText(address);
                    accid.setText(accountid);
                    pass.setText(password);
                    yeared.setText(year);

                }
            }
            else{
                progressDialog.dismiss();
                Toast.makeText(UserProfile.this, serverResponse.getString(Responce.TAG_MESSAGE), Toast.LENGTH_LONG).show();
            }

        }catch (JSONException e){

        }
    }

    private void handleUserProfileUpdateResponse(JSONObject serverResponse){
        int success =0;
        try{
            success = serverResponse.getInt(Responce.TAG_SUCCESS);
            if (success ==1){
                progressDialog.dismiss();
                Toast.makeText(UserProfile.this, serverResponse.getString(Responce.TAG_MESSAGE), Toast.LENGTH_LONG).show();
            }
            else{
                progressDialog.dismiss();
                Toast.makeText(UserProfile.this, serverResponse.getString(Responce.TAG_MESSAGE), Toast.LENGTH_LONG).show();
            }
        }catch(JSONException e)
        {

        }

    }
    private void handleUpdatePassResponse(JSONObject serverResponse){
        int success =0;
        try{
            success = serverResponse.getInt(Responce.TAG_SUCCESS);
            if (success ==1){
                progressDialog.dismiss();
                Toast.makeText(UserProfile.this, serverResponse.getString(Responce.TAG_MESSAGE), Toast.LENGTH_LONG).show();
            }
            else{
                progressDialog.dismiss();
                Toast.makeText(UserProfile.this, serverResponse.getString(Responce.TAG_MESSAGE), Toast.LENGTH_LONG).show();
            }
        }catch(JSONException e)
        {

        }
    }

    @Override
    public void onComplete(JSONObject serverResponse, int RESPONSE_IDENTIFIER_FLAG) throws JSONException {
        switch (RESPONSE_IDENTIFIER_FLAG){
            case 1:handleUserProfileResponse(serverResponse);
                break;
            case 2: handleUserProfileUpdateResponse(serverResponse);
                break;
            case 3:handleUpdatePassResponse(serverResponse);
            default:
                break;
        }

    }
}
pokales
  • 1
  • 1

1 Answers1

0

Pass activity instead of context in following line :

AlertDialog.Builder alert = new AlertDialog.Builder(context);
R.R.M
  • 780
  • 4
  • 10