我对Android编程很陌生,现在遇到了我无法通过搜索解决的第一个问题。
我在尝试显示自定义alertdialog时遇到“Source not found”错误,该错误显示“源附件不包含文件DialogFragment.class的源。”
无论是否使用android支持库(然后是FragmentActivity和getSupportFragmentManager),我都会收到此错误(然后是Activity和getFragmentManager)。
下面是一个极端简化的代码,它仍会产生错误。在这种情况下,它只能通过android:onClick="startDialogbyButton"
MainActivity:
package com.example.testdialogue;
//import android.app.Activity;
//import android.app.DialogFragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Toast.makeText(
this , "Achtung:...lala" , Toast.LENGTH_SHORT).show();
}
public void startDialogbyButton(View view){
showDialogU();
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void showDialogU() {
DialogFragment newFragment = new PlInputDialog();
newFragment.show(getSupportFragmentManager(), "dialog");
}
}
和PlInputDialog:
package com.example.testdialogue;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
public class PlInputDialog extends DialogFragment {
public PlInputDialog(){
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//int title = getArguments().getInt("title");
AlertDialog alertdialog = null;
LayoutInflater inflater = getActivity().getLayoutInflater();
final View customView = inflater.inflate(R.layout.dialog_player_input, null);
AlertDialog.Builder PlInDia = new AlertDialog.Builder(getActivity())
.setView(customView)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertdialog = PlInDia.create();
return alertdialog;
}
}
我可以想象问题不是代码本身,而是我添加库时的错误。但是考虑到当我不使用支持库时也会发生这种情况,我不知道只使用普通的android库我会在哪里犯错。
提前致谢。
答案 0 :(得分:0)
您使用错误的导入支持包
import android.app.Fragment;
你应该使用
import android.support.v4.app.Fragment;
以及getFragmentManager()
使用getSupportFragmentManager()
代替