cryptomanic
cryptomanic

Reputation: 6306

Unable to understand communication between fragments?

I am trying to increase the value in textview by 1 in second fragment when button fron first fragment is clicked.No error detected by IDE(eclipse).When i run the app and click the button a dialog box appears saying [unfortunately,apple (my app name)has stopped] .I google and find that we should use interface for inter-communication.But i want to know where i am making the mistake.Which concept i am applying incorrectly.please help me...

MainActivity.Java

package com.iphone.apple;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

MyFragment_2 ob;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ob = new MyFragment_2();

}

public void run(int count) {
    ob.rockon(count);
  }

}

FirstFragment

package com.iphone.apple;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MyFragment extends Fragment implements OnClickListener {
int counter = 0;
Button mButton;
MainActivity ob;

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.myfragment, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    mButton = (Button) getView().findViewById(R.id.button1);
    mButton.setOnClickListener(this);
    ob = new MainActivity();
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    counter++;
    ob.run(counter);
}

}

SecondFragment

package com.iphone.apple;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment_2 extends Fragment {
  TextView ob = null;

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)             {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_2, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    ob = (TextView) getView().findViewById(R.id.textview);
}

public void rockon(int count) {
    ob.setText("count:"+count);
     }
  }

Upvotes: 0

Views: 32

Answers (1)

Henry
Henry

Reputation: 43728

    ob = new MainActivity();

You must never instantiate an activity yourself. That's the responsibility of the Android framework.

Upvotes: 1

Related Questions