-2

newbie here to android/java code... fully expect downvotes. but hopefully some other dude will be able to easily find this and not have to ask what the problem is.

Trying to get a button when clicked to send user to an external website

public class MessagesFragment extends Fragment {

public MessagesFragment() {
    // Required empty public constructor
}

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

    Button button = (Button)findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            Intent viewIntent =
                    new Intent("android.intent.action.VIEW",
                            Uri.parse("http://www.stackoverflow.com/"));
            startActivity(viewIntent);
        }
    });

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_messages, container, false);

}

}

Button,(Button)findViewById, setOnClickListener, OnClickListener(), Intent, Intent, Uri. all have errors -_-

Here is the xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#67ffae"
tools:context=".MessagesFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="20sp"
    android:textStyle="bold"
    android:text="Messages Fragment" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</FrameLayout>

lalalalalalalalalalalalalalalalalalalalala I know its mostly code that's what we are discussing stack overflow.

  • Do you have `import` statements at the top of the class for `android.widget.Button`, `android.content.Intent`, `android.net.Uri`, etc.? – Mike M. Jun 24 '18 at 04:20
  • yes I now do the only error now is with findViewById –  Jun 24 '18 at 04:25
  • and onclicklistener –  Jun 24 '18 at 04:26
  • `OnClickListener` is another `import`: `import android.view.View.OnClickListener;`. Hit ctrl+alt+O, I think it is, and it should do those semi-automatically. Have a look at [this answer](https://stackoverflow.com/a/6496013) to see how to use `findViewById()` in a `Fragment`'s `onCreateView()` method. – Mike M. Jun 24 '18 at 04:30
  • im not sure how the findViewById() relates to mine. thanks for the help! –  Jun 24 '18 at 04:33
  • `Fragment` does not have a `findViewById()` method. You have to `inflate()` the `View` first, and call `findViewById()` on that, like is shown in that answer. – Mike M. Jun 24 '18 at 04:35
  • getView(). crashes the app and onCreateView() will not run –  Jun 24 '18 at 04:36
  • You're looking at the wrong one. Follow the answer I linked directly: https://stackoverflow.com/a/6496013. – Mike M. Jun 24 '18 at 04:37
  • View view = inflater.inflate(R.layout.fragment_messages, container, false); Button button = (Button) view.findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.stackoverflow.com/")); startActivity(viewIntent); –  Jun 24 '18 at 04:40
  • Yes, sorry I had not seen you updated message. now button does nothing, no errors though –  Jun 24 '18 at 04:40
  • Note the `return` statement there: `return view;`. – Mike M. Jun 24 '18 at 04:40
  • unexpected return value –  Jun 24 '18 at 04:44
  • You need to change your `return inflater.inflate(...);` statement to that. Don't try to add that somewhere else. – Mike M. Jun 24 '18 at 04:45
  • wouldn't that break the navigation view I have setup though? –  Jun 24 '18 at 04:46
  • no it doesn't thank you for your help! –  Jun 24 '18 at 04:48
  • I have no idea what "navigation view" you mean, but, no, that's what you need to return there. You're just moving the inflation to the top of the method, and returning the inflated `View` at the end, instead of inflating and returning in one line. – Mike M. Jun 24 '18 at 04:48
  • Would you mind pasting the following code so I can give you credit for answering –  Jun 24 '18 at 04:49
  • import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.content.Intent; import android.net.Uri; import android.view.View.OnClickListener; import android.widget.ImageView; /** * A simple {@link Fragment} subclass. */ –  Jun 24 '18 at 04:49
  • public class MessagesFragment extends Fragment { –  Jun 24 '18 at 04:50
  • public MessagesFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_messages, container, false); Button button = (Button) view.findViewById(R.id.button); –  Jun 24 '18 at 04:50
  • button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.stackoverflow.com/")); startActivity(viewIntent); } }); // Inflate the layout for this fragment return view; } } –  Jun 24 '18 at 04:50
  • 1
    Oh, I'm good. Thanks, though. This kinda turned into a little tutorial session that covered multiple things, which we're really not supposed to do. Feel free to post an answer yourself, if you like. Glad you got it working. Cheers! – Mike M. Jun 24 '18 at 04:54

1 Answers1

1

Everything is correct in the code just change

Intent viewIntent= new Intent("android.content.Intent.action.VIEW",
                           Uri.parse("http://www.stackoverflow.com/"));
startActivity(viewIntent);

Hope it will work

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50