当上下文传递给一串非 activity 方法时,无法访问 activity 上下文

Unable to access activity context when the context is passed down a string of non activity methods

我正在将我的 activity 上下文从我的主 activity 传递到 OnClickListener,后者又将该上下文(或者我认为如此)传递给对话框生成器。 onClickListener 可以调用 activity 方法,但对话框构建器不能使用 getActivity()。可能有更好的方法来实现弹出 table 但我是 GUI 和面向对象编程的初学者。代码如下。

public class MainActivity extends AppCompatActivity {

    private MyClickListener myClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myClickListener = new MyClickListener(this);

        TextView textView = (TextView)findViewById(R.id.clicked_text_view);
        textView.setOnClickListener(myClickListener);
   }
}


public class MyClickListener implements View.OnClickListener {

    private MyDialogBuilder dialogBuilder;
    private AlertDialog dialog;
    private Context context;

    public MyClickListener(Context context) {
        this.context = context;
    }

    @Override
    public void onClick(View view) {
        //do some stuff and pass some stuff to dialog builder
        //create a dialog
        dialogBuilder = new MyDialogBuilder(context);
        dialog = dialogBuilder.buildDialog();
        dialog.show();
    }
}


    public class MyDialogBuilder {
    Context context;
    AlertDialog dialog;

    public MyDialogBuilder(Context context){
        this.context = context;
    }

    public AlertDialog buildDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        // inflate a layout and pass it to the dialog builder
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.popup_dialog, null));

        builder.setMessage(R.string.dialog_message)
                                .setTitle("pop up title");
        dialog = builder.create();

        return dialog;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.applicationwithpopup.MainActivity">

    <TextView android:id="@+id/clicked_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="72dp"
        android:layout_gravity="center"
        android:text="click me"/>
</LinearLayout>



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

<TextView
    android:id="@+id/dialog_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dialog layout"
    android:textSize="@dimen/text_size" />
</LinearLayout>

您需要在 Activity

中创建自己的方法
private void createDialog(Context context) {
    // Create our dialog here
    private MyDialogBuilder dialogBuilder;
    private AlertDialog dialog;

    dialogBuilder = new MyDialogBuilder(context);
    dialog = dialogBuilder.buildDialog();
    dialog.show();
}

然后在您的侦听器中调用此方法:

public class MyClickListener implements View.OnClickListener {
    @Override
    public void onClick(View view) {
        createDialog(getContext);
    }
}