Forme
Forme

Reputation: 301

The method in the type is not applicable for the arguments

I have structure:

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

I have issue

(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 offers the change newInstance:

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

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

But it also raises an error. What may be possible? Thanks in advance

Upvotes: 2

Views: 7737

Answers (2)

Ivan Bartsov
Ivan Bartsov

Reputation: 21046

You're trying to pass Fragment into the newInstance() method, but it expects Activity. In pre-eclipse-suggestion version change this

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

to this

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

Upvotes: 1

JNL
JNL

Reputation: 4703

The Intent constructor in android does not take (UserQuestionFragments, XXX ) as arguments.

The constructors are below:

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.

Hope this helps.

Upvotes: 2

Related Questions