java - Android 应用程序单击按钮调用不正确的 OnClick 监听器

标签 java android

我昨天将我的应用程序上传到 Google Play,今天早上我只想做一个布局调整,因为一些文本在较小的屏幕上是重叠的按钮,基本上我只是想将按钮进一步移动到屏幕下方。我认为这会像使用 eclipse 的图形编辑器一样简单...不。

我不知道为什么,但我对“view_fact”布局上的按钮位置所做的小修改已将按钮注册到错误的 OnClick 监听器, View 上只有两个按钮,它们正在使用彼此的事件监听器,我不知道为什么。我没有触及在旧布局上完美运行的事件监听器代码。

这是我的 view_fact 布局:

<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" >

<TextView
    android:id="@+id/viewFactTitleText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:text="@string/factTitleText"
    android:textSize="22dp"
    tools:context=".MainActivity" />

<ImageView
    android:id="@+id/randomFactImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/viewFactTitleText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:contentDescription="Fact Image"
    android:src="@drawable/canadaflag" />

<TextView
    android:id="@+id/factData"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/randomFactImage"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="TextView" />

<Button
    android:id="@+id/anotherFactButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/backToHomeButton"
    android:layout_alignLeft="@+id/backToHomeButton"
    android:layout_alignRight="@+id/backToHomeButton"
    android:text="@string/anotherFactButtonText" />

<Button
    android:id="@+id/backToHomeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/factData"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/factData"
    android:text="@string/backToHomeButtonText" />


</RelativeLayout>

监听器和启动代码:

public class MainActivity extends Activity {
/* Declaration of global variables */
private boolean debugMode = true; // Whether debugging is enabled or not 

private static String logtag = "CanadianFacts"; // For use as the tag when logging 
private TextView factData;
private int totalFacts = 72;
private String[][] facts = new String[totalFacts][5];
private int lastFact = 0;


/* Buttons */
/* Home page */
private Button randomFactButton;

/* View Fact page */
private Button anotherRandomFactButton;
private Button backToHomeButton;

/* About page */
private Button backToHomeFromAboutButton;

/* Image Views */
private ImageView randomFactImage;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /* Home Page Objects */
    randomFactButton = (Button)findViewById(R.id.randomFactButton);
    randomFactButton.setOnClickListener(randomFactListener); // Register the onClick listener with the implementation above

    /* View Fact Page Objects */


    /* Build Up Fact Array */
    buildFactArray();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_about:
            loadAboutPage(); // Call the loadAboutPage method
            return true;
     }

    return false;
}

public void loadAboutPage() {
    setContentView(R.layout.about);

    /* Set up home page button listener */
    backToHomeFromAboutButton = (Button)findViewById(R.id.backToHomeFromAboutButton); 
    backToHomeFromAboutButton.setOnClickListener(backToHomeListener); // We can reuse the backToHomeListener

}

/* Home Page Listeners */
//Create an anonymous implementation of OnClickListener, this needs to be done for each button, a new listener is created with an onClick method
private OnClickListener randomFactListener = new OnClickListener() {
    public void onClick(View v) {
        if (debugMode) {
            Log.d(logtag,"onClick() called - randomFact button");              
            Toast.makeText(MainActivity.this, "The random fact button was clicked.", Toast.LENGTH_LONG).show();
        }

      setContentView(R.layout.view_fact); // Load the view fact page

      /* We're now on the View Fact page, so elements on the page are now in our scope, instantiate them */

      /* Another Random Fact Button */
      anotherRandomFactButton = (Button)findViewById(R.id.anotherFactButton);
      anotherRandomFactButton.setOnClickListener(anotherRandomFactListener); // Register the onClick listener with the implementation above

      /* Back to Home Button */
      backToHomeButton = (Button)findViewById(R.id.backToHomeButton);
      backToHomeButton.setOnClickListener(backToHomeListener); // Register the onClick listener with the implementation above

      // Get a random fact
      String[] fact = getRandomFact();

      if (fact[2] == null) { // If this fact doesn't have an image associated with it
          fact[2] = getRandomImage();
      }

      int imageID = getDrawable(MainActivity.this, fact[2]);

      /* See if this fact has an image available, if it doesn't select a random generic image */
      randomFactImage = (ImageView) findViewById(R.id.randomFactImage);
      randomFactImage.setImageResource(imageID);

      factData = (TextView) findViewById(R.id.factData);
      factData.setText(fact[1]);

        if (debugMode) {
            Log.d(logtag,"onClick() ended - randomFact button");
        }
      }
};


/* View Fact Page Listeners */
private OnClickListener anotherRandomFactListener = new OnClickListener() {
    public void onClick(View v) {
        if (debugMode) {
            Log.d(logtag,"onClick() called - anotherRandomFact button");              
            Toast.makeText(MainActivity.this, "The another random fact button was clicked.", Toast.LENGTH_LONG).show();
        }

          // Get a random fact
          String[] fact = getRandomFact();

          if (fact[2] == null) { // If this fact doesn't have an image associated with it
              fact[2] = getRandomImage();
          }

          int imageID = getDrawable(MainActivity.this, fact[2]); // Get the ID of the image

          /* See if this fact has an image available, if it doesn't select a random generic image */
          randomFactImage = (ImageView) findViewById(R.id.randomFactImage);
          randomFactImage.setImageResource(imageID);

          factData = (TextView) findViewById(R.id.factData);
          factData.setText(fact[1]);

          if (debugMode) {
              Log.d(logtag,"onClick() ended - anotherRandomFact button");
          }
    }
};

private OnClickListener backToHomeListener = new OnClickListener() {
    public void onClick(View v) {
        if (debugMode) {
            Log.d(logtag,"onClick() called - backToHome button");              
            Toast.makeText(MainActivity.this, "The back to home button was clicked.", Toast.LENGTH_LONG).show();
        }

          // Set content view back to the home page
          setContentView(R.layout.main); // Load the home page

          /* Reinstantiate home page buttons and listeners */
          randomFactButton = (Button)findViewById(R.id.randomFactButton);
          randomFactButton.setOnClickListener(randomFactListener); // Register the onClick listener with the implementation above

          if (debugMode) {
              Log.d(logtag,"onClick() ended - backToHome button");
          }
    }
};

谢谢。

最佳答案

我设法解决了这个问题,方法是四处移动按钮,更改 ID 几次,然后再将它们改回来。并删除所有对齐设置并重置其位置。

一个很奇怪的问题,可能是因为eclipse的图形化编辑器。

关于java - Android 应用程序单击按钮调用不正确的 OnClick 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758052/

相关文章:

java - 如何在JAVA中访问java.lang.Object json值?

java - 如何实现可折叠(带 handle )的垂直侧边栏android

java - 将 openid4java 集成到 GWT 项目

android - SurfaceView 还是 GLSurfaceview?

android - 使用适用于 iO、Android 和 Windows Phone 的 Xamarin

java - Servlet 响应包装器存在编码问题

java - 远程管理接口(interface) : RemoteAccountService extends Remote

android - 使用 run-as 从内部存储中检索数据库或任何其他文件

java - 如何将 ImageView 与 SimpleAdapter 一起使用?

android - 如何在自定义警报对话框中使用 AutoCompleteTextView?