Java 如何将数据从活动发送到扩展对话框片段的自定义对话框
我是android studio的初学者。我陷入了无法将数据从活动传递到对话框的困境。我尝试了捆绑,但也无法正常工作,因为它显示空点异常。或者我猜我的方法是错误的。如果我能得到一些帮助,那就太好了 对话类Java 如何将数据从活动发送到扩展对话框片段的自定义对话框,java,android,Java,Android,我是android studio的初学者。我陷入了无法将数据从活动传递到对话框的困境。我尝试了捆绑,但也无法正常工作,因为它显示空点异常。或者我猜我的方法是错误的。如果我能得到一些帮助,那就太好了 对话类 public class Mdlog extends DialogFragment { LayoutInflater inflater; TextView sc1,sc2; @Override public Dialog onCreateDialog(Bundle savedInstanceS
public class Mdlog extends DialogFragment
{
LayoutInflater inflater;
TextView sc1,sc2;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.layout_dialog,null);
Intent i=getActivity().getIntent();
sc1= (TextView) view.findViewById(R.id.score1);
sc2= (TextView) view.findViewById(R.id.score2);
sc1.setText(i.getStringExtra("sc1"));
sc2.setText(i.getStringExtra("sc2"));
builder.setView(view);
AlertDialog dialog=builder.create();
return dialog;
}}
主要活动类
public class MainActivity extends AppCompatActivity {
int sc1=0;
int sc2=0;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t= (TextView) findViewById(R.id.timeleft);
new CountDownTimer(3000,1000)
{
@Override
public void onTick(long millisUntilFinished)
{
t.setText(""+millisUntilFinished/1000);
sc1+=1;
sc2+=2;
}
@Override
public void onFinish()
{
}
}.start();
Intent i=new Intent(this,Mdlog.class);
i.putExtra("sc1",sc1);
i.putExtra("sc2",sc2);
startActivity(i);
Mdlog dialog=new Mdlog();
dialog.show(getFragmentManager(),"sss");
}}
全部删除
Intent i=new Intent(this,Mdlog.class);
i.putExtra("sc1",sc1);
i.putExtra("sc2",sc2);
startActivity(i);
为参数设置一个bundle以传递数据,然后设置为片段
Bundle args = new Bundle();
args.putInt("sc1", sc1);
args.putInt("sc2", sc2);
Mdlog dialog=new Mdlog();
dialog.setArguments(args);
dialog.show(getFragmentManager(),"dialog");
并在与的对话框中获取数据
Bundle args = getArguments();
int sc1 = args.getInt("sc1");
int sc2 = args.getInt("sc2");
全部删除
Intent i=new Intent(this,Mdlog.class);
i.putExtra("sc1",sc1);
i.putExtra("sc2",sc2);
startActivity(i);
为参数设置一个bundle以传递数据,然后设置为片段
Bundle args = new Bundle();
args.putInt("sc1", sc1);
args.putInt("sc2", sc2);
Mdlog dialog=new Mdlog();
dialog.setArguments(args);
dialog.show(getFragmentManager(),"dialog");
并在与的对话框中获取数据
Bundle args = getArguments();
int sc1 = args.getInt("sc1");
int sc2 = args.getInt("sc2");
您应该使用静态方法创建片段。如果在对话框中键入newIsntance,Android Studio将生成一个newInstance静态方法。您可以将数据传递给该方法。例如:
public static DialogFragment newInstance(int value1, int value2) {
DownloadDialogFragment dialogFragment = new DownloadDialogFragment();
Bundle args = new Bundle();
args.putInt("args_value1", value1);
args.putInt("args_value2", value2);
dialogFragment.setArguments(args);
return dialogFragment;
}
您可以在onCreateDialogBundle savedInstanceState中获取数据,如下所示:
public Dialog onCreateDialog(Bundle savedInstanceState) {
int value1 = getArguments().getInt("args_value1");
int value2 = getArguments().getInt("args_value2");
}
您应该使用静态方法创建片段。如果在对话框中键入newIsntance,Android Studio将生成一个newInstance静态方法。您可以将数据传递给该方法。例如:
public static DialogFragment newInstance(int value1, int value2) {
DownloadDialogFragment dialogFragment = new DownloadDialogFragment();
Bundle args = new Bundle();
args.putInt("args_value1", value1);
args.putInt("args_value2", value2);
dialogFragment.setArguments(args);
return dialogFragment;
}
您可以在onCreateDialogBundle savedInstanceState中获取数据,如下所示:
public Dialog onCreateDialog(Bundle savedInstanceState) {
int value1 = getArguments().getInt("args_value1");
int value2 = getArguments().getInt("args_value2");
}
您可以共享日志吗?您可以共享日志吗?android.content.res.Resources$NotFoundException:字符串资源ID 0x2。此错误显示在哪一行@SAYAM.SSANGHVIsc1.setTextsc3;这行@AtaerCanersc3?我不明白s3从何而来,但如果它是一个int,则将其转换为类似于sc1.setTextString.valueOfs3;的字符串@SAYAM.SSANGHVIYup非常有用。谢谢你。android.content.res.Resources$NotFoundException:字符串资源ID 0x2。这个错误显示在哪一行@SAYAM.SSANGHVIsc1.setTextsc3;这行@AtaerCanersc3?我不明白s3从何而来,但如果它是一个int,则将其转换为类似于sc1.setTextString.valueOfs3;的字符串@SAYAM,SSANGHVIYup这很有帮助,谢谢你。