textviewで毎回HTMLコードを追加します。これは私の段落をstring.xml
ファイルに書き込んですべてのものをstory.java
ファイルにして、そのテキストを表示するためにtextview
を1つ追加しました。stringview.xmlファイルからtextview htmlコードを呼び出すときにヌルオブジェクト参照で仮想メソッドを呼び出そうとしています
動作するかどうかわかりません。だから、まず第一にこれは正しい論理ですか?正しい場合は、私はNull pointer error
を取得しています。
これを解決するソリューションをお願いします。
これは私のStory.java
ファイルです。
@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);
}
}
});
}
}
そして、これが私のstory.xml
ファイル
<?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>
エラーログを追加します。 –