K
K
Kirill Petrov2014-03-15 10:52:00
Java
Kirill Petrov, 2014-03-15 10:52:00

Nested class (not static) - why does Android crash?

Greetings. Learn Java by trial and error. Tell me why this construction causes an error in the application:
1. I want to put the class in a separate file and briefly described it like this:

public class Functions extends Activity{
  
  public class StatControl{
    public void add_biletzakonchen(int t2, int questFail) {
      SharedPreferences pref = getSharedPreferences("stat", MODE_PRIVATE);  //Тут происходит ошибка
      Editor ed = pref.edit();
        //...//
      ed.commit();
    }
  }
}

2. And by trial and error, I wrote such a construction so that eclipse would not highlight:
Functions.StatControl statControl = new Functions().new StatControl();
statControl.add_biletzakonchen(t2, questFail);

What is the right way to write it down?
PS SharedPreferences doesn't work inside a static class.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
one pavel, 2014-03-15
@Recosh

>How should I write it down?
do not create objects of derived classes from Activity through new, for this there is starAtctivity. Move the StatControl to a separate file and pass the Context into it, either in the constructor or directly into the method itself.

K
Kirill Petrov, 2014-03-15
@Recosh

Through context, the application also crashes.
Here is the log

03-15 14:14:00.292: E/AndroidRuntime(10082): FATAL EXCEPTION: main
03-15 14:14:00.292: E/AndroidRuntime(10082): java.lang.IllegalStateException: Could not execute method of the activity
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.view.View$1.onClick(View.java:3595)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.view.View.performClick(View.java:4088)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.view.View$PerformClick.run(View.java:16984)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.os.Handler.handleCallback(Handler.java:615)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.os.Looper.loop(Looper.java:137)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at dalvik.system.NativeStart.main(Native Method)
03-15 14:14:00.292: E/AndroidRuntime(10082): Caused by: java.lang.reflect.InvocationTargetException
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at java.lang.reflect.Method.invoke(Method.java:511)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at android.view.View$1.onClick(View.java:3590)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	... 11 more
03-15 14:14:00.292: E/AndroidRuntime(10082): Caused by: java.lang.NullPointerException
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at com.example.test3pdd61.Functions$StatControl.add_biletzakonchen(Functions.java:14)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at com.example.test3pdd61.TestingProcActivity.show_result(TestingProcActivity.java:512)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at com.example.test3pdd61.TestingProcActivity.push(TestingProcActivity.java:279)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	at com.example.test3pdd61.TestingProcActivity.onClick(TestingProcActivity.java:204)
03-15 14:14:00.292: E/AndroidRuntime(10082): 	... 14 more
03-15 14:14:01.662: I/Process(10082): Sending signal. PID: 10082 SIG: 9
03-15 14:14:01.772: E/Trace(10493): error opening trace file: No such file or directory (2)

O
one pavel, 2014-03-15
@onepavel

What do you think, with your "new" "Activity" will have the "Environment" of the system and a full "Context" inside such an activity object to call getSharedPreferences ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question