简体   繁体   中英

Button doesn't open new activity

I have a "inicio" activity with viewpager and have a button in one page inside of the viewpager , but when i click on it nothing happens.

I've tried every mode i found on internet cuz it nothing happens or "has stopped working"

I dont know what to do. It's simpliest thing ( open a activity ) but dont know why it dont works....

I think that maybe it is opened in background by the viewpager...

inicio . java

package com.example.vamaro.vamaroapp;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.vamaro.vamaroapp.R;

public class inicio extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inicio);
        ViewPager viewPager = (ViewPager)findViewById(R.id.iniciopagerp);
        viewPager.setAdapter(new inicioAdapter());

    }
    public class inicioAdapter extends PagerAdapter{
            LayoutInflater layoutInflater;
            int[] layouts={R.layout.inicioslide,R.layout.activity_casasslide,R.layout.deptoslide};
            @Override
    public int getCount(){
                return layouts.length;
            }
            @Override
    public boolean isViewFromObject(View view, Object object) {
           return (view==(LinearLayout)object);
            }
            @Override
    public Object instantiateItem(ViewGroup container,int position) {
                layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View vamaro1 = layoutInflater.inflate(R.layout.inicioslide, container,false);
                View vamaro2 = layoutInflater.inflate(R.layout.activity_casasslide, container,false);
                View vamaro3 = layoutInflater.inflate(R.layout.deptoslide, container,false);
                View viewarr[] = { vamaro1,vamaro2,vamaro3};
                container.addView(viewarr[position]);
                return viewarr[position];
            }
            @Override
            public void destroyItem(ViewGroup container,int position, Object object){
                container.removeView((LinearLayout)object);
            }

}
}

and the casasslider . java

package com.example.vamaro.vamaroapp;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class casasslide extends AppCompatActivity {
private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_casasslide);
        btn = (Button) findViewById(R.id.butt);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivityTotalC();
            }
        });
    }
    public void openActivityTotalC(){
        Intent i = new Intent(this, TotalCasas.class);
        casasslide.this.startActivity(i);
    }
}

you said your button is inside one of viewpager pages

but you've defined your button inside casasslide activity .

then where is the view pager page?

there should be a fragment in the viewPager that has the button . and the button should be defined there, not in the casasslide

That is alredy activity you don't need to write getactivity() or anything.

just this

Intent I = new Intent (this,Next.class);
startActivity(i);

if that working then debug your code and check onclick() called or not

happy coding!!!

Change from this

public void openActivityTotalC(){
    Intent i = new Intent(this, TotalCasas.class);
    casasslide.this.startActivity(i);
}

To this

public void openActivityTotalC(){
    Intent i = new Intent(this, TotalCasas.class);
    startActivity(i);
}

It should solve the problem. Otherwise you might need to post your Error log so other can help you to debug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM