Reputation: 97
Within my application I have set up intents to open specific activities from a Menu screen.
Each Button opens a new Activity. They are all working as intented apart from One button which doesnt do anything when clicked.
What am I doing wrong? I have checked:
-manifest
-xml
-both activities
and I cannot see a problem. Also there is no logcat error.
Full menu activity:
package com.example.brianapp;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
public class Search extends ActionBarActivity implements View.OnClickListener {
TextView instruction;
Button date;
Button game;
Button med;
Button att;
Button score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchhome);
// setting up vars
instruction = (TextView) findViewById(R.id.tvSearchHome);
date = (Button) findViewById(R.id.btnSearchHomeDate);
game = (Button) findViewById(R.id.btnSearchHomeGame);
med = (Button) findViewById(R.id.btnSearchHomeMedValues);
att = (Button) findViewById(R.id.btnSearchHomeAttValues);
game = (Button) findViewById(R.id.btnSearchHomeScore);
// set on click listeners for the buttons
date.setOnClickListener(this);
game.setOnClickListener(this);
med.setOnClickListener(this);
att.setOnClickListener(this);
game.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearchHomeDate:
Intent openDateSearch = new Intent(
"com.example.brianapp.SearchDate");
// Start activity
startActivity(openDateSearch);
break;
case R.id.btnSearchHomeGame:
// change this to make sure it opens the med screen
Intent openGameSearch = new Intent(
"com.example.brianapp.SearchGame");
// Start activity
startActivity(openGameSearch);
break;
case R.id.btnSearchHomeMedValues:
// change this to make sure it opens the med screen
Intent openMedSearch = new Intent(
"com.example.brianapp.MeditationSearchHome");
// Start activity
startActivity(openMedSearch);
break;
case R.id.btnSearchHomeAttValues:
// change this to make sure it opens the med screen
Intent openAttSearch = new Intent("com.example.brianapp.AttentionSearchHome");
// Start activity
startActivity(openAttSearch);
break;
case R.id.btnSearchHomeScore:
// change this to make sure it opens the med screen
Intent openScoreSearch = new Intent(
"com.example.brianapp.SearchScore");
// Start activity
startActivity(openScoreSearch);
break;
}// switch end
}
}
Specific Case note working:
case R.id.btnSearchHomeGame:
// change this to make sure it opens the med screen
Intent openGameSearch = new Intent(
"com.example.brianapp.SearchGame");
// Start activity
startActivity(openGameSearch);
break;
Activity that button is supposed to open:
package com.example.brianapp;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
import android.content.Intent;
public class SearchGame extends ActionBarActivity implements View.OnClickListener {
// declare vars
TextView instruction;
TextView tvResults;
DatabaseHelper db = new DatabaseHelper(this);
Button maths;
Button memory;
Button stroop;
String log;
List<Score> results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchgame);
// set up vars
instruction = (TextView) findViewById(R.id.tvSearchGame1);
tvResults = (TextView) findViewById(R.id.tvSearchGameResults1);
tvResults.setMovementMethod(new ScrollingMovementMethod());
maths = (Button) findViewById(R.id.btnSearchGameMaths);
memory = (Button) findViewById(R.id.btnSearchGameMemory);
stroop = (Button) findViewById(R.id.btnSearchGameStroop);
// set on click listener for btns
maths.setOnClickListener(this);
memory.setOnClickListener(this);
stroop.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearchGameMaths:
// just change the expression below
results = db.getMathsGame();
// showing all the scores in the database that match the query in
// Logcat (Trace code)
for (Score s : results) {
log = " Name: " + s.getName() + " Meditation: "
+ s.getMeditation() + " Max: " + s.getMax() + "Score: "
+ s.getScore() + " Date: " + s.getDate();
// Writing Contacts to log
Log.d("Details: ", log);
// hides the keyboard from screen once btn is pressed
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
tvResults.setText(listToString(results));
break;
case R.id.btnSearchGameMemory:
//results = db.getSpecificAverageAtt(avgValToSearch);
// showing all the scores in the database that match the query in
// Logcat (Trace code)
for (Score s : results) {
log = " Name: " + s.getName() + " Meditation: "
+ s.getMeditation() + " Max: " + s.getMax() + "Score: "
+ s.getScore() + " Date: " + s.getDate();
// Writing Contacts to log
Log.d("Details: ", log);
// hides the keyboard from screen once btn is pressed
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
tvResults.setText(listToString(results));
break;
case R.id.btnSearchGameStroop:
//results = db.getSpecificAverageAtt(avgValToSearch);
// showing all the scores in the database that match the query in
// Logcat (Trace code)
for (Score s : results) {
log = " Name: " + s.getName() + " Meditation: "
+ s.getMeditation() + " Max: " + s.getMax() + "Score: "
+ s.getScore() + " Date: " + s.getDate();
// Writing Contacts to log
Log.d("Details: ", log);
// hides the keyboard from screen once btn is pressed
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
tvResults.setText(listToString(results));
}
}//end of onClick()
/**
* Method used to format the output of a List
* so it is more presentable to the user
* @param list
* @return
*/
public static String listToString(List<?> list) {
String result = "";
for (int i = 0; i < list.size(); i++) {
result += "" + list.get(i) + "\n\n";
}
return result;
}
}
The corresponding parts of Manifest :
<activity
android:name="com.example.brianapp.Search"
android:label="@string/title_activity_search" >
<intent-filter>
<action android:name="com.example.brianapp.Search" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.brianapp.SearchGame"
android:label="@string/title_activity_search_game" >
<intent-filter>
<action android:name="com.example.brianapp.SearchGame" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0
Views: 89
Reputation:
you override the ref of game button:
game = (Button) findViewById(R.id.btnSearchHomeGame);
game = (Button) findViewById(R.id.btnSearchHomeScore);
you should change it to :
game = (Button) findViewById(R.id.btnSearchHomeGame);
score= (Button) findViewById(R.id.btnSearchHomeScore);
Upvotes: 2
Reputation: 86
Change this:
Intent openGameSearch = new Intent("com.example.brianapp.SearchGame");
to this:
Intent openGameSearch = new Intent(this,
com.example.brianapp.SearchGame.class);
Upvotes: 0