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

Eclipse 提供了更改newInstance

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

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

但它也会引发错误。什么是可能的?提前致谢

4

2 回答 2

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.

希望这可以帮助。

于 2013-08-13T16:43:08.170 回答
1

您正在尝试传入FragmentnewInstance()方法,但它需要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);
}
于 2013-08-13T16:44:08.657 回答