How to start a class in the same. java file?

Asked

Viewed 71 times

0

I am new in this part of compilation for Android APP, I would like to know how to start a class q is in the same java file, for example: my 'Mainactivity', already declared the button that should start the class "Udp_client", but every time q press this button it gives error, someone can help me??

public class MainActivity extends AppCompatActivity {

public static final String MESSAGE_RECEIVED = "";
public String Payload = "2459247B0";
public String IP_Address = "192.168.2.255";
public String FeedbackUDP;
public String eltin;

public int port = 4212;

public boolean Server_Act = true;
public boolean turnCheck = false;

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main););
    Switch turn = findViewById(R.id.switch1);
    final TextView ip = findViewById(R.id.textView3);
    final TextView payload = findViewById(R.id.textView4);

    turnCheck = turn.isChecked();

    turn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(turnCheck = true){
                Payload = "2459247B1";
                payload.setText(Payload);
                ip.setText(eltin);
            }else{
                Payload = "2459247B0";
                payload.setText(Payload);
                ip.setText(eltin);
            }
            Intent intent = new Intent(MainActivity.this,UDP_Client.class);
            startActivity(intent);
            UDP_Client client = new UDP_Client();
            try {
                client.NachrichtSenden();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }

        }
    });

And the Udp_client class

class UDP_Client extends IOException {

MainActivity dir = new MainActivity();
private AsyncTask<Void, Void, Void> asyncTask;

@SuppressLint("NewApi")
public void NachrichtSenden() throws UnknownHostException {
    asyncTask = new AsyncTask<Void, Void, Void>() {

        MainActivity mainActivity = new MainActivity();
        byte[] ip = InetAddress.getByName(dir.IP_Address).getAddress();
        InetAddress IP = InetAddress.getByAddress(ip);

        @Override
        protected Void doInBackground(Void... voids) {
            try
            {
                DatagramSocket socket = new DatagramSocket(4212,IP);
                DatagramPacket packet;
                packet = new DatagramPacket(dir.Payload.getBytes(), dir.Payload.length(), IP, dir.port);
                socket.setBroadcast(true);
                socket.send(packet);
                dir.eltin = IP.getHostAddress();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                dir.eltin = "ERROR!";
                dir.Payload = "ERROR!";
            }
            return null;
        }
    };
}

}

And the mistake...

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.automatizze/com.example.automatizze.UDP_Client}: java.lang.IllegalAccessException: java.lang.Class is not accessible from java.lang.Class

Someone has some idea of what it is?

  • when you call Intent intent = new Intent(MainActivity.this,UDP_Client.class); startActivity(intent); the method takes as parameter a class that extends Appcompat... But in your case you are calling a class an asynctask and not an Activity, hence the error. By removing this part I put in, it may be possible to undo the error. But still I believe that will give error in this class of asynctask, it is a little confused.

  • It did not error, but did not start Udp_client :/ E agr?

  • I changed the code a little bit instead of initializing the class with UDP_Client client = new UDP_Client();&#xA; try {&#xA; client.NachrichtSenden();&#xA; } catch (UnknownHostException e) {&#xA; e.printStackTrace();&#xA; } To starting with: UDP_Client client = new UDP_Client();&#xA; client.asyncTask.execute();

  • If this asynctask is in the same Mainactivity java file, it removes every instance of Mainactivity in the Udp_client class. Since you have already stated the variables in the main class, you do not need to create an instance to manipulate it. That is, instead of using: dir.eltin, just use eltin. If it still doesn’t work, try debugging line/per line and check the returned values.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.