gpt4 book ai didi

java - 如何将数据从 Activity 传递到 DialogFragment

转载 作者:行者123 更新时间:2023-12-05 00:10:01 25 4
gpt4 key购买 nike

我知道有一些关于如何将数据从 Activity 传递到对话框(即 Bundle 或 Intent)的示例。但是,我尝试过的一切都不起作用。我不断收到 NPE 和 "Unable to find explicit activity class" 错误。即使当我使用对话框构建 super 基本 Activity 时,它也不起作用。我必须在代码中输入什么才能使其正常工作?

主要 Activity :

public class MainActivity extends AppCompatActivity {

Button button;
String textIWantToSee;

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

button = (Button) findViewById(R.id.button);

textIWantToSee = "If this is the text I want to pass form this activity to the Fragment";

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});
}
}

对话框:

public class Dialog extends DialogFragment {

TextView textView;

@Override
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {

LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null);

textView = (TextView) v.findViewById(R.id.textView);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v);

return builder.create();
}
}

更新

主要 Activity :

public class MainActivity extends AppCompatActivity {

TextView tvIntent;
Button button;
String textIWantToSee;

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

tvIntent = (TextView) findViewById(R.id.tvIntent);
button = (Button) findViewById(R.id.button);

textIWantToSee = "If this is the text I want to pass form this activity to the Fragment";
tvIntent.setText(textIWantToSee);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String intent = String.valueOf(tvIntent);
Intent intentToDialog = new Intent(MainActivity.this, Dialog.class);
intentToDialog.putExtra("keyForIntent", intent);
startActivity(intentToDialog); //Here is the exception

}
});
}

}对话框:

public class Dialog extends DialogFragment {

TextView textView;
String intent;

@Override
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {

LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null);

textView = (TextView) v.findViewById(R.id.textView);
Intent intentFromDialog = new Intent(getActivity().getApplicationContext(),MainActivity.class);
intent = intentFromDialog.getStringExtra("keyForIntent");
textView.setText(intent);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v);

return builder.create();
}

E/AndroidRuntime: FATAL EXCEPTION: main
Process: nl.blogvandetoekomst.passdatafromactivitytodialog, PID: 2947
android.content.ActivityNotFoundException: Unable to find explicit activity class {nl.blogvandetoekomst.passdatafromactivitytodialog/nl.blogvandetoekomst.passdatafromactivitytodialog.Dialog}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at nl.blogvandetoekomst.passdatafromactivitytodialog.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.blogvandetoekomst.passdatafromactivitytodialog">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

最佳答案

这是将数据从 Activity 传递到 DialogFragment 的方式:

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button button;
String textIWantToSee;

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

button = (Button) findViewById(R.id.button);

textIWantToSee = "If this is the text I want to pass form this activity to the Fragment";

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Dialog dialogFragment = new Dialog();
Bundle bundle = new Bundle();
bundle.putString("TEXT",textIWantToSee);
dialogFragment.setArguments(bundle);
dialogFragment.show((MainActivity.this).getSupportFragmentManager(),"Image Dialog");

}
});
}
}

Dialog.java

public class Dialog extends DialogFragment {

TextView textView;

@Override
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {

LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null);

Bundle bundle = getArguments();
String imageLink = bundle.getString("TEXT","");

textView = (TextView) v.findViewById(R.id.textView);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v);

return builder.create();
}
}

关于java - 如何将数据从 Activity 传递到 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42042248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com