App Crash Android Java OnClickListener

时间:2014-05-25 02:53:52

标签: android onclicklistener

我是Android开发的新手,并且一直在做一些练习。 我做了一些研究和调试,并相信我找到了问题的根源。 我相信这部分就在这里。 我的目标是创建两个按钮,添加1并在点击时扣除1并显示总和。

public class MainActivity extends ActionBarActivity {
int counter;
Button add,sub;
TextView Display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter=0;
    add=(Button)findViewById(R.id.bAdd);
    sub=(Button)findViewById(R.id.bSub);
    Display=(TextView)findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {         
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            Display.setText("Your total is "+ counter);

        }
    });//Take it as one liner
    sub.setOnClickListener(new View.OnClickListener() {
        //using the id of the buttons
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            Display.setText("Your total is " + counter);

        }
    });//Take it as one liner

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

但我似乎无法弄清楚出了什么问题。我非常确定问题在于onClickListener,因为它只在我实现代码时崩溃,而且我非常确定我使用了正确的id。

我把我的按钮ID放在字符串下面。我错了吗?

2 个答案:

答案 0 :(得分:2)

我不确定你的问题是什么,但我可以提出一个我认为更加一致和可靠的替代方案。

您可以在XML文件中设置等效的代码,而不是使用我个人觉得设置和使用麻烦的onClickListener。只要定义了对象并且XML文件中包含代码(例如TextViewImageView),这将起作用。它也适用于按钮。

在按钮的XML代码集中,添加以下两行代码。

android:Clickable="true"
android:onClick="methodYouWantCalled"

第一行使对象(在本例中为按钮)可单击。第二行告诉程序在单击时调用哪种方法。它将使用相应类中的引号中的名称调用该方法。只需确保您的课程设置如下:

public static void methodYouWantCalled(View view){

因此,对于您的添加按钮,您要添加的XML代码如下所示:

android:Clickable="true"
android:onClick="add"

您需要在相应的类中使用此代码:

public static void add(View view){

  counter++;
  Display.setText("Your total is "+ counter);
}

至于你把按钮ID放在字符串下面,我只想将id放入实际的XML代码中。

android:id="@+id/=yourIdHere"

这是最安全的方式,您对象的ID是引号中的内容。因此,添加按钮的最终XML代码应为:

<Button android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add One" <!-- Or whatever your text is -->
        android:Clickable="true"
        android:onClick="add"
/>

你的add方法的最终java与上面相同。您可以为每个按钮编辑这些按钮。

如果尝试此操作后应用仍然崩溃,请确保侦听器/方法中的代码正确无误。如果我们遇到崩溃时看到的logcat /错误,我们可以提供更多帮助。

希望这有帮助!

EDIT

<小时/> 您遇到的问题是您不能将整个方法放在另一个方法中。 Super.onCreate()是它自己的方法,就像

一样运行
 private static void main(String[] args){

您收到该错误的原因是您无法在那里放置方法。而不是把它放在这里:

public class yourActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);

        public static void add(View view){
            //code
            }

}   
}

把它放在这里:

public class yourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);


}   

        public static void add(View view){
            //code
            }

}

一旦它在onCreate方法之外,没有理由在任何地方调用它,因为XML代码为你做了。

至于View v / View view,只要你总是使用同一个,就没关系。

希望这有帮助!

答案 1 :(得分:0)

使用:

Display.setText("Your total is "+ Integer.toString(counter));

同样地:

Display.setText("Your total is " + Integer.toString(counter));