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

如何从另一个类(java)访问另一个类函数,java,android,android-studio,Java,Android,Android Studio,我是java新手,目前正在android studio中测试它 我正在尝试从TEST2类访问TEST类中的函数showMessage() 如果在同一个类中调用了showMessage()函数,则没有问题,但是如果我试图从另一个类访问它,代码就会停止 也许我的逻辑或语法有问题,但我不完全确定是哪一个。我已经试着寻找一些解决办法,但我找不到有助于解决我的情况的答案 这是我的TEST.java代码 import android.app.AlertDialog; import android.conte

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

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

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

也许我的逻辑或语法有问题,但我不完全确定是哪一个。我已经试着寻找一些解决办法,但我找不到有助于解决我的情况的答案

这是我的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");
    }
}
这是我的活动_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>

您可以通过传递参数上下文来实现这一点

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

警报对话框是一个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();
        }
    });
}

只需传递
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");
}

您需要在ParametersHanks中输入上下文!在参数中传递上下文。:)“如果活动没有改变,试试这个……”OP明确表示,他想从另一个角度来称呼它activity@AwaisMajeed他说他是从另一个班打来的。。不管怎样,如果是活动,那么他需要传递上下文。对不起,你说的“如果活动没有改变”是什么意思?当我试图添加runOnUiThread时,getActivity()出现了一个错误,从技术上说,你是对的……但是OP是一个初学者,他以测试类的形式编写了他的第一个活动……所以普通感官
这个
而不是
getActivity()
。我在片段上尝试了您的代码,这就是为什么
这个
上下文
getActivity()
替换的原因。我尝试了您针对我遇到的其他问题的解决方案,它工作得非常完美。你的回答是我主要问题的最佳答案。
public TEST2(Context context) {
    super(context);

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