Ich schreibe eine einfache App, die gut funktioniert, aber das Problem, das ich habe, ist, dass ich eine Passwort-Bedingung verwende, um einen Knopfklick auszulösen, wenn richtig eingegeben.onButtonClick wird nie im Quellcode verwendet?
Das Problem ist, dass mein Quellcode sagt "OnButtonClick" wird nie verwendet und wenn ich manuell auf diese Schaltfläche in der App drücken, wird es plötzlich zwangsweise geschlossen und stürzt ab. Jeder weiß, was ich falsch mache. Ich erweitere Activity
, am Anfang des Quellcodes. Sollte ich erweitern 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);
}
}
Update: Unten ist die XML-Datei. Bitte beachten Sie, dass onButtonClick
hat zu der XML-Datei hinzugefügt worden, aber es immer noch Kraft schließt die App, wenn die Taste, um die Bedingungsanweisung geklickt wird unter Verwendung von:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ececec">
<ImageView
android:layout_width="100dip"
android:layout_height="100dip"
android:background="@drawable/patient_two"
android:id="@+id/pimage"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="@+id/textView12"
android:layout_below="@+id/textView11"
android:layout_alignStart="@+id/textView11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="150dp"
android:layout_height="50dp"
android:id="@+id/lvVoiceReturn1"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter"
android:id="@+id/Blogin1"
android:onClick="onButtonClick"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/bVoice1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
android:id="@+id/bVoice1"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/TFusername1"
android:layout_alignParentStart="true"
android:hint="Speech to Text" />
Haben Sie diese Android: onClick = "onButtonClick" in Ihrer Schaltfläche xml? – Masum
@N MC Können Sie bitte Fehlerprotokolle einfügen – Qasim
Geben Sie Ihren 'XML' Code ein. –