按钮无法打开新活动

时间:2018-10-02 06:36:21

标签: java android android-button

我使用viewpager进行“ inicio”活动,并且viewpager内的一页上有一个按钮,但是当我单击它时,什么也没有发生。

我尝试了在互联网上找到的每种模式,因为它什么也没有发生或“已停止工作”

我不知道该怎么办。这是最简单的事情(打开一个活动) 但是不知道为什么它不起作用。...

我认为可能是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);
            }

}
}

和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);
    }
}

3 个答案:

答案 0 :(得分:0)

您说您的button位于viewpager页面之一内

但是您已经在button casasslide内定义了activity

然后view pager页在哪里?

fragment中应该有一个viewPager的{​​{1}}。 并且button应该在此处定义,而不是在button

中定义

答案 1 :(得分:0)

这是不需要您编写 getactivity()或任何内容的活动。

就是这个

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

如果可以,请调试您的代码并检查是否调用onclick()

编码愉快!

答案 2 :(得分:0)

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.