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 :(