So I have a simple project that has a button, and when clicked an Alert Dialog Box is supposed to open with a link to a site.
My problem is, that when the button is clicked the app crashes, and a null object reference error is given (the null reference is the link).
When the same code is used for the main activity, the link works as intended.
I'm sure it's some simple solution, but I'm a novice at android dev., so any help would be greatly appreciated.
Will provide the xml files if needed.
My problem is, that when the button is clicked the app crashes, and a null object reference error is given (the null reference is the link).
When the same code is used for the main activity, the link works as intended.
I'm sure it's some simple solution, but I'm a novice at android dev., so any help would be greatly appreciated.
Java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupPopup();
}
private void setupPopup() {
ImageView btn = findViewById(R.id.btnShowDialog);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
PopupFragment dialog = new PopupFragment();
dialog.show(manager, "MessageDialog");
}
});
}
}
Java:
public class PopupFragment extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.info_popup, null);
final TextView link = getActivity().findViewById(R.id.linkText);
link.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
link.setMovementMethod(LinkMovementMethod.getInstance());
}
});
return new AlertDialog.Builder(getActivity())
.setView(v)
.create();
}
}
Will provide the xml files if needed.