我的方法出错了。我已经实现了该方法,但它仍然显示错误

时间:2017-10-05 05:19:58

标签: android

这是我的Common.java。我在这里使用问题列表。我应该在这里实现我的getUserName()吗?我唯一的错误是getCurrentUser()。

package sonu.enigma.Common;

import java.util.ArrayList;
import java.util.List;

import sonu.enigma.Model.Question;

/**
 * Created by PAWAN on 04-10-2017.
 */

public class Common {
    public static String CategoryId;
    public static String CurrentUser;
    public static List<Question>questionList=new ArrayList<>();
}

My error is here in getUserName(). I have implemented the method in other class as User.java`。但仍然显示她的错误。任何人都可以帮我修复它吗?我应该再次写我的User.java吗?或者我班上有什么问题吗?提前谢谢。

package sonu.enigma;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import sonu.enigma.Common.Common;
import sonu.enigma.Model.QuestionScore;

public class Done extends AppCompatActivity {
    Button btnTryAgain;
    TextView txtResultScore, getTxtResultQuestion;
    ProgressBar progressBar;
    FirebaseDatabase database;
    DatabaseReference question_score;


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

        database = FirebaseDatabase.getInstance();
        question_score=database.getReference("Question_Score");
        txtResultScore=(TextView)findViewById(R.id.txtTotalScore);
        getTxtResultQuestion=(TextView)findViewById(R.id.txtTotalQuestion);
        progressBar=(ProgressBar)findViewById(R.id.doneProgressBar);
        btnTryAgain=(Button)findViewById(R.id.btnTryAgain);

        btnTryAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(Done.this, Home.class);
                startActivity(intent);
                finish();

            }
        });

        Bundle extra= getIntent().getExtras();
        if (extra!=null){
            int score=extra.getInt("SCORE");
            int totalQuestion=extra.getInt("TOTAL");
            int correctAnswer=extra.getInt("CORRECT");

            txtResultScore.setText(String.format("SCORE : %d", score));
            getTxtResultQuestion.setText(String.format("PASSED : %d / %d",correctAnswer, totalQuestion ));
            progressBar.setMax(totalQuestion);
            progressBar.setProgress(correctAnswer);

            question_score.child(String.format("%s_%s", Common.CurrentUser.getUserName(), Common.CategoryId))
                    .setValue(new QuestionScore(String.format("%s_%s", Common.CurrentUser.getUserName(), Common.CategoryId),Common.CurrentUser.getUserName(),String.valueOf(score)));

        }





    }
}
`**

`package sonu.enigma.Model;

/**
 * Created by PAWAN on 01-10-2017.
 */

public class User {
    private String userName;
    private String password;
    private String email;
    public User(){

    }
    public User(String userName, String password, String email){
        this.userName=userName;
        this.password=password;
        this.email =email;


    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
`**above is my User.java . **

getUserName方法

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个,

package sonu.enigma;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import sonu.enigma.Common.Common;
import sonu.enigma.Model.QuestionScore;

public class Done extends AppCompatActivity {
    Button btnTryAgain;
    TextView txtResultScore, getTxtResultQuestion;
    ProgressBar progressBar;
    FirebaseDatabase database;
    DatabaseReference question_score;
    User user_oject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_done);
        user_oject=new User();
        database = FirebaseDatabase.getInstance();
        question_score=database.getReference("Question_Score");
        txtResultScore=(TextView)findViewById(R.id.txtTotalScore);
        getTxtResultQuestion=(TextView)findViewById(R.id.txtTotalQuestion);
        progressBar=(ProgressBar)findViewById(R.id.doneProgressBar);
        btnTryAgain=(Button)findViewById(R.id.btnTryAgain);

        btnTryAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(Done.this, Home.class);
                startActivity(intent);
                finish();

            }
        });

        Bundle extra= getIntent().getExtras();
        if (extra!=null){
            int score=extra.getInt("SCORE");
            int totalQuestion=extra.getInt("TOTAL");
            int correctAnswer=extra.getInt("CORRECT");

            txtResultScore.setText(String.format("SCORE : %d", score));
            getTxtResultQuestion.setText(String.format("PASSED : %d / %d",correctAnswer, totalQuestion ));
            progressBar.setMax(totalQuestion);
            progressBar.setProgress(correctAnswer);

            question_score.child(String.format("%s_%s", user_oject.getUserName(), Common.CategoryId))
                    .setValue(new QuestionScore(String.format("%s_%s", user_oject.getUserName(), Common.CategoryId),user_oject.getUserName(),String.valueOf(score)));

        }

    }
}
`**

`package sonu.enigma.Model;

/**
 * Created by PAWAN on 01-10-2017.
 */

public class User {
    private String userName;
    private String password;
    private String email;
    public User(){

    }
    public User(String userName, String password, String email){
        this.userName=userName;
        this.password=password;
        this.email =email;


    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

答案 1 :(得分:0)

您收到错误是因为班级getUserName()中没有方法String。而不是

public static String CurrentUser;

使用:

public static User CurrentUser;

OR

public static DataSnapshot CurrentUser;