I am developing an android app and just need some help on selecting the correct text files and keeping a count of the number of clicks for buttons.
So basically I have two activity classes. The homepage of the app is stored in the MainActivity
class and the other class is known as Content
In the MainActivity
class there are three buttons:
Jokes,
Poems
and Funny Stories
Basically whichever option the user selects out of those three buttons, the content on the next page (Content
class) will display the correct passage of text relating to the choice selected.
Currently my code works for jokes when the user selects jokes and the content it displays is randomly selected from the jokes.txt
file.
MainActivity
public class MainActivity extends AppCompatActivity{
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button jokesButton = findViewById(R.id.button_jokes);
Button poemsButton = findViewById(R.id.button_poems);
Button funnyStoriesButton = findViewById(R.id.button_funny_stories);
jokesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openContentPage();
}
});
}
private void openContentPage(){
Intent intentContentPage = new Intent(MainActivity.this, Content.class);
startActivity(intentContentPage);
}
}
Content
public class Content extends AppCompatActivity{
Button backButton;
Button selectAnotherButton;
TextView contentText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
backButton = findViewById(R.id.button_back);
selectAnotherButton = findViewById(R.id.button_select_another);
contentText = findViewById(R.id.content_text);
contentText.setMovementMethod(new ScrollingMovementMethod());
setContent();
selectAnotherButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContent();
}
});
backButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
backToMainActivity();
}
});
}
private void backToMainActivity(){
Intent intentMainActivity = new Intent(this, MainActivity.class);
startActivity(intentMainActivity);
}
private void setContent(){
String text = "";
String randomJoke = "";
try {
// file to inputstream
InputStream input = getAssets().open("files/jokes.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
text = new String(buffer);
String[] jokes = text.split("###");
Random rand = new Random();
int randomIndex = rand.nextInt(jokes.length);
randomJoke = jokes[randomIndex];
}
catch (Exception e) {
System.out.println(e);
}
contentText.setText(randomJoke);
}
}
However this code needs to be manipulated so that it includes Poems and Funny Stories. Basically if the user selects Poems then it will grab the content from the poems.txt
file, if they select Funny Stories
then it will grab from the funnystories.txt
file. Also if they select the Select Another
button, it will randomly select a new entry from the correct text file. Like I said the code I have done works for jokes only, but I need to make it more dynamic so it would work for poems and funny stories too depending on which option the user selected from the homepage.
One final thing as well. I want a count of the number of times the user has clicked on either Jokes, Poems, Funny Stories from MainActivity
and also add Select Another button to the count as well.
How can this be implemented?
UPDATE:
Trying to receive the intent I receive the following error from this code:
private void setContent(){
String text = "";
String randomText = "";
String keyPageValue = getIntent().getStringExtra("keyPage");
String fileName = "";
if(keyPageValue.equals("0")){
fileName.equals("files/jokes.txt");
}
else if (keyPageValue.equals("1")){
fileName.equals("files/poems.txt");
}
else if (keyPageValue.equals("2")){
fileName.equals("files/funnystories.txt");
}
try {
InputStream input = getAssets().open(fileName);
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
text = new String(buffer);
String[] splitText = text.split("###");
Random rand = new Random();
int randomIndex = rand.nextInt(splitText.length);
randomText = splitText[randomIndex];
}
catch (Exception e) {
System.out.println(e);
}
contentText.setText(randomText);
}
Stack Trace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mima.chilltime, PID: 18747
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mima.chilltime/com.mima.chilltime.Content}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
...