1

I tried using a second class that extends Activity but still crashing... This is what I have

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Fragment1 extends Fragment{
    private Button buttonOne ;
    private Handler handler;
    private Activity activity ;
    @Override


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View V = inflater.inflate(R.layout.fragement1_layout, container, false);
        final Button button = (Button)V.findViewById(R.id.two);
        ImageView imageView = (ImageView)V.findViewById(R.id.my_image);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.setText("Hello");

            }
        });

      activity.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
            }
        });

        return V;
    }


}

I can't call a a toast message from the same class that extends fragment so I created one object that extends activity for a toast message but no luck :|. Can someone here help me.

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66

2 Answers2

0

Try Toast.makeText(this.getActivity(), "Hello", Toast.LENGTH_SHORT).show();

DigitalNinja
  • 1,078
  • 9
  • 17
  • Add `import android.app.Fragment;` to your imports. – DigitalNinja Apr 02 '15 at 22:51
  • I should say I started off using `import android.support.v4.app.Fragment;` as well but after upgrading `android.app.Fragment` should be used. – DigitalNinja Apr 02 '15 at 22:59
  • ... Unless you need the app to run on older versions of Android. See this [post](http://stackoverflow.com/questions/15109017/difference-between-android-app-fragment-and-android-support-v4-app-fragment). – DigitalNinja Apr 02 '15 at 23:05
  • If you use `this.getActivity()` within the Toast code he provided, it will attempt to find a `getActivity()` method within the anonymous Runnable class – Ed Holloway-George Apr 02 '15 at 23:12
  • Right, okay, but if it's called just inside `onCreateView` it will work - cause I do it all the time for debugging purposes. I didn't think about the runnable or rather I overlooked `activity.runOnUiThread(new Runnable()`. – DigitalNinja Apr 02 '15 at 23:14
0

From your code, I can see no need for the runOnUiThread there? All of the onCreateView method is executed on the UI thread anyway.

I believe, as you mention it is crashing, this is probably a 'NPE' caused by using your activity variable. You haven't supplied any code to suggest it is being initialised.

To get the current Context within a Fragment, be it a android.support.v4.app.Fragment or a android.app.Fragment, you can use getActivity().

The following code should be used:

Toast.makeText(getActivity(), "Hello", Toast.LENGTH_SHORT).show();

Should this code be only a snippet and you do require the runOnUiThread, you can just use getActivity() as follows:

getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_SHORT).show();
            }
        });
Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66