我是android编程的新手。我想创建一个自定义的进度对话框与一些文本视图和按钮,并显示进度两个progressBar和更新他们发送文件从Asynctask,也希望它与最小的API 10的作品。谷歌文档推荐我使用DialogFragment和我没有任何想法如何更新进度条和textviews是在我fragmentDialog的是自定义布局,当我尝试引用一个TextView或进度条扔空exeption带对话框的自定义进度条对话框框架
这里是我的代码
public static class FireMissilesDialogFragment extends DialogFragment {
public FireMissilesDialogFragment(){
}
public static FireMissilesDialogFragment newInstance(String title) {
FireMissilesDialogFragment frag = new FireMissilesDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = this.getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View view = inflater.inflate(R.layout.custom_progress, null);
ProgressBar pbCurrent = (ProgressBar) view.findViewById(R.id.current);
builder.setView(view);
builder.setMessage("Fire Missiles")
.setPositiveButton("Fire", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
I在我的主要活动有一个nullExeption这里的时候,尝试引用视图
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button frag = (Button) findViewById(R.id.frag);
frag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FireMissilesDialogFragment fragment = FireMissilesDialogFragment.newInstance("hi") ;
fragment.getActivity().findViewById(R.id.current);// nullExeption here
// downloadAsync as = new downloadAsync();
// as.execute();
}
});
我没有看到太多的问题,比如这件事,我才走一路错了,应该选择另一种方式来实现我的观点? ?!!
编辑: 我想提前创建类似this
感谢
进度属于到片段。如果你在活动中初始化它,你最终会陷入nullpointerexception。你可以在片段本身调用asynctask – Raghunandan
我真的很陌生的android编程。你能帮我吗?我该怎么办?如果它包含示例代码,我将不胜感激...... – Pedram
阅读关于片段和asynctask的文档。 – Raghunandan