-2

Is there anyway to start an activity on a button press from a fragment? I have followed exactly with the exact same project and variable names in the videos in youtube. I just keep getting force closes when i press the button, any ideas? here's my code in SubPage0.java:

import android.content.Context;
import android.content.Intent;
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.widget.TextView;

public class SubPage0 extends Fragment {


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_thread_feed, container, false);


    Button button = (Button) rootView.findViewById(R.id.but2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), ProjectInfo.class);
            getActivity().startActivity(intent);
        }
    });

    return rootView;
 }
 }

and heres the code in my fragment_thread_feed.xml

<Button
    android:id="@+id/but2"
    android:layout_width="0dp"
    android:layout_height="61dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:onClick="onClickListener"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

and here is the activity class I am wanting to jump into from that button with the filename ProjectInfo.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

public class ProjectInfo extends AppCompatActivity {

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


    Bundle bundle = getIntent().getExtras();
    if(bundle!=null){
        if(bundle.getString("some")!=null){
            Toast.makeText(getApplicationContext(),"data :" + bundle.getString("some"), Toast.LENGTH_SHORT).show();
            }

        }
    }

}

I am really a beginner, those codes are what I gathered and combined from tutorials and videos. Please help :(

2 Answers2

0

I think you problem come from

Intent intent = new Intent(getActivity(), ProjectInfo.class);
            getActivity().startActivity(intent);

Another thing its

Bundle bundle = getIntent().getExtras();

Where you are passing Extras values? LOL

Null pointer exception.

  • I did omit those lines, still getting force closes, any ideas? – KevinJonathan911 Oct 28 '17 at 11:24
  • 1
    i've posted my logcat, hope you can help @PauloRodrigues – KevinJonathan911 Oct 28 '17 at 12:26
  • java.lang.IllegalStateException: Could not find method onClickListener(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'but2' So see this: https://stackoverflow.com/questions/38392359/could-not-find-method-in-parent-or-ancestor-context Defining onClick in xml means you need to define it for a particular view here is but2 you can not have two arguments in that method. –  Oct 28 '17 at 12:31
  • 2
    @KevinJonathan911 just remove **`android:onClick="onClickListener"`** from your button as per pritesh ans – AskNilesh Oct 28 '17 at 12:32
  • @NileshRathod I did remove that, now when i click the button nothing happened – KevinJonathan911 Oct 28 '17 at 12:35
  • @PauloRodrigues now the button doesnt do anything – KevinJonathan911 Oct 28 '17 at 12:35
0

just remove android:onClick="onClickListener" from your button

<Button
    android:id="@+id/but2"
    android:layout_width="0dp"
    android:layout_height="61dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />