Before any of you tell me that this has been answered many times before, let me clarify most of those who've asked this question either gave the wrong view id or set a different ContentView than where the TextView was located. I have already used this function in a different where it worked fine. I've tried using a string literal in the TextView.setText(), but it was futile.
contactstory.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textyman"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="100dp"
android:text="TextView" />
<Button
android:id="@+id/close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close" />
</LinearLayout>
Function in java file
public void ShowStory(int pos)
{
final Dialog dialog = new Dialog(this);
dialog.setTitle(srcnames[pos]);
dialog.setContentView(R.layout.contactstory);
String username=getIntent().getStringExtra("username");
LoginDataBaseAdapter loginDataBaseAdapter=new
LoginDataBaseAdapter(this);
loginDataBaseAdapter.open();
String story=loginDataBaseAdapter.GetStory(pos,username);
TextView t1=(TextView)findViewById(R.id.textyman);
Button btnend=(Button)findViewById(R.id.close);
if(TextUtils.isEmpty(story)){
t1.setText(username+" hasn't added any story for this song");
}
else {
t1.setText(story); //Exception is thrown here
}
btnend.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
Logcat
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.sherry.escuchame.ContactInfo.ShowStory(ContactInfo.java:157)
at
com.example.sherry.escuchame.ContactInfo.onContextItemSelected
(ContactInfo.java:140)
at android.app.Activity.onMenuItemSelected(Activity.java:2660)
at android.support.v4.app.FragmentActivity.onMenuItemSelected
(FragmentActivity.java:408)
at
android.support.v7.app.AppCompatActivity.onMenuItemSelected
(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected
(WindowCallbackWrapper.java:113)