user3539771
user3539771

Reputation: 11

Passing values from android activity to custom view

I'm attempting to pass values from an activity to a custom view which I can then later be used to draw. Thought that this should be easy to do but when run it keeps on throwing a NullPointerException error. Any help someone could provide would be appreciated. Thanks in advance.

Activity code:

public class PlayGame_Activity extends ActionBarActivity {
private Game game;
private GameView gameView;
private int cardNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_game_);

    game = new Game();
    cardNumber = 1;

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();

    }

    gameView = (GameView) findViewById(R.id.gameView1);
}


@Override
protected void onResume() {
    int[] testarray = {1,3,5,7,9}; //Real array values will come from game
    gameView.setCardNumbers(testarray);
    super.onResume();
}

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_play_game_,
                container, false);
        return rootView;
    }
}

View Code:

public class GameView extends View {
    private Paint paint;
    private int[] cardNumbers;

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    public void setCardNumbers(int[] array) {
        cardNumbers = array;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

}

Layout code

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.numberguessgame.PlayGame_Activity"
    tools:ignore="MergeRootFrame" />

and

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.numberguessgame.PlayGame_Activity$PlaceholderFragment" >

    <com.example.numberguessgame.GameView
        android:id="@+id/gameView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

So it appears that findviewbyid(gameview1) is returning null. Any ideas on where I should place that line of code?

Upvotes: 0

Views: 1549

Answers (3)

sushh
sushh

Reputation: 115

we can access the custom view variables in fragment and hence v can pass value from fragments to custom view.. Example: if custom view "Drawview" contains value "Value", v can set the value of Value in fragment like below: (assuming id of Drawview as drawview)

Drawview drawview = (Drawview)view.findValueById(R.id.drawview); drawview.Value=value;

this worked fr me.

Upvotes: 0

user3539771
user3539771

Reputation: 11

Turns out that moving the

 gameView = (GameView) findViewById(R.id.gameView1);

into onResume() instead of onCreate() fixed the problem.

Upvotes: 1

DDsix
DDsix

Reputation: 1984

You can use EventBus framework for sending objects from one activity to another (or other instance of a class). It helped me a lot and even allows objects to be delivered in threads independently from the posting thread. Just follow these four steps

  • implement the handling method: public void onEvent(MyEventType event)

  • register subscribers: EventBus.getDefault().register(this)

  • post events: EventBus.getDefault().post(event)

  • unregister: EventBus.getDefault().unregister(this)

Upvotes: 0

Related Questions