0

I'm trying to build a simple application that displays two fragments. The first fragment is displayed by default. It contains a list of names which you can choose and when you click on one of the items, it supposes to display a second fragment with a text view, displaying the name you have chosen.

The problem is everytime I click one of the names on the list, it throws me a NullPointerException. I really don't know what could be the problem.

Here are the codes( The app contains three class - two fragments and one activity. The FriendsF fragment is the list fragment and it performs well. The second fragment is FeedFragment and onitemclick it should display the name that was clicked)

FriendsF fragment:

package com.example.fragmentsexcersize;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FriendsF extends ListFragment {

private static final String[] FRIENDS = { "ladygaga", "msrebeccablack",
        "taylorswift13" };
public interface SelectionListener {
    public void onItemSelected(int position);
}
private SelectionListener mCallback;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
            : android.R.layout.simple_list_item_1;
    setListAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(), layout, FRIENDS));
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mCallback = (SelectionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement SelectionListener");
    }
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (isInTwoPaneMode()) {
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

@Override
public void onListItemClick(ListView l, View view, int position, long id) {
    mCallback.onItemSelected(position);
}

private boolean isInTwoPaneMode() {
    return getFragmentManager().findFragmentById(R.id.tweets) != null;
}
}

FeedFragment:

package com.example.fragmentsexcersize;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FeedFragment extends Fragment{

private TextView mTextView;
private static final String[] data = { "ladygaga", "msrebeccablack",
"taylorswift13" };

public FeedFragment() {

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tweet_view, container, false);
    mTextView = (TextView) rootView.findViewById(R.id.tweet_view);
    return rootView;
}

public void updateFeedDisplay(int position) {
    mTextView.setText(data[position]);
}
}

MainActivity:

package com.example.fragmentsexcersize;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.app.FragmentTransaction;

public class MainActivity extends Activity implements FriendsF.SelectionListener{

private FriendsF mFriendsFragment;
private FeedFragment mFeedFragment;
private FragmentManager fragMana;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mFriendsFragment = new FriendsF();
    fragMana = getFragmentManager();
    transaction = fragMana.beginTransaction();
    transaction.add(R.id.friends, mFriendsFragment);
    transaction.commit();
}

private boolean isInTwoPaneMode() {
    return findViewById(R.id.tweets) == null;
}
public void onItemSelected(int position) {

    if (mFeedFragment == null)
        mFeedFragment = new FeedFragment();
    if (!isInTwoPaneMode()) {
        transaction = fragMana.beginTransaction();
        transaction.add(R.id.tweets, mFeedFragment);
        transaction.commit();
    }
    mFeedFragment.updateFeedDisplay(position);
}
}
2

1 Answer 1

0

Make the following changes to your source:

MainActivity

public void onItemSelected(int position) {

    Bundle bundle = new Bundle();
    if (mFeedFragment == null)
        mFeedFragment = new FeedFragment();
    if (!isInTwoPaneMode()) {
        bundle.putInt("POSITION", position);
        mFeedFragment.setArguments(bundle);

        transaction = fragMana.beginTransaction();
        transaction.replace(R.id.tweets, mFeedFragment);
        transaction.commit();
    }
}

FeedFragment

private TextView mTextView;
private static final String[] data = { "ladygaga", "msrebeccablack",
"taylorswift13" };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    View rootView = inflater.inflate(R.layout.tweet_view, container, false);
    mTextView = (TextView) rootView.findViewById(R.id.tweet_view);

    int mPosition = getArguments().getInt("POSITION", 0);
    mTextView.setText(data[mPosition]);
    return rootView;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.