2017-10-05 145 views
-4

這是我的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

+0

您正在使用getUserName像Common.CurrentUser.getUserName(),但正確的做法是user_object.getUserName(); –

+0

所以我應該寫什麼呢? – sonu

+0

發佈您的Common.java文件代碼 – sasikumar

回答

0

你所得到的錯誤,因爲在該類String沒有方法getUserName()。取而代之的

public static String CurrentUser; 

用途:

public static User CurrentUser; 

OR

public static DataSnapshot CurrentUser; 
+0

沒有任何變化。仍然是同樣的錯誤。 – sonu

+0

使用'public static User CurrentUser;' –

+0

謝謝。它爲我工作 – sonu

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; 
    } 
} 
+0

無法正常工作。 #pravin先生 – sonu

+0

同樣的問題越來越多? –

+0

錯誤:(60,90)錯誤:無法找到符號方法getUserName()....我得到這個錯誤先生。當我將Common.categoryUser更改爲user_object。它仍顯示錯誤 – sonu

相關問題