1
package com.example.mukullashkari.onlinetestportal;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Tab1_fragment extends Fragment {

Button btnC;

public static Tab1_fragment newInstance(int sectionNumber) {
    Tab1_fragment fragment = new Tab1_fragment();
    return fragment;
}

public Tab1_fragment()
{

}
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    btnC=(Button) btnC.findViewById(R.id.C_btn);
    btnC.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent=new Intent(Tab1_fragment.this,Cassessment.class);
            startActivity(myIntent);
        }
    }

    );
}


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


   return inflater.inflate(R.layout.tab1_fragment,container,false);
}

}

I have created Cassessment activity.But the error is coming in Intent Constructor. Says that constructor cannot be resolved. What is wrong in this code can anyone help !!

ERROR IS::

Cannot resolve constructor 'Intent(com.example.mukullashkari.onlinetestportal.Tab1_fragment, java.lang.Class)'

Mukul
  • 23
  • 1
  • 4

2 Answers2

0

you are passing fragment as Context that is invalid get context in fragment use use getActivity() like below code

btnC=(Button) btnC.findViewById(R.id.C_btn);
    btnC.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent=new Intent(getActivity(),Cassessment.class);
            startActivity(myIntent);
        }
    }

or try this

 Intent myIntent=new Intent(Tab1_fragment.this.getActivity(),Cassessment.class);
 startActivity(myIntent);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

The Intent constructor is expecting you to provide a Context as the first parameter, either change this to something like getActivity() or use another Context, (a fragment is not a valid Context, but an Activity is).

RestingRobot
  • 2,938
  • 1
  • 23
  • 36