-1

HOw do you use a method in a different class from the one you're calling it from in Android Studio? I have the following main class:

package com.example.bluebus;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.SECONDS;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BeeperControl test = new BeeperControl();
        //test.test();
        //test.test();*/

    }
}

and in the same folder I have the following BeeperControl class:

import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Created by Arthur on 1/7/2017.
 */

public class BeeperControl extends AppCompatActivity {
        /*private final ScheduledExecutorService scheduler =
                Executors.newScheduledThreadPool(1);*/
        final TextView text = (TextView) findViewById(R.id.text);
    /*
        public void beepForAnHour() {
            final Runnable beeper = new Runnable() {
                public void run() {
                    text.setText("Hello!");
                }
            };
            final ScheduledFuture<?> beeperHandle =
                    scheduler.scheduleAtFixedRate(beeper, 0, 1, SECONDS);
            scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
        }*/
        public void test(){
            text.setText("Who are you?");
        }
}

When written as is, the app works fine. However, when I uncomment test.test(), the app keeps closing when I try to run it in an emulator. From what I've seen, it may be a problem with updating the manifest, but I'm not sure how to fix it. Any help will be appreciated!

achang2
  • 31
  • 6
  • 1
    What are you trying to achieve by this? and why are you calling `findViewById(R.id.text);` outside `onCreate` of `BeeperControl`?!! (this will give you a `NullPointerExeption` BTW) – Atef Hares Mar 30 '17 at 00:22
  • Bottom line, you never use `new` to instantiate an Activity. You'll need to use a LocalBroadcast, EventBus, or something similar. Take a look at both of the linked duplicates. – Daniel Nugent Mar 30 '17 at 00:57

1 Answers1

0

Have the method in the class return the value, your findbyid will return null as it is out of scope.

For example:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.SECONDS;

public class MainActivity extends AppCompatActivity {

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BeeperControl test = new BeeperControl();
        TextView text = (TextView) findViewById(R.id.text);
        text.setText(test.test());
        //test.test();*/

    }
}

Your Class:

import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Created by Arthur on 1/7/2017.
 */

public class BeeperControl extends AppCompatActivity {
        /*private final ScheduledExecutorService scheduler =
                Executors.newScheduledThreadPool(1);*/
        /* final TextView text = (TextView) findViewById(R.id.text);^/
    /*
        public void beepForAnHour() {
            final Runnable beeper = new Runnable() {
                public void run() {
                    text.setText("Hello!");
                }
            };
            final ScheduledFuture<?> beeperHandle =
                    scheduler.scheduleAtFixedRate(beeper, 0, 1, SECONDS);
            scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
        }*/
        public string test(){
            return ("Who are you?");
        }
}

This probably doesn't assist in your use of the class in whatever context it is used, if you pass text by reference to test() that may do it for you. R/ Prescott ...

Prescott Chartier
  • 1,519
  • 3
  • 17
  • 34
  • I think that isn't actually the error. Yes, the object is final but OP is using methods to change the text which doesn't change the final object. – Kurt Acosta Mar 30 '17 at 00:31
  • You would be correct, I revised my answer. As @ Atef Hares points out, the findbyid returns a null pointer. – Prescott Chartier Mar 30 '17 at 00:35
  • Yes, it would invoke a NullPointerException because BeeperControl has no layout wherein in MainActivity, the layout is specified through the setContentView() method. But there's still something wrong in your code. Change line of `text = test.test()` to `text.setText(test.test())`. The text variable is a TextView and the test() method returns a String. The types are completely different and would cause another exception. :) – Kurt Acosta Mar 30 '17 at 00:40
  • Opps ... fat fingers .... hate it when I do that .... – Prescott Chartier Mar 30 '17 at 00:42
  • I constantly switch between VB.NET and C# with Android, iOS and Windows ... it's not pretty sometimes :), constant facepalming Thanks for the assistance. – Prescott Chartier Mar 30 '17 at 00:53
  • Yes. I've recognized the code syntax and it looked like VB.NET. Anyway, it's an honest mistake. No worries and glad I could be of some help. :) – Kurt Acosta Mar 30 '17 at 00:54