2015-05-28 17 views
1

我的團隊正在嘗試創建聯繫人列表,哪些聯繫人可以添加,但他們正在收到以下錯誤:無法通過靜態上下文引用非靜態方法populatelist()。但是,我們不知道這是否會實際工作。任何幫助表示讚賞如何解決非靜態錯誤

public class Profile extends Activity { 

    ImageView contactImageImgView; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.profile); 

     Button editProfileButton = (Button) findViewById(R.id.BeditProfile); 
     Button addContactButton = (Button) findViewById(R.id.AdContactButton); 
     //Text ChangeProfilePictureText = (Text) findViewById(R.id.ChangeProfilePicture); 

     String Name = getIntent().getStringExtra("Name"); 
     String eMail = getIntent().getStringExtra("Mail"); 
     String Mobile = getIntent().getStringExtra("Mobile"); 
     String Adress = getIntent().getStringExtra("Adress"); 
     String Bio = getIntent().getStringExtra("Bio"); 


     contactImageImgView = (ImageView) findViewById(R.id.imgViewContactImage); 
     TextView tv_Name = (TextView) findViewById(R.id.Name); 
     TextView tv_Mail = (TextView) findViewById(R.id.Email); 
     TextView tv_Mobile = (TextView) findViewById(R.id.Mobile); 
     TextView tv_Adress = (TextView) findViewById(R.id.Adress); 
     TextView tv_Bio = (TextView) findViewById(R.id.Bio); 

     tv_Name.setText(Name); 
     tv_Mail.setText(eMail); 
     tv_Mobile.setText(Mobile); 
     tv_Adress.setText(Adress); 
     tv_Bio.setText(Bio); 

     if(Cube.fromUnit){ 
      editProfileButton.setVisibility(View.GONE); 
      //ChangeProfilePictureText.replaceWholeText(""); 
      tv_Name.setText(Cube.Name); 
      tv_Mail.setText(Cube.eMail); 
      tv_Mobile.setText(Cube.Mobile); 
      tv_Adress.setText(Cube.Adress); 
      tv_Bio.setText(Cube.Bio); 
     } 

     contactImageImgView.setOnClickListener(new View.OnClickListener() { 

      public void onClick (View v){ // error @ View v, cannot resolve symbol v , expected ; 
       Intent intent = new Intent(); 
       intent.setType("image*/"); 
       intent.setAction(intent.ACTION_GET_CONTENT); 
       startActivityForResult(intent.createChooser(intent, "Select Profile Image"), 1); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_maps, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.menu_home) { 
      Intent i = new Intent(this, HomeScreen.class); 
      startActivity(i); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void onButtonClick(View v) { 
     if (v.getId() == R.id.BeditProfile) { 
      Intent i = new Intent(Profile.this, editProfile.class); 
      startActivity(i); 
     } 

     if (v.getId() == R.id.AdContactButton) { 
      MainActivity.addContact(Cube.Name, Cube.Adress, "", Cube.Mobile, Cube.eMail); 
      MainActivity.populateList(); 
      Toast pass = Toast.makeText(this, Cube.Name + " " + "Has been added to your contacts", Toast.LENGTH_LONG); 
      pass.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 10); 
      pass.show(); 
     } 
    } 

    public void onActivityResult(int reqCode, int resCode, Intent data) { 
     if(resCode == RESULT_OK){ 
      if(reqCode == 1) 
       contactImageImgView.setImageURI(data.getData()); 
     } 
    } 
} 

我們得到了關於我們的按鈕「添加聯繫人」的新的崩潰。其中說populateList無法執行

+0

[調用非靜態方法的可能重複在Java中的靜態方法](http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java) –

回答

6

如果populateList聲明不是static,則不能參考MainActivity.populateList();
Check JLS (§8.5)

必須創建的MainActivity

MainActivity ma = new MainActivity(); // or another constructor 
ma.populateList(); // valid call of method 

實例或者,如果你的MainActivity聲明populateList()如下不需要實例:

public static void populateList() 
+0

它的工作,他們固定它,但現在別的東西是壞了(啊編程奇蹟啊) – Uridel

+1

檢查你的代碼,調試,如果還有其他錯誤**發佈一個新的問題** ...不要更新這一個!! [這是爲什麼](http://meta.stackoverflow.com/questions/260066/drastic-question-revision) –

相關問題