Pharetra
Pharetra

Reputation: 302

Cannot Resolve Intent Constructor

I'm developing an app that uses the Reddit API to display a bunch of posts in a listview. When you click on a post in the listview, the plan is to open a detailactivity that shows some of the details of the post. I've got some problems making it work, however.

This is the code I'm using in the mainactivity class (where the listview is displayed):

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;

public class MainActivity extends Activity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context context = this;
        Fresco.initialize(context);
        setContentView(R.layout.activity_main);
        addFragment();

        View listview = findViewById(R.id.posts_list);
        listview.setOnClickListener (new android.widget.AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View p_view,
                                    int p_pos, long arg3) {


                TextView text = (TextView ) p_view.findViewById(R.id.post_link); //id of textview from layout file that you are inflating.

                CharSequence mytext=text.getText();
                Intent i = new Intent(this, DetailActivity.class);
                i.putExtra("text",mytext);
                startActivity(i);

            }
        });

    };

    void addFragment(){
        getFragmentManager()
                .beginTransaction()
                .add(R.id.fragments_holder
                        , PostsFragment.newInstance("wallpapers"))
                .commit();
    }

    @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_main, 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.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

Whenever a list item is clicked, the text in the textview with id "post_link" will be sent to the new activity so I can display it in there.

The problem is with the line Intent i = new Intent(this, DetailActivity.class);. Android Studio says it cannot resolve the constructor. I tried a few solutions from the other questions, but these didn't work, sadly.

Upvotes: 1

Views: 3014

Answers (4)

Zain
Zain

Reputation: 2372

Are you inside a Fragment? In which case the you code should look like

Intent intent = new Intent(getApplicationContext(), DetailActivity.class);

Or you can hold a reference to the context and use that instead of getActivity()

Upvotes: 0

Mohammad Arman
Mohammad Arman

Reputation: 7065

Try this:

Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);

Upvotes: 0

Andrea Cinesi
Andrea Cinesi

Reputation: 276

try this

Intent intent = new Intent(First.this, Second.class);
intent.putExtra("mytext","" +p_pos);
startActivity (intent)

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13348

Try this code

Intent myIntent = new Intent(getApplicationContext(), DetailActivity.class);

Upvotes: 1

Related Questions