1

I have made a simple Android app which reads the CPU frequencies' (available frequencies, maximum and minimum frequencies) values from certain system files, adds them to 2 spinners, and writes the ones user selected (on those spinners) to other system files. Problem is, it always writes the maximum frequency (first spinner) value to minimum frequency (second spinner) file (or something similar, can't think of the exact problem). Here's the code:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    cmd CMD = new cmd();    //I use a class file to store functions and strings, whose values originate from the mentioned functions.
    Spinner spinner = (Spinner) findViewById(R.id.spinner); //maximum frequency.
    Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);   //minimum frequency.
    List<String> categories = new ArrayList<String>();
    ArrayAdapter<String> dataAdapterMAX = new ArrayAdapter<String>(this, simple_spinner_item, categories);
    ArrayAdapter<String> dataAdapterMIN = new ArrayAdapter<String>(this, simple_spinner_item, categories);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner.setOnItemSelectedListener(this);
        spinner2.setOnItemSelectedListener(this);

        for (int i = 0; i < CMD.getFreqs.length; i++)   //getting frequencies and writing them to a list.
        {
            categories.add (CMD.getFreqs[i]);
        }

        dataAdapterMAX.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapterMIN.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(dataAdapterMAX);
        spinner2.setAdapter(dataAdapterMIN);
        int maxFreqPos = dataAdapterMAX.getPosition(CMD.MaxFreq);   //selecting default (system provided) frequencies.
        int minFreqPos = dataAdapterMIN.getPosition(CMD.MinFreq);
        spinner.setSelection(maxFreqPos);
        spinner2.setSelection(minFreqPos);
    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String item = parent.getItemAtPosition(position).toString();
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, item, duration);
        toast.show();
        switch(parent.getId()) {
            case R.id.spinner:
                CMD.sudo("echo " + item + " > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");  //executing a bash command to select the frequency.
                break;
            case R.id.spinner2:
                CMD.sudo("echo " + item + " > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq");
                break;
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

Compiling such code yields no errors, however, the app instantly crashes whenever I try to open it. Here's the debugging log (errors only): 05-03 18:15:29.115 21558-21558/xxx E/AndroidRuntime: FATAL EXCEPTION: main

    Process: xxx, PID: 21558
                                                                                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{xxx/xxx.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2553)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
                                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5912)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                      at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
                                                                                      at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
                                                                                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
                                                                                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                                                                                      at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                                                                                      at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                                      at xxx.MainActivity.<init>(MainActivity.java:20)
                                                                                      at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                                      at java.lang.Class.newInstance(Class.java:1690)
                                                                                      at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2543)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771) 
                                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:135) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5912) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
05-03 18:16:19.915 21558-21558/xxx I/Process: Sending signal. PID: 21558 SIG: 9

Any help is appreciated.

  • The current Exception is due to the `findViewById()` call you have at the top of your class. However, you can't instantiate those `ArrayAdapter`s there, either. Move all of those initializations into `onCreate()`, after the `setContentView()` call. – Mike M. May 03 '17 at 23:27

1 Answers1

0

You have a nullpointerexeption when you init AppCompatDelegateImplBase class

developer_hatch
  • 15,898
  • 3
  • 42
  • 75