-1

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)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ruturaj Gole
  • 111
  • 1
  • 11
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx Apr 18 '17 at 11:05

1 Answers1

1

Don't set pass layout id directly like R.layout.contactstory instead use layout inflater to inflate layout like below code

View view = LayoutInflater.from(context).inflate(R.layout.contactstory,null);
dialog.setContentView(view);
TextView t1=(TextView)view.findViewById(R.id.textyman);
bhaumiksoni
  • 244
  • 1
  • 7