-2

I am going to add HTML code in each time in the textview, for that I have written my paragraph in my string.xml file and taken that every thing to story.java file, then I added one textview to show that text.

I don't know if it will work or not. So,first of all Is this a correct logic? If correct then, I am getting Null pointer error.

Please give solution to solve this.

This is my Story.java file.

@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);
            }
        }
    });
}
}

And this is my story.xml file

<?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>
Atef Hares
  • 4,715
  • 3
  • 29
  • 61

2 Answers2

0

You have given TextView id as textView2 in xml but yoou are using textview in java file for TextView. Either update the id in xml as textview or give proper id in java file.

Sanjeet
  • 2,385
  • 1
  • 13
  • 22
0

In the story.xml file your TextView has id textView2 but in the java code you are finding that view using id textview. Make sure you use the same id at both places.

t = (TextView) this.findViewById(R.id.textview);
Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44
  • Thank You. If I am using two different text views in two different xml files, even though it is taking as textview1 in first file and textview2 in second file(i.e automatically created id|). That's why confused. – Chitrapandi Feb 23 '17 at 01:34