Ich werde HTML-Code jedes Mal in der Textansicht hinzufügen, dafür habe ich meinen Absatz in meine string.xml
Datei geschrieben und genommen dass jede Sache zu story.java
Datei, dann fügte ich eine textview
hinzu, um diesen Text zu zeigen.Versuch, virtuelle Methode auf eine Null-Objektverweis beim Aufrufen von Textview HTML-Code aus string.xml-Datei
Ich weiß nicht, ob es funktioniert oder nicht. Also, vor allem Ist das eine richtige Logik? Wenn es dann richtig ist, bekomme ich Null pointer error
.
Bitte geben Sie eine Lösung, um dies zu lösen.
Dies ist meine Story.java
Datei.
@SuppressWarnings("deprecation")
public class story extends AppCompatActivity {
Button next;
Button prev;
TextView t;
int count=0;
int[] stories = {
R.string.firststory,
R.string.story2,
R.string.story3,
R.string.story4,
R.string.story5,
R.string.story6,
R.string.story7,
R.string.story8,
R.string.story9,
R.string.story10,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.story);
next = (Button) this.findViewById(R.id.next);
prev = (Button) this.findViewById(R.id.prev);
t = (TextView) this.findViewById(R.id.textview);
t.setText(Html.fromHtml(getString(stories[0])));
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
count=count+1;
if(count<=10) {
t.setText(Html.fromHtml(getString(stories[count])));
}
else
{
Toast toast=Toast.makeText(getApplicationContext(),"This is Last story",Toast.LENGTH_SHORT);
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
count=count-1;
if(count>=0)
{
t.setText(Html.fromHtml(getString(stories[count])));
}
else{
Toast toast=Toast.makeText(getApplicationContext(),"This is first story",Toast.LENGTH_SHORT);
}
}
});
}
}
Und das ist meine story.xml
Datei
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<Button
android:text="previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prev"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
fügen Sie Fehlerprotokoll –