open an Activity from another Activity

2

Good morning everyone I develop an app in Android studio but I have a problem opening an activity and that is when I touch a button, it is supposed to send me to another screen however when I do that, the app closes, I followed the steps I've seen on YouTube but it does not work. ..

this is the main activity that calls another one called searchtabs

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


Button btnLogin;
EditText txtUser, txtPass;

@Override
public void onClick(View view) {
    Intent intent = new Intent(getApplicationContext(),searchtabs.class);
    startActivity(intent);
}

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

    txtUser = (EditText) findViewById(R.id.txtUser);
    txtPass = (EditText) findViewById(R.id.txtPass);
    btnLogin = (Button) findViewById(R.id.btnInicio);

    btnLogin.setOnClickListener(this);

 }
}

and this is the activity that should be executed

public class searchtabs extends AppCompatActivity {

private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;

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

    tabLayout = (TabLayout) findViewById(R.id.tablayout_id);
    appBarLayout = (AppBarLayout) findViewById(R.id.appbarid);
    viewPager = (ViewPager) findViewById(R.id.viewpager_id);

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    // Añadiendo fragments
    adapter.AddFragment(new FragmentBuscar(), "Buscar");
    adapter.AddFragment(new FragmentPromo(), "Promociones");
    adapter.AddFragment(new FragmentPerfil(), "Perfil");
    // adaptador
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
 }
}

If I forget something, I thank you, thank you.

    
asked by ragnamex 23.07.2018 в 06:40
source

1 answer

1

The error message says that it does not find the Activity , probably because it has not been declared in the file AndroidManifest.xml .

It is convenient that you open that file and check if you have added your activity searchtabs .

There must be something like this more or less, and if it is not there, you should add it:

    <activity
        android:name=".activities.searchtabs"
        android:label="@string/title_activity_searchtab"
        android:parentActivityName=".activities.MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="tu-paquete.activities.MainActivity" />
    </activity>

You must verify all attributes such as label, parentActivityName (if you have it), theme ... and the% meta-data if it is an activity dependent on another. In all those parts you should put real data of your context.

If you have questions about the structure of Manifest , you can check the documentation of Android about it .

  

NOTE ON THE NAME CONVENTION:

     

It is convenient that you apply the rules on the naming convention   to make your code readable for any programmer who owes it   review or if in the future there is work in collaboration with others.

     

The naming convention for activities is the same as for the   classes: the first letter of each word in uppercase , also, by   generally, when it is a activity the word is usually put    Activity in the name. Respecting those rules your class should be   Call: SearchTabsActivity .

     

This does not affect the operation of the code, but it does affect the clarity of the   same.

    
answered by 23.07.2018 / 07:22
source