简体   繁体   中英

How initialize correctly static variable in PreferenceActivity

I have Preference class extent PreferenceActivity. I create public static String quality; in Preference.class i add in onCreate

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref);
  quality = "QUALITY_HIGH";//initialize
    }

and add in Preference.class this method

public void getQuality() {
    if (keyquality.equals("480p")) {
        quality = "QUALITY_LOW";
        //

    }
    if (keyquality.equals("720p")) {
        //
       quality = "QUALITY_720P";
    }
    if (keyquality.equals("1080p")) {
        //
        quality = "QUALITY_HIGH";
    }

}

in another class i create method to get my variable and set settings

 private void getqualityvideo() {
    /*if (Prefernce.quality == null) {
        preferencecamrecoder = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    } else {*/
        if (Prefernce.quality.equals("QUALITY_LOW")) {
            preferencecamrecoder = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
        }
        if (Prefernce.quality.equals("QUALITY_720P")) {
            preferencecamrecoder = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
        }
        if (Prefernce.quality.equals("QUALITY_HIGH")) {
            preferencecamrecoder = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        }
   // }
}

Problem: when start application

 private void startServes() {

    btnStart = (ImageView) findViewById(R.id.StartService);
    btnStart.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            v.startAnimation(mAnimationImage);

            Intent intent = new Intent(MainActivity.this, RecorderService.class);

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startService(intent);
            changeCamera
                    .setEnabled(false);
            btnStart.setEnabled(false);
            setings.setEnabled(false);

            moveTaskToBack(false);
        }
    });

}

in another class in method getqualityvideo() error NullPointerException error in this first line if (Prefernce.quality.equals("QUALITY_LOW")) why the quality variable is empty?

The reason is that you're setting Preference.quality in the onCreate method in your Preference class. So what's probably happening is that when you start your application in your other class, Preference.quality is going to be null because it was never initialized to anything. The reason is that the other class has no way to access the onCreate method in your Preference class as of now. onCreate is executed when an activity starts, but that doesn't seem to happen anywhere in your code. A solution could be to initialize public static String quality outside of your onCreate method but still within the Preference class ,

public static String quality = "QUALITY_HIGH";

@Override
public void onCreate(Bundle savedInstanceState) {
    //insert code here
}

The problem was merely a scope issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM