Em Ae
Em Ae

Reputation: 8704

Activity wouldn't start from the fragment

I am going through the tutorial on https://www.udacity.com/course/viewer#!/c-ud853/l-1474559101/e-1480808718/m-1480808721 and currently I am at Chapter 3, lesson 11. I have done everything mentioned in the tutorial but my activity wouldn't start. HEre is what i have put in the manifest

    <activity
        android:name=".DetailActivity"
        android:label="@string/title_activity_detail"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.sunshine.app.MainActivity" />
    </activity>

And here is my code which is calling this activity onItemClick listener

    listViewForecast.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent detailActivity = new Intent(getActivity(), DetailActivity.class);
            detailActivity.putExtra(Intent.EXTRA_TEXT, mForecastAdapter.getItem(position));
            getActivity().startService(detailActivity);
        }
    });


    return rootView;

This code is being called, I was using the Toast message to see if it is working or not and it works. However, when i put the activity there, It doesn't show up thew new activity.

And here is my DetailActivity (posting the class with ommited menus etc)

public class DetailActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_detail, container, false);

        Intent intent = getActivity().getIntent();
        if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
            String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
            ((TextView) rootView.findViewById(R.id.detail_text))
                    .setText(forecastStr);
        }
        return rootView;
    }
}
}

Upvotes: 0

Views: 79

Answers (1)

M D
M D

Reputation: 47807

doing wrong

 getActivity().startService(detailActivity);

Instead of do

 getActivity().startActivity(detailActivity);

called startActivity(...)

Upvotes: 2

Related Questions