类型中的方法不适用于参数

时间:2013-08-13 16:35:05

标签: java android

我有结构:

@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;
}

Eclipse提供了更改newInstance

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

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

但它也引发了一个错误。 什么可能? 提前致谢

2 个答案:

答案 0 :(得分:2)

android中的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.

希望这有帮助。

答案 1 :(得分:1)

您尝试将Fragment传递给newInstance()方法,但它期望Activity。 在pre-eclipse-suggestion版本中更改此

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);

到这个

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