如何从另一个类(java)访问另一个类函数

时间:2021-07-08 07:17:29

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

我是java的新手,我目前正在android studio中测试它。

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

我正在尝试从TEST2类访问TEST类中的函数showMessage()。

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.

如果在同一个类中调用showMessage()函数没有问题,但是如果我试图从另一个类访问它,代码就会停止。

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

这是我的TEST.java代码

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

这是我的activity_test.xml布局

<?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>

4 个解决方案

#1


0  

public void onTest(){
        if (getContext() instanceof Test)
            ((Test)getContext()).showMessage("TESTING FROM TEST2 CLASS");
    }

#2


1  

You can do this by passing parameter context

您可以通过传递参数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();
}

#3


1  

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

Alert Dialog是一个UI元素,如果更改了活动,它将不会从另一个类运行。如果活动不变,试试这个...

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();
        }
    });
}

#4


0  

Just pass the Context of your TEST2 class.

只需传递TEST2类的Context。

 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");
}

#1


0  

public void onTest(){
        if (getContext() instanceof Test)
            ((Test)getContext()).showMessage("TESTING FROM TEST2 CLASS");
    }

#2


1  

You can do this by passing parameter context

您可以通过传递参数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();
}

#3


1  

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

Alert Dialog是一个UI元素,如果更改了活动,它将不会从另一个类运行。如果活动不变,试试这个...

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();
        }
    });
}

#4


0  

Just pass the Context of your TEST2 class.

只需传递TEST2类的Context。

 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");
}