图像按钮在片段中不起作用

时间:2016-03-10 09:59:32

标签: android android-fragments android-fragmentactivity android-button android-imagebutton

我创建了一个Android应用程序并且卡住了!

无论我尝试什么,我无法解决它。

它确实像一个非按钮。

我的片段活动代码是

public class Home extends FragmentActivity {

    ActionBar action;

    Typeface tf1;
    TextView icindekiler;
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    Fragment fragment = null;


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


        viewPager=(ViewPager)findViewById(R.id.pager1);

        FragmentManager fm=getSupportFragmentManager();

        viewPager.setAdapter(new MyAdapter(fm));


    }

    class MyAdapter extends FragmentPagerAdapter{
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {



            if(position==0)
                fragment= new Home1();




            if(position==1)
                fragment=new Home2();
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }

}

和我的第一片段是

public class MainActivity extends Fragment {



    ImageButton btn;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View viewMain=inflater.inflate(R.layout.activity_main,container,false);

        btn=(ImageButton)viewMain.findViewById(R.id.calisanbtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();
                Intent i=new Intent(getActivity(),Sunus.class);
                startActivity(i);
            }
        });
        //actionBar gizleme
        //android.support.v7.app.ActionBar action=getActivity().getSupportActionBar();
        //action.hide();





        return viewMain;
    }


    }

以及我的片段Activity and Fragment XML

图像按钮中的XML

<ImageButton
                android:layout_width="110dp"
                android:layout_height="100dp"
                android:src="@drawable/opet_sunus"
                android:id="@+id/kurumsalbtn"
                android:background="@drawable/mybutton"
                android:layout_marginRight="20dp"
                android:scaleType="fitCenter"
                android:padding="5dp"

                />

最后一次认为我在View页面中的Fagment活动可能是我做错了。

 <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/pager1"
    >

</android.support.v4.view.ViewPager>

我无法解决它,任何人都可以帮助我吗? Thnx很多:)

修改

在logcat中

03-10 06:48:22.468 1751-1751/? D/dalvikvm: Late-enabling CheckJNI
03-10 06:48:22.528 1751-1757/? D/dalvikvm: Debugger has detached; object registry had 1 entries
03-10 06:48:22.576 1751-1751/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-10 06:48:22.576 1751-1751/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-10 06:48:22.576 1751-1751/? W/dalvikvm: VFY: unable to resolve interface method 14516: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-10 06:48:22.580 1751-1751/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-10 06:48:22.580 1751-1751/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-10 06:48:22.580 1751-1751/? W/dalvikvm: VFY: unable to resolve interface method 14520: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-10 06:48:22.580 1751-1751/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-10 06:48:22.636 1751-1751/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-10 06:48:22.636 1751-1751/? W/dalvikvm: VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-10 06:48:22.652 1751-1751/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-10 06:48:22.652 1751-1751/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-10 06:48:22.652 1751-1751/? W/dalvikvm: VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
03-10 06:48:22.652 1751-1751/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-10 06:48:22.976 1751-1751/? D/dalvikvm: GC_FOR_ALLOC freed 155K, 7% free 3310K/3528K, paused 17ms, total 18ms
03-10 06:48:23.088 1751-1751/? I/dalvikvm-heap: Grow heap (frag case) to 29.179MB for 27146244-byte allocation
03-10 06:48:23.100 1751-1755/? D/dalvikvm: GC_CONCURRENT freed 3K, 1% free 29816K/30040K, paused 2ms+2ms, total 18ms
03-10 06:48:23.100 1751-1760/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 17ms
03-10 06:48:23.432 1751-1751/? D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so
03-10 06:48:23.432 1751-1751/? D/: HostConnection::get() New Host Connection established 0xb8dafbd8, tid 1751
03-10 06:48:23.448 1751-1751/? D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so
03-10 06:48:23.448 1751-1751/? D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so
03-10 06:48:23.512 1751-1751/? W/EGL_genymotion: eglSurfaceAttrib not implemented
03-10 06:48:23.516 1751-1751/? E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
03-10 06:48:23.516 1751-1751/? E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
03-10 06:48:23.528 1751-1751/? E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
03-10 06:48:23.532 1751-1751/? E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
03-10 06:48:23.532 1751-1751/? D/OpenGLRenderer: Enabling debug mode 0
03-10 06:48:27.560 1751-1751/taslak.ridvan.com.crosscomtaslak D/dalvikvm: GC_FOR_ALLOC freed 76K, 1% free 35139K/35284K, paused 7ms, total 7ms
03-10 06:48:27.712 1751-1751/taslak.ridvan.com.crosscomtaslak D/dalvikvm: GC_FOR_ALLOC freed 3K, 1% free 42384K/42532K, paused 7ms, total 7ms
03-10 06:48:27.916 1751-1751/taslak.ridvan.com.crosscomtaslak W/EGL_genymotion: eglSurfaceAttrib not implemented

1 个答案:

答案 0 :(得分:2)

我认为你的错误是你的ImageButton id。在您的片段中,您设置了ID calisabtn

btn=(ImageButton)viewMain.findViewById(R.id.calisanbtn);

但在你的xml中声明为id kurumsalbtn

<ImageButton
                android:layout_width="110dp"
                android:layout_height="100dp"
                android:src="@drawable/opet_sunus"
                android:id="@+id/kurumsalbtn" <----
                android:background="@drawable/mybutton"
                android:layout_marginRight="20dp"
                android:scaleType="fitCenter"
                android:padding="5dp"

                />