我不能从另一个类运行方法

时间:2018-09-13 12:36:25

标签: android

我无法从方法中获取值。即使该方法返回了string,我在上面也遇到了错误。

有人可以帮忙吗?

这是我的代码:

public class BackgroundService extends Service {

    public Context context = this;
    public Handler handler = null;
    public static Runnable runnable = null;
    String obj;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();
        handler = new Handler();
        runnable = new Runnable() {
            public void run() {
                Backgroundfunction s=new Backgroundfunction();
                Log.d("Check",s.gettingskill());
                handler.postDelayed(runnable, 10000);
            }
        };

        handler.postDelayed(runnable, 15000);
    }

    @Override
    public void onDestroy() {
        /* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
        handler.removeCallbacks(runnable);
    }

    @Override
    public void onStart(Intent intent, int startid) {
         obj= intent.getStringExtra("obj");

        Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
    }
}

我要运行的课程:

public class Backgroundfunction extends AppCompatActivity {

    String obj="qGyankbxKg";

    ArrayList<Object> jobopskills = new ArrayList<>();
    ArrayList<Object> jobskills = new ArrayList<>();
    Integer testskillsoutof=4;
    Integer totalskillsmark=0;
    float totalskillpercent ;
    String curjobobj;
    ArrayList<Object> jobopdegree = new ArrayList<>();
    ArrayList<Object> jobsdegree = new ArrayList<>();
    Integer testdegreeoutof=4;
    Integer totaldegreesmark=0;
    float totaldegreepercent ;
    ArrayList<Object> suggestions = new ArrayList<>();
    ArrayList<Object> selected = new ArrayList<>();
    String alarm;
    String genderi,locationi,catog;
    Integer expi,agesi;

    /*--------*/Integer totalmarks=0;/*--------*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_backgroundfunction);
        catog = "It and programming";
        genderi = "male";
        expi=9;
        agesi=19;
        locationi="chennai";
        gettingskill();
    }

    public String gettingskill(){
          return "subash";
    }
}

我的错误:

09-13 17:58:45.209 4781-4781/com.parse.starter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parse.starter, PID: 4781
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
    at com.parse.starter.Backgroundfunction$1.done(Backgroundfunction.java:261)
    at com.parse.starter.Backgroundfunction$1.done(Backgroundfunction.java:91)
    at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

0 个答案:

没有答案