-2

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> 
+0

エラーログを追加します。 –

答えて

0

story.xmlファイルのTextViewにはtextView2というIDがありますが、JavaコードではIDがtextviewのビューが見つかります。両方の場所で同じIDを使用していることを確認してください。

t = (TextView) this.findViewById(R.id.textview); 
+0

ありがとうございます。 2つの異なるXMLビューで2つの異なるテキストビューを使用している場合、最初のファイルではtextview1、2番目のファイルではtextview2(自動的に作成されたid |)を使用しています。それが混乱している理由です。 – Chitrapandi

0

あなたがXMLでtextView2としてのTextView IDを与えているが、yoouがTextViewのためのJavaファイルにのTextViewを使用しています。 xmlのidをtextviewとして更新するか、javaファイルに適切なIDを指定してください。

関連する問題