2013-08-13 19 views
2

인수에 적용되지 않습니다 :방법은 내가 구조를 가지고

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    if(getActivity() != null) 
     Intent intenta = StatisticsActivity.newInstance(this, (Question)mStream.get(position)); 
    startActivity(intenta); 
} 

내가

(Intent intenta = StatisticsActivity.newInstance(this, (Question)mStream.get(position))): 
The method newInstance(Activity, Question) in the type StatisticsActivity is not applicable for the arguments (UserQuestionsFragment, Question). 

newInstance 문제가 있습니다

public static Intent newInstance(Activity activity, Question question) { 
    Intent intent = new Intent(activity, StatisticsActivity.class); 
    intent.putExtra(QUESTION_KEY, question); 
    return intent; 
} 

이클립스가 변경 newInstance을 제공합니다 :

public static Intent newInstance(UserQuestionsFragment userQuestionsFragment, Question question) { 

    Intent intent = new Intent(userQuestionsFragment, StatisticsActivity.class); 
    intent.putExtra(QUESTION_KEY, question); 
    return intent; 
} 

하지만 오류가 발생합니다. 무엇이 가능할까요? 미리 감사드립니다.

+0

오류에

if(getActivity() != null) Intent intenta = StatisticsActivity.newInstance(this, (Question)mStream.get(position)); // Also, this line should be giving you a compiler error // because you created intenta inside if clause, so // it's not visible here startActivity(intenta); 

하는 라인? –

+0

안녕하십니까 : Intent intenta = StatisticsActivity.newInstance (this, (Question) mStream.get (position)) – Forme

+0

newInstance - 표시된 – Forme

답변

1

FragmentnewInstance() 메서드로 전달하려고 시도했지만 Activity이 필요합니다. 전 이클립스 제안 버전에서 변경이이

Activity curActivity = getActivity(); 
if(curActivity != null) { 
    Intent intenta = StatisticsActivity.newInstance(
    /* this is where the change is -> */ curActivity, (Question)mStream.get(position)); 
    startActivity(intenta); 
} 
2

안드로이드의 Intent 생성자는 (UserQuestionFragments, XXX)를 인수로 사용하지 않습니다.

생성자는 다음과 같습니다 :

Intent() 
Create an empty intent. 

Intent(Intent o) 
Copy constructor. 

Intent(String action) 
Create an intent with a given action. 

Intent(String action, Uri uri) 
Create an intent with a given action and for a given data url. 

Intent(Context packageContext, Class<?> cls) 
Create an intent for a specific component. 

Intent(String action, Uri uri, Context packageContext, Class<?> cls) 
Create an intent for a specific component with a specified action and data. 

희망이 도움이됩니다.

관련 문제