onButtonClick 从未在源代码中使用过?

Posted

技术标签:

【中文标题】onButtonClick 从未在源代码中使用过?【英文标题】:onButtonClick is never used in source code? 【发布时间】:2016-09-15 21:55:23 【问题描述】:

我正在编写一个运行良好的简单应用程序,但我遇到的问题是,如果输入正确,我正在使用密码条件触发按钮点击。

问题是我的源代码说“onButtonClick”从未使用过,当我在应用程序中手动按下该按钮时,它突然强制关闭并崩溃。任何人都知道我做错了什么。我在源代码的开头扩展了Activity。我应该扩展AppCompatActivity吗?

public class Gvoice extends Activity implements OnClickListener

ListView lv1;
static final int check = 1111;
Button b1;
Button b_home;
EditText a1;


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gvoice);
    lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
    b1 = (Button)findViewById(R.id.GVoice);
    a1 = (EditText) findViewById(R.id.editTextHome);
    b1.setOnClickListener(this);

    //This now handles an automatic press of the bVoice button 1 second after the activity is opened
    new Handler().postDelayed(new Runnable() 
        @Override
        public void run() 
            b1.callOnClick();
        
    , 1000);



public void onButtonClick(View v) 
    if (v.getId() == R.id.BHome) 
        String str = a1.getText().toString();


        //Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'xxx' which is not case sensitive
        if (str.toLowerCase().contains("home")) 
            Intent userintent = new Intent(Gvoice.this, PocketSphinxActivity.class);
            startActivity(userintent);
         else 
            Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
        
    


public void onClick(View v)
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
    startActivityForResult(i1, check);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 

    if (requestCode == check && resultCode == RESULT_OK)
        ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
        a1.setText((String) lv1.getItemAtPosition(0));    //Get the first phrase in the first row of list view

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() 
            @Override
            public void run() 
                b_home.performClick();
            
        , 500);    //Automatically click the 'Blogin' button after 500ms
    

    super.onActivityResult(requestCode, resultCode, data);



更新:下面是 xml 文件。请注意,onButtonClick 已添加到 xml 文件中,但仍会在使用条件语句单击按钮时强制关闭应用程序:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#ececec">

<ImageView
    android:layout_
    android:layout_
    android:background="@drawable/patient_two"
    android:id="@+id/pimage"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="85dp" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Patient Name: Joe Blogs"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Cause of Injury: Car crash"
    android:id="@+id/textView2"
    android:layout_below="@+id/pimage"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Date of Birth:"
    android:id="@+id/textView3"
    android:layout_below="@+id/textView2"
    android:layout_toStartOf="@+id/textView2"
    android:layout_marginTop="25dp" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Gender:"
    android:id="@+id/textView4"
    android:layout_below="@+id/textView3"
    android:layout_alignStart="@+id/textView3" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Occupation:"
    android:id="@+id/textView5"
    android:layout_below="@+id/textView4"
    android:layout_alignStart="@+id/textView4" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Address:"
    android:id="@+id/textView6"
    android:layout_below="@+id/textView5"
    android:layout_alignStart="@+id/textView5" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medical History"
    android:id="@+id/textView7"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/textView6"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Heart attack"
    android:id="@+id/textView8"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/textView7"
    android:layout_alignStart="@+id/textView6" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Arthritis"
    android:id="@+id/textView9"
    android:layout_below="@+id/textView8"
    android:layout_alignStart="@+id/textView8" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Tests Completed"
    android:id="@+id/textView10"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/textView9"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="X-Ray"
    android:id="@+id/textView11"
    android:layout_below="@+id/textView10"
    android:layout_alignStart="@+id/textView9"
    android:layout_marginTop="15dp" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="..."
    android:id="@+id/textView12"
    android:layout_below="@+id/textView11"
    android:layout_alignStart="@+id/textView11" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Tests Due"
    android:id="@+id/textView14"
    android:layout_below="@+id/textView12"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="..."
    android:id="@+id/textView15"
    android:layout_below="@+id/textView14"
    android:layout_alignStart="@+id/textView12"
    android:layout_marginTop="15dp" />

<ListView
    android:layout_
    android:layout_
    android:id="@+id/lvVoiceReturn1"
    android:textColor="@color/white"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_
    android:layout_
    android:text="Enter"
    android:id="@+id/Blogin1"
    android:onClick="onButtonClick"
    android:layout_alignParentBottom="true"
    android:layout_toStartOf="@+id/bVoice1" />

<Button
    android:layout_
    android:layout_
    android:text="Speak"
    android:id="@+id/bVoice1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_
    android:layout_
    android:id="@+id/TFusername1"
    android:layout_alignParentStart="true"
    android:hint="Speech to Text" />

【问题讨论】:

你在按钮 xml 中添加了这个 android:onClick="onButtonClick" 吗? @N MC 你能粘贴错误日志吗 发布您的XML 代码。 哦,如果是这样的话,那我就太傻了。让我在比赛前检查一下,然后回复你 好的,我已将 onButtonClick 添加到 xml 文件中,但应用程序仍然关闭。 【参考方案1】:

你可以4个方法处理按钮点击:

方法一:

public class Mtest extends Activity 

  Button b1;
  public void onCreate(Bundle savedInstanceState) 
    ...
   Button b1 = (Button) findViewById(R.id.b1);
   b1.setOnClickListener(myhandler1);
     ...
  
  View.OnClickListener myhandler1 = new View.OnClickListener() 
    public void onClick(View v) 
      // it was the 1st button
    
  ;


方法二:

class MTest extends Activity implements OnClickListener 
    public void onCreate(Bundle savedInstanceState) 
    ...
    Button b1 = (Button) findViewById(R.id.b1);

    b1.setOnClickListener(this);
      ...
    

    @Override
    public void onClick(View v) 

    

xml 和 android:onClick="HandleClick" 中的方法 3:

<Button
    android:id="@+id/button2"
    android:layout_
    android:layout_
    android:onClick="HandleClick" />



public class MTest extends Activity 
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    public void HandleClick(View view) 

    

方法四:

public class MTest extends Activity 
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         Button b1 = (Button) findViewById(R.id.b1);
        b1.setOnClickListener(new OnClickListener() 
            public void onClick(View view) 
                // do stuff
            
        );

    

【讨论】:

【参考方案2】:

问题在于最初没有在 xml 文件中添加 onButtonClick 并且也没有在 java 文件中正确分配按钮标签。

    lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
    b1 = (Button)findViewById(R.id.GVoice);
    a1 = (EditText) findViewById(R.id.editTextHome);
    c1 = (Button)findViewById(R.id.BHome);

    b1.setOnClickListener(this);

以下解决了问题,现在一切正常。希望这可以在将来对其他人有所帮助

【讨论】:

以上是关于onButtonClick 从未在源代码中使用过?的主要内容,如果未能解决你的问题,请参考以下文章

SonarQube:从未计算过新代码的覆盖率

为啥从未执行过的 Swift 3 代码会引发运行时错误?

如何在 WPF 中的按钮上使用 click 方法? CS8321:声明了局部函数但从未使用过

Firebase OnTokenRefresh() 从未调用过?

onLocationChanged 从未调用过?

从未使用过捕获自我