Ich bin neu in der Android-Entwicklung und mein Code funktioniert nicht so, wie er sollte. Im Folgenden wird der Text, den ich in einer Zeichenfolge namens otp1 habe, nicht aktualisiert. Ich denke, es wird die Ausnahme ausgelöst. Gibt es eine Möglichkeit zu sehen, ob dies der Fall ist? Alle Tipps, warum es nicht funktioniert, wären großartig.
package com.josephflynn.HelloWorld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
final TextView textViewToChange = (TextView) findViewById(R.string.otp1);
textViewToChange.setText("otp");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Dies ist meine main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/otp1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is my first Android Application!" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add this clickable button!" />
</LinearLayout>
Sie müssen hier eine gültige Ansichts-ID angeben:
findViewById(R.string.otp1)
Wollten Sie das tun? this?
findViewById(R.id.otp1)
Wenn Sie es nicht verstehen, veröffentlichen Sie auch Ihr Layout. Wenn Sie versuchen, die Ausnahme zu sehen, entfernen Sie das try / catch. Schauen Sie dann nach dem Absturz in LogCat (Fenster - & gt; Ansicht anzeigen - & gt; LogCat).