错误“UserInfo没有默认构造函数”

时间:2017-07-09 09:14:30

标签: java android

我的应用程序出现问题。错误说“UserInfo没有默认构造函数” 这是我在UserInfo中的Java代码。

package com.example.christian.loginapp2;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class UserInfo extends Activity {
    SharedPreferences.Editor edit;
    SharedPreferences pref;

    private static final String PREF_NAME = "userinfo";
    private static final String KEY_UNAME = "uname";
    private static final String KEY_PASS = "pass";
    private static final String KEY_FNAME = "fname";
    private static final String KEY_MNAME = "mname";
    private static final String KEY_LNAME = "lname";
    private static final String KEY_BDATE = "bdate";
    private static final String KEY_CONTACT = "contact";

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

        final TextView tvUsername = (TextView) findViewById(R.id.tvUsername);
        final TextView tvPassword = (TextView) findViewById(R.id.tvPassword);
        final TextView tvFirstname = (TextView) findViewById(R.id.tvFirstname);
        final TextView tvMiddlename = (TextView) findViewById(R.id.tvMiddlename);
        final TextView tvLastname = (TextView) findViewById(R.id.tvLastname);
        final TextView tvBirthdate = (TextView) findViewById(R.id.tvBirthdate);
        final TextView tvContact = (TextView) findViewById(R.id.tvContact);

        String uname = pref.getString(KEY_UNAME, "");
        String pass = pref.getString(KEY_PASS, "");
        String fname = pref.getString(KEY_FNAME, "");
        String mname = pref.getString(KEY_MNAME, "");
        String lname = pref.getString(KEY_LNAME, "");
        String bdate = pref.getString(KEY_BDATE, "");
        String contact = pref.getString(KEY_CONTACT, "");

        tvUsername.setText(uname);
        tvPassword.setText(pass);
        tvFirstname.setText(fname);
        tvMiddlename.setText(mname);
        tvLastname.setText(lname);
        tvBirthdate.setText(bdate);
        tvContact.setText(contact);

    }

    public void cacheUserInfo(String uname, String pass, String fname, String mname, String lname, String bdate, String contact) {
        edit.putString(KEY_UNAME, uname);
        edit.putString(KEY_PASS, pass);
        edit.putString(KEY_FNAME, fname);
        edit.putString(KEY_MNAME, mname);
        edit.putString(KEY_LNAME, lname);
        edit.putString(KEY_BDATE, bdate);
        edit.putString(KEY_CONTACT, contact);
        edit.commit();
    }

    public UserInfo(Context c){
        pref = c.getSharedPreferences(PREF_NAME, 0);
        edit = pref.edit();
        edit.apply();
    }
}

我是Android Studio和Java的初学者。

1 个答案:

答案 0 :(得分:0)

请删除此构造函数:

public UserInfo(Context c){
    pref = c.getSharedPreferences(PREF_NAME, 0);
    edit = pref.edit();
    edit.apply();
}

并在onCreate()方法中移动此行(这是唯一需要的行,因为您只是在阅读,而不是编辑首选项):

pref = c.getSharedPreferences(PREF_NAME, 0);