0

I am using tab activity and add fragments and activity. Now I want to open activity by clicking on button in fragment this is not working when I click on button application crash error occur application stop unfortunately.

I am using this code:

public class SmsList extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.fragment_sms_list, container, false);
        Button button1 = (Button)rootview.findViewById(R.id.btinboxsms);
        Button button2 = (Button)rootview.findViewById(R.id.btsetetime);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(getActivity(), InboxSms.class);

                startActivity(i);

            }
        });

        return rootview;
    }
}

java.lang.IllegalStateException: Could not find method setListFooter(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btinboxsms'

Julien Marrec
  • 11,605
  • 4
  • 46
  • 63
Usman Arshad
  • 153
  • 1
  • 10

3 Answers3

0

Try this

this.getActivity.startActivity(this.getApplicationContext(), InboxSms.class);
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
0
 Context context=rootview.getContext();
 Intent i = new Intent(context, InboxSms.class);
 startActivity(i);
Gautam Dev
  • 399
  • 2
  • 4
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yennsarah Dec 08 '16 at 09:48
  • Thanks Brother,I am new on Stackoverflow will keep in future. – Gautam Dev Dec 08 '16 at 10:49
0

You have to call just Intent on click of Button in Fragment. try below code hope this will work for you.

public class AlertFragment extends android.support.v4.app.Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
abcBtnn.setOnClickListener(new View.OnClickListener() {
 @Override
        public void onClick(View view) {
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
}
});
}

P.S You have to declare your activity in your app Menifest

Amey Bawiskar
  • 361
  • 1
  • 3
  • 17