我对onClick事件方法的方法没有做任何事情

时间:2015-11-27 18:51:50

标签: java android onclick

我正在开发一个Android项目

  1. MainActivity上创建一个方法,如下所示:

    public void diagnose(View View) {
    
        System.out.println("%%%%%%%%%%%%%%%%% INSIDE DIAGNOSE %%%%%%%%%%%%%%");
    
        String PTCondition = " ";
    
        int row, col, count, percentageOfLikelihood, maxPercent = 0;
    
        //To store the name of each condition and its percentage of likelihood
        String[][] percentPerCondition = new String[8][];
    
        percentPerCondition[0][0] = "Rotator Cuff Strain";
        percentPerCondition[1][0] = "Coracoid Impingement";
        percentPerCondition[2][0] = "Supraspinatus Impingement";
        percentPerCondition[3][0] = "Bicipital Tendonitis";
        percentPerCondition[4][0] = "Glenohumeral Dislcation";
        percentPerCondition[5][0] = "Clavicular Fracture";
        percentPerCondition[6][0] = "Labral Tear";
        percentPerCondition[7][0] = "SubAcromial Bursitis";
    
        //Store binary sequence of Special Tests that correspond to the conditions above
        int[][] STValuesPercondition = new int[][] {
            {
                1, 0, 1, 0, 1
            }, {
                0, 1, 0, 1, 1
            }, {
                0, 1, 1, 0, 0
            }, {
                1, 1, 0, 0, 1
            }, {
                0, 0, 1, 1, 1
            }, {
                1, 1, 0, 1, 0
            }, {
                1, 0, 1, 1, 0
            }, {
                0, 1, 1, 1, 0
            }
        };
    
        //Compare the binary sequence in PTFindings to all conditions
        for (row = 0; row < STValuesPercondition.length; row++) {
            count = 0;
            for (col = 0; col < STValuesPercondition.length; col++) {
                if (STValuesPercondition[row][col] == PTFindings[row]) count++;
            }
    
            //Calculate percentage of likelihood
            percentageOfLikelihood = (count / 5) * 100;
            if (percentageOfLikelihood > maxPercent) {
                maxPercent = percentageOfLikelihood;
                PTCondition = percentPerCondition[row][0];
            }
    
            System.out.println("&&&&&&& Inside diagnosis: " + PTCondition);
            //Store percentage as a String in the percentPerCondition 2D array; may need it later
            percentPerCondition[row][1] = Integer.toString(percentageOfLikelihood);
        }
    
        //Point to the TextView where data to be displayed - in layout file (displaydx)
        DxTextView = (TextView) findViewById(R.id.dx1);
    
        //Populate the textView with the data from
        DxTextView.setText(PTCondition);
    
        //Display the PTCondition on the activity
        String passPTCondition = "";
    
        Intent i = new Intent(getApplicationContext(), DisplayDxActivity.class);
        i.putExtra(passPTCondition, PTCondition);
        startActivity(i);
    
    } //public void diagnose(View View)
    
  2. 这是我的布局文件;看起来这个方法(诊断(View View))没有被调用。我的diagnose()方法在onCreate()中定义 点击诊断时没有任何反应? !!!!

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Generate Possible Diagnoses"
        android:id="@+id/SubmitButton"
        android:textColor="#ffff6949"
        android:background="#ffffffff"
        android:textStyle="bold"
        android:onClick="diagnose" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/dx1"
        android:layout_gravity="center_horizontal" />
    

1 个答案:

答案 0 :(得分:1)

除了视图参数名称中的代码约定错误外,您的代码似乎是正确的。

确保使用onCreate方法夸大布局,并调用diagnose方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_main_layout);
    // Where your_main_layout is the one who has the android:onClick="diagnose"
    // Rest of the code...
}

以防万一,说{4}已在API级别4中引入,因此请确保您的API目标符合该目标。

编辑:

作为另一项测试,您可以将android:onClick侦听器添加到您的活动中,并通过View id将所有onClick事件添加到您的MainActivity.java中来获取所有onClick事件:

View.OnClickListener

@Mike已经检测到@Coeus,将public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_main_layout); findViewById(R.id.SubmitButton).setOnClickListener(this); } .... @Override public void onClick(View v) { switch (v.getId()) { case R.id.SubmitButton: diagnose(v); break; default: break; } } 方法移出diagnose并使其成为MainActivity类方法。否则,当Android查找使用反射在布局中找到的onCreate方法时,它将找不到任何内容。