简体   繁体   中英

How to access another class function from another class (java)

I'm new to java and I'm currently testing it in android studio.

I'm trying to access function showMessage() in TEST class from TEST2 class.

There is no problem if showMessage() function is called within the same class but the code stopped if I tried to access it from another class.

Maybe there is something wrong with my logic or syntax, but I'm not entirely sure which one is it. I already tried to search for some solution but there is no answer that I can find to help with my situation.

Here is my TEST.java code

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class TEST extends AppCompatActivity {
    public TEST2 ab;
    Button button;

    private final String text ="Testing test test test testing test";

    public void showMessage(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setCancelable(false) // cancel with button only
                .show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        button = (Button) findViewById(R.id.buttontest);

        ab = new TEST2(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMessage(text);
            }
        });
    }

}

class TEST2 extends View{
    public TEST t = new TEST();

    public TEST2(Context context) {
        super(context);

    //there is error if I tried this 2 line code
    //onTest();     
    //t.showMessage("TESTING FROM TEST2 CLASS");

    }

    public void onTest(){
        t.showMessage("TESTING FROM TEST2 CLASS");
    }
}

and this is my activity_test.xml layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttontest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

You can do this by passing parameter context

public void showMessage(String message, Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setCancelable(false) // cancel with button only
                .show();
}

Alert Dialog is a UI element it will not run from another class if activity is changed. if activity is not change try this...

public void showMessage(final String message,final Context mContext) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setMessage(message)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    })
                    .setCancelable(false) // cancel with button only
                    .show();
        }
    });
}
public void onTest(){
        if (getContext() instanceof Test)
            ((Test)getContext()).showMessage("TESTING FROM TEST2 CLASS");
    }

Just pass the Context of your TEST2 class.

 public void showMessage(Context context, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    .............
    ......................
 }

TEST2:

public TEST2(Context context) {
    super(context);

    t.showMessage(context, "TESTING FROM TEST2 CLASS");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM