onClick事件的按钮不工作android

时间:2016-02-28 10:25:39

标签: android button

我试图在我的应用中通过yalantis实现断头台菜单。 我在断头台活动中放了一个按钮,并为断头台活动做了一个班级。在那个课程中,我试图为按钮实现听众。我还在该功能中举杯,但也没有显示出来。 这是断头台活动:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/guillotine_background"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/guillotine_background"
            android:layout_margin = "0dp"
            android:divider="#666"
            android:orientation="vertical"   >
    <android.support.v7.widget.Toolbar
        style="@style/Toolbar"
        app:contentInsetStart="0dp">

        <ImageView
            android:id="@+id/guillotine_hamburger"
            style="@style/ImageView.Hamburger"
            android:src="@drawable/ic_menu" />
    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:id="@+id/profile_group"
        style="@style/LinearLayout.GuillotineItem"
        android:layout_marginTop="@dimen/guillotine_medium_margin">

        <ImageView
            style="@style/ImageView.GuillotineItem"
            android:src="@drawable/ic_profile" />

        <Button
            style="@style/TextView.GuillotineItem"
            android:text="MAIN PAGE"
           android:background="@color/guillotine_background"
            android:id="@+id/b1"
            />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/feed_group"
        style="@style/LinearLayout.GuillotineItem">

        <ImageView
            style="@style/ImageView.GuillotineItem"
            android:src="@drawable/ic_feed" />

        <TextView
            style="@style/TextView.GuillotineItem"
            android:text="@string/feed" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/activity_group"
        style="@style/LinearLayout.GuillotineItem">

        <ImageView
            style="@style/ImageView.GuillotineItem"
            android:src="@drawable/ic_activity_active" />

        <TextView
            style="@style/TextView.GuillotineItem.Selected"
            android:text="@string/activity" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/guillotine_divider_height" />

    <LinearLayout
        android:id="@+id/settings_group"
        style="@style/LinearLayout.GuillotineItem">

        <ImageView
            style="@style/ImageView.GuillotineItem"
            android:src="@drawable/ic_settings" />

        <TextView
            style="@style/TextView.GuillotineItem"
            android:text="@string/settings" />
    </LinearLayout>
</LinearLayout>
    </ScrollView>
</LinearLayout>

这是班级:

package manipal.freshers;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;

import java.io.File;

/**
 * Created by Ankit Vimal on 28-02-2016.
 */
public class Guillotine extends AppCompatActivity {
    private Button b1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guillotine);
        b1=(Button)findViewById(R.id.b1);
        b1.setOnClickListener(new OnClickListener() {
                                  public void onClick(View v) {
                                      Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();

                                      Intent i = new Intent(Guillotine.this, MIT_Sports_Club_Activity.class);
                                      startActivity(i);


                                  }

                              }
        );
}

}

Styles.xml:

<resources>

    <style name="LinearLayout" />

    <style name="LinearLayout.GuillotineItem">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:layout_marginTop">@dimen/guillotine_small_margin</item>
    </style>

    <style name="TextView" />

    <style name="TextView.ToolbarTitle">
        <item name="android:gravity">center</item>
        <item name="android:textSize">24sp</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="TextView.GuillotineItem">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">@dimen/guillotine_small_margin</item>
        <item name="android:textSize">@dimen/guillotine_item_text_size</item>
        <item name="android:layout_gravity">center_vertical</item>
        <item name="android:textColor">@android:color/white</item>
    </style>

    <style name="TextView.GuillotineItem.Selected">
        <item name="android:textColor">@color/selected_item_color</item>
    </style>

    <style name="Toolbar">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:minHeight">?attr/actionBarSize</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@color/guillotine_background</item>
    </style>

    <style name="ImageView" />

    <style name="ImageView.ContentItem">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">match_parent</item>
    </style>

    <style name="ImageView.Hamburger" parent="Widget.AppCompat.ActionButton">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_marginBottom">@dimen/abc_action_bar_overflow_padding_start_material</item>
    </style>

    <style name="ImageView.GuillotineItem">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center_vertical</item>
        <item name="android:layout_marginLeft">@dimen/guillotine_large_margin</item>
    </style>

</resources>

的themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.Guillotine"/>

    <style name="Theme.Guillotine" parent="Theme.AppCompat.NoActionBar">
        <item name="android:color">@color/guillotine_background</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:colorPrimaryDark">@color/guillotine_background</item>
        <item name="android:colorPrimary">@color/guillotine_background_dark</item>
        <item name="android:windowActionBar">false</item>
        <item name="windowActionBar">false</item>
    </style>

</resources>

MainActivity.java:

package manipal.freshers;

import android.annotation.SuppressLint;

import android.os.Bundle;
import android.os.Handler;

import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;

import android.view.View;

import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.yalantis.guillotine.animation.GuillotineAnimation;


public class MainActivity extends AppCompatActivity {
    private static final long RIPPLE_DURATION = 250;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.root)
    FrameLayout root;
    @InjectView(R.id.content_hamburger)
    View contentHamburger;

    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private TextView t;
    int []imageArray={R.drawable.manipal1,R.drawable.manipal2,R.drawable.manipal3,R.drawable.manipal4,R.drawable.manipal5};
    private ImageView i1;
    private final Handler handler = new Handler();
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        i1=(ImageView) findViewById(R.id.imageView1);
        t = (TextView) findViewById(R.id.textView3);
        t.setMovementMethod(new ScrollingMovementMethod());

         if(toolbar!= null){
        if (android.os.Build.VERSION.SDK_INT >= 11){
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(null);
                    }}
        View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.guillotine, null);
        root.addView(guillotineMenu);

        new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(R.id.guillotine_hamburger), contentHamburger)
                .setStartDelay(RIPPLE_DURATION)
                .setActionBarViewForAnimation(toolbar)
                .setClosedOnStart(true)
                .build();

        Runnable runnable = new Runnable() {
            int i=0;
            public void run()
            {
                i1.setImageResource(imageArray[i]);
                i++;
                if(i>imageArray.length-1)
                {
                    i=0;
                }
                handler.postDelayed(this, 3000);  //for interval...
            }

        };
        handler.postDelayed(runnable,0);
    }

}

1 个答案:

答案 0 :(得分:0)

I tapped on Main Page I'm able to see toast

package com.example.vinaymaneti.button;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button b1;

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

        b1 = (Button) findViewById(R.id.b1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
            }
        });
    }
}