1

So I'm trying to make a Settings screen and it seemed to work until I realized that the preferences won't react to clicks. I tried using onPreferenceStartFragment, but there's a strange error I'm encountering with the line

boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) 

Here's the onPreferenceStartFragment:

public interface OnPreferenceStartFragment {
        boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);
        //Syntax error on token ";", { expected after this token

        if(pref == password) {
            LayoutInflater inflater = LayoutInflater.from(context);
            final View text = inflater.inflate(R.layout.changepassword, null);
            final EditText currentPassword = (EditText)text.findViewById(R.id.currentPassword);
            final EditText newPassword = (EditText)text.findViewById(R.id.newPassword);

            final AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setTitle("Change Your Password");
            alert.setView(text);
            alert.setPositiveButton("Change", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                        String stringData = currentPassword.getText().toString().trim();
                        String stringNew = newPassword.getText().toString().trim();
                        dataReturned = myFolder.getString("passwordKey", "");
                        if(dataReturned.equals(stringData)) {                       
                            String newData = newPassword.getText().toString().trim();
                            SharedPreferences.Editor editor = myFolder.edit();
                            editor.putString("passwordKey", newData);
                            editor.commit();
                            dataReturned  = myFolder.getString("passwordKey", "couldn't load data");
                            Toast.makeText(context, "Password changed", Toast.LENGTH_SHORT).show();
                            currentPassword.setText("");
                            newPassword.setText("");
                        }

                        else {
                            Toast.makeText(context, "Incorrect Password", Toast.LENGTH_LONG).show();
                            currentPassword.setText("");
                            newPassword.setText("");
                        }
                    }
                });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
                });
            alert.show();
            ;
        }
        if(pref == notification) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.device_access_secure)
            .setOngoing(true)
            .setContentTitle("Obstruct")
            .setContentText("Start Stealth Mode");

            Intent resultIntent = new Intent();

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addNextIntent(resultIntent);

            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);

            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(0, mBuilder.build());
        }
        return true;
    }

Everything seems to be right, but it's flummoxing me that it's not. Any Ideas?

user2759839
  • 305
  • 1
  • 3
  • 11

2 Answers2

0

I'm not all too familiar with Java interfaces, but I suspect that the compiler is expecting you to define a method called onPreferenceStartFragment. Certainly your code seems to indicate as much, given that the next statement after the line

boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);

is an if statement.

You may want to review your brackets and what you're trying to achieve by defining the onPreferenceStartFragment method as well as it's body in the interface. In fact, after a quick search, I'm not even certain you are allowed to define method bodied in interfaces.

Edit: For the sake of correctness, I'll add that default method bodies defined in interfaces are an available feature as of Java 8. However Android doesn't support Java 8 and therefore does not support method bodied defined in interfaces. See What is the "default" implementation of method defined in an Interface?

Community
  • 1
  • 1
TheIT
  • 11,919
  • 4
  • 64
  • 56
0
boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);

is a method, and the code that you have below it is what is executed upon calling this method. There should be not semicolon at the end of the line, and it should be an opening bracket that starts the method.

It should be:

boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref){

    if(pref == password) {
    ... <the rest of your code here>
    ...
    return true;
}
Randy
  • 1,068
  • 2
  • 14
  • 32