【问题标题】:Buttons not working properly按钮无法正常工作
【发布时间】:2013-01-28 04:45:36
【问题描述】:

在我的 main.xml 中,我有 5 个按钮,所有这些按钮都指向不同的页面,但是当我启动应用程序时,这些按钮是它们的,但是当单击它们时它们不起作用。我认为问题可能出在AndroidManifest.xml

菜单.Java

package com.invoice;

import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;

public class menu extends Activity implements OnClickListener {
    /** called the activity is first created. */
    Button button1, button2, button3, button4, button5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);
        button4 = (Button)findViewById(R.id.button4);
        button5 = (Button)findViewById(R.id.button5);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
    }   
    public void onClick(View v) {
        //TODO Auto-generate method stub
        //figure out which button was pressed
        switch (v.getId()) {
        case R.id.button1:
            //do button1 action
            break;
        case R.id.button2:
            //do button2 action
            break;
        case R.id.button3:
            //do button3 action
            break;
        case R.id.button4:
            //do button4 action
            break;
        case R.id.button5:
            //do button5 action
            break;
        }


        }
    }

Android Manifest(仅按钮活动)

android:name=".menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.invoice.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        </activity>
    <activity android:name = ".Help" />
    <activity android:name = ".Job" />
    <activity android:name=".Fourm" />
    <activity android:name=".Receipt"/>
    <activity android:name=".Reportissue"/>

Job.java(每个按钮的 java 文件中都有相同的代码)

package com.invoice;


import android.app.Activity;
import android.os.Bundle;

public class Job extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.job);
    }

}  

Main.xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="87dp"
        android:text="@string/report_issue"
        android:width="300dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_marginBottom="29dp"
        android:text="@string/help"
        android:width="300dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignLeft="@+id/button3"
        android:layout_marginBottom="36dp"
        android:text="@string/fourm"
        android:width="300dp" android:height="50dp"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginTop="15dp"
        android:height="150dp"
        android:text="@string/reciept"
        android:width="145dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button5"
        android:layout_alignBottom="@+id/button5"
        android:layout_toLeftOf="@+id/button5"
        android:height="150dp"
        android:text="@string/job"
        android:width="150dp" />

    <DigitalClock
        android:id="@+id/digitalClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="38dp"
         />

</RelativeLayout>

【问题讨论】:

  • 请提供堆栈跟踪以提取更多详细信息
  • 请也发布按钮点击代码。那部分可能有问题。
  • 这看起来不错..请提供更多详细信息,以便我们为您提供帮助..并为此屏幕提供 xml...您还可以使用 log 来检查 onClick 方法是否被调用或不是..
  • 我刚刚添加了请求的代码。
  • 您在此处定义的 xml 文件名是 Menu.xml,在您的 java 文件中您使用的是 main 布局文件。为什么会这样?

标签: android button android-activity onclicklistener


【解决方案1】:

您在此处定义的 xml 文件名是 Menu.xml,在您的 java 文件中您使用的是 main 布局文件。为什么会这样?

您应该在您的 java 文件中使用布局文件 Menu.xml。只需更改布局文件名,然后尝试实现 onClick。它应该可以工作。

public void onClick(View v) {
    //TODO Auto-generate method stub
    //figure out which button was pressed
    switch (v.getId()) {
    case R.id.button1:
        //do button1 action
       System.out.println("Button1 Clicked.");
         break;
     }
   }

【讨论】:

  • 名称不同的原因是我在命名文件时犯了一个错误,具有不同的命名XML和Java文件应该不会影响它,应该吗?
  • 请告诉我您的main.xml 文件是否包含所有按钮?或者您正在尝试通过扩展布局setContentView(R.layout.main); 来使用Menu.xml 文件的按钮?
  • main.xml 包含我所有的按钮(实际上没有 menu.xml 是我将它放在我的问题中的错误)然后 java (Menu.java) 正在链接所有按钮。抱歉,如果我说的不是很清楚,我对此有点陌生。
  • 在这种情况下,您的按钮单击应该可以工作。你在 logcat 中有什么错误吗?
  • 尝试在你的 logcat 中打印一些东西。看看我已经更新了我的答案。
【解决方案2】:

您的按钮位于Menu.xml 文件中,您正在加载R.Layout.main 并在那里执行按钮功能 第一次加载正确的引用 XMl,其中定义了按钮,然后运行,如果仍有问题,则在此处共享 logcat 跟踪

【讨论】:

    【解决方案3】:

    当你根你的 android 时会发生这种情况。 生根对你的安卓手机来说是一件很棒的事情,因为它会打开你手机上的功能。但是,风险很大,所以我建议如果你要root,root风险自负!

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 1970-01-01
      • 2013-11-11
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多