简体   繁体   English

OnClickListener 没有响应

[英]OnClickListener not responding

I can't get my click listeners working.我无法让我的点击监听器工作。 The ImageButton is retrieved correctly. ImageButton 被正确检索。

Listener (Extending my custom Activity which is setting the view):侦听器(扩展我正在设置视图的自定义活动):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("UserActivity", "Loading imagebutton...");
    ImageButton iButton = (ImageButton) findViewById(R.id.user_button_ratings);
    Log.d("UserActivity", "Button " + iButton);
    iButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Button", "Image was clicked");
        }
    });
}

BaseActivity基本活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout,
                R.drawable.ic_drawer, 0, 0) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(R.string.menu);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        layers = getResources().getStringArray(R.array.layers_array);
        drawerList = (ListView) findViewById(R.id.left_drawer);

        drawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.adapter_navigation, R.id.navigation_text, layers));

        final BaseActivity activity = this;
        drawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {
                // Do something
            }
        });

        drawerList.setBackgroundColor(Color.LTGRAY);
    }

Layout:布局:

<ImageButton
    android:id="@+id/user_button_ratings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@color/transparent"
    android:scaleType="fitCenter"
    android:src="@drawable/mmb_rating_big" />

Manifest:显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.panic.xenira.mmb"
    android:versionCode="2"
    android:versionName="0.1.1" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SearchActivity"
            android:label="@string/title_activity_search" >
        </activity>
        <activity
            android:name=".BaseActivity"
            android:label="@string/title_activity_base" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <activity
            android:name=".UserActivity"
            android:label="@string/title_activity_user" >
        </activity>
        <activity
            android:name=".UserViewerActivity"
            android:label="@string/title_activity_user_viewer" >
        </activity>
    </application>

</manifest>

There is no error in the LogCat and the onClick method is not caled. LogCat 中没有错误,并且没有调用 onClick 方法。 I've tryed different methods of using the listener but never got any response.我尝试了使用侦听器的不同方法,但从未得到任何响应。 I also tryed a code that worked for me befor but no response.我还尝试了一个对我有用的代码,但没有响应。

Would be great if you culd help me out ;)如果你能帮我一下,那就太好了;)

When using a DrawerLayout , there should be only one main content View , with the drawer View - in this case, your ListView - listed after it.当使用DrawerLayout ,应该只有一个主要内容View ,抽屉View - 在这种情况下,你的ListView - 在它后面列出。 Using a DrawerLayout in any other way will result in incorrect, unpredictable behavior, often preventing normal interaction with other layout elements.以任何其他方式使用DrawerLayout将导致不正确的、不可预测的行为,通常会阻止与其他布局元素的正常交互。

A tutorial with links to a sample and docs can be found on this developer page .可以在此开发人员页面上找到包含示例和文档链接的教程。

Here I am giving an example of Image button.这里我给出了一个图像按钮的例子。 If it is also giving error then clean projects of eclipse and try again.如果它也给出错误,则清理 Eclipse 项目并重试。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="35dp"
    android:layout_marginTop="32dp"
    android:contentDescription="image"
    android:src="@drawable/ic_launcher" />

 import android.app.Activity;
 import android.os.Bundle; 
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.ImageButton;
 import android.widget.Toast;

 public class MainActivity extends Activity {

ImageButton imgButton;

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

    addListenerOnButton();
}

private void addListenerOnButton() {

    imgButton = (ImageButton) findViewById(R.id.imageButton1);

    imgButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT)
                    .show();
        }
    });
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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