相关疑难解决方法(0)

片段真的需要一个空的构造函数吗?

我有一个Fragment带有多个参数的构造函数.我的应用程序在开发期间工作正常,但在生产中我的用户有时会看到此崩溃:

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment 
make sure class name exists, is public, and has an empty constructor that is public
Run Code Online (Sandbox Code Playgroud)

我可以创建一个空的构造函数,因为这个错误消息建议,但这对我没有意义,因为那时我将不得不调用一个单独的方法来完成设置Fragment.

我很好奇为什么这种崩溃偶尔会发生.也许我使用ViewPager不正确?我Fragment自己实例化所有s并将它们保存在一个列表中Activity.我不使用FragmentManager事务,因为ViewPager我看到的例子并不需要它,并且一切似乎都在开发期间工作.

android android-fragments

252
推荐指数
3
解决办法
10万
查看次数

如何忽略"避免片段中的非默认构造函数"错误?

我在尝试生成已签名的APK时遇到此错误.问题是,我的应用程序能够在我的设备上正常运行和调试.我不知道为什么当我尝试生成APK时,出现了这个错误.如何忽略此错误并生成签名的APK?我正在使用Android Studio 1.2.1.1 for Mac.

android

25
推荐指数
3
解决办法
2万
查看次数

错误:此片段应提供默认构造函数(不带参数的公共构造函数)

我的课程扩展DialogFragment如下:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    EditText editDate;
    private Calendar dateTime = Calendar.getInstance();
    private SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMMM yyyy");

    public DatePickerFragment(EditText editText) {
        editDate = editText;
    }

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return …
Run Code Online (Sandbox Code Playgroud)

java android android-studio

15
推荐指数
2
解决办法
1万
查看次数

如何解决此片段"非默认构造函数"错误?

我继承了这个Android应用程序.当我尝试构建已签名的APK时,我收到一条错误消息Error:(31) Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]

它指向这段代码:

public class BasicInfoFragment extends BasePassportFragment implements LocationEditText.LocationEditTextListener {

    private User user;
    private Templates templates;

    public BasicInfoFragment() {
        super(false);
    }

    public BasicInfoFragment(User user) {
        super(false);
        this.user = user;
    }

    public BasicInfoFragment(User user, Templates templates) {
        super(false);
        this.user = user;
        this.templates = templates;
    }

   ...

}
Run Code Online (Sandbox Code Playgroud)

错误指向第二和第三的方法,public BasicInfoFragment(User user)public BasicInfoFragment(User user, Templates templates)让我困惑,为什么public BasicInfoFragment()是好的,但其他两个都没有?

android android-fragments

1
推荐指数
1
解决办法
2471
查看次数

标签 统计

android ×4

android-fragments ×2

android-studio ×1

java ×1