I cannot seem to get my onSearchRequest() working. I think i have all of the coding correct, would someone be able to check please.
Manifest file
<activity android:name="SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint">
</searchable>
SearchActivity.java
public class SearchActivity extends android.app.Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if(Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, query,Toast.LENGTH_LONG).show();
}
}
}
Method thats called when the button is clicked
public void search(View view)
{
onSearchRequested();
Toast.makeText(this, "text",Toast.LENGTH_LONG).show();
}
Can anyone sport any mistakes anywhere??? as from what i can see, it should all be working perfectly.
Related
I have just gotten into Android app development and have been working on parts of my project in bits and pieces.
I first created my onboarding screens and then made my animated splash screen. However, when I run my application my splash screen does not show and instead loads the onboarding pages first. How can I fix this to make my app first transition through the splash screen before moving on the onboarding screens?
This is my SplashActivity Class code
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_SCREEN = 5000;
// Variables
Animation topAnim;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
// Animations
topAnim = AnimationUtils.loadAnimation(this,R.anim.top_animation);
// Hooks
image = findViewById(R.id.splashScreenLogo);
image.setAnimation(topAnim);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, OnboardingActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_SCREEN);
}
}
This is my OnboardingActivity Class code
public class OnboardingActivity extends AppCompatActivity {
private OnboardingAdapter onboardingAdapter;
private LinearLayout layoutOnboardingIndicators;
private MaterialButton buttonOnboardingAction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onboarding);
// Onboarding Screens
layoutOnboardingIndicators = findViewById(R.id.layoutOnboardingIndicators);
buttonOnboardingAction = findViewById(R.id.buttonOnboardingAction);
setupOnboardingItems();
final ViewPager2 onboardingViewPager = findViewById(R.id.onboardingViewPager);
onboardingViewPager.setAdapter(onboardingAdapter);
setupOnboardingIndicators();
setCurrentOnboardingIndicator(0);
onboardingViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
setCurrentOnboardingIndicator(position);
}
});
buttonOnboardingAction.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onboardingViewPager.getCurrentItem() + 1 < onboardingAdapter.getItemCount()) {
onboardingViewPager.setCurrentItem(onboardingViewPager.getCurrentItem() + 1);
}
else {
startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
finish();
}
}
});
}
}
Edit - My Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mentalhealthapp">
<application
android:allowBackup="true"
android:icon="#drawable/treen_app_logo"
android:label="treen"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity"></activity>
<activity android:name=".HomeActivity" />
<activity android:name=".SignInActivity" />
<activity android:name=".SignUpActivity" />
<activity android:name=".OnboardingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
What should I change in my Manifest File?
You need to set the Launch activity as SplashActivity in your AndroidManifest.xml file
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
please help , when i run the app, the app launch a Fatal Error im learning
just i create a instat with a call but not run, (sorry for my english)
.......................................................................
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.octa.appprueba3">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_CONTACTS" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
my code:
public class SecondActivity extends AppCompatActivity {
private EditText editPhoneText;
private ImageButton imageCallButton;
private final int PHONE_CALL_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editPhoneText = (EditText) findViewById(R.id.editTextPhone);
imageCallButton = (ImageButton) findViewById(R.id.imageCallButton1);
imageCallButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
implicito();
}
});
}
public void implicito(){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("Tel: 9999999" ));
startActivity(intent);
}
}
You may want to check whether your device can handle your intent by doing this:
PackageManager packageManager = getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else {
Log.d(TAG, "Cannot handle this intent");
}
What's more, if you are placing a call like so, there is NO need to declare the permission in your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
Because you are not directly calling someone within your app. You are actually transferring the "duty" to other apps which can handle your intent to call. This doesn't need a permission.
To directly make a call within your app, the intent should be Intent.ACTION_CALL, which needs the permission you declared.
Hope this will help.
There is no activity on your device that handles ACTION_DIAL for a Tel: scheme. Try:
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:9999999" ));
Case and whitespace are important when assembling a Uri.
Also note that even my revised Intent will not work on all devices, such as on an Android tablet that lacks a dialer.
Background
In a rather simple Android App a Settings Activity is used for just one ListPreference. When clicking the menu item in the header a new screen shows up without any preference or header.
compiledSdkVersion & targetSdkVersion = 25
minSdkVersion = 10
Code
Here is the code that I currently have.
SettingsActivity.java: Here the onCreate method is never called.
public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {
/**
* {#link PreferenceActivity#onCreate(Bundle)}
*/
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
addPreferencesFromResource(R.xml.pref_movies);
}
/**
* {#link android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange(Preference, Object)}
*/
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
return false;
}
}
pref_movies.xml: Is located in res/xml.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:title="#string/setting_sort_label"
android:key="#string/setting_sort_key"
android:entries="#array/movies_sort"
android:entryValues="#array/movies_sort_values" />
</PreferenceScreen>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.brainchest.udacity.popularmovies">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:label="#string/general_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="net.brainchest.udacity.popularmovies.MainActivity"></meta-data>
</activity>
</application>
</manifest>
onOptionsItemSelected in my Fragment: Here the intent is created and startActivity is called. The debugger runs through it, no exceptions.
/**
* {#link Fragment#onOptionsItemSelected(MenuItem)}
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// open the settings
if (item.getItemId() == R.id.action_settings) {
Intent settingsIntent = new Intent(getContext(), SettingsActivity.class);
startActivity(settingsIntent);
}
return super.onOptionsItemSelected(item);
}
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="movies_sort">
<item>Most popular</item>
<item>Highest rated</item>
</string-array>
<string-array name="movies_sort_values">
<item>0</item>
<item>1</item>
</string-array>
</resources>
something else I need to show here?
What I tried so far
Searching the web I found some common mistakes that refer either to a missing AndroidManifest.xml entry or mixing up some resource IDs. I double checked this to my best knowledge but still it doesn't work.
What did I miss?
Try wrapping the ListPreference in a PreferenceCategory like this:
<PreferenceCategory
android:key="<YOUR_KEY>"
android:title="<YOUR_TITILE>" >
<ListPreference
android:title="#string/setting_sort_label"
android:key="#string/setting_sort_key"
android:entries="#array/movies_sort"
android:entryValues="#array/movies_sort_values" />
</PreferenceCategory>
Finally found out what the issue is. I used the wrong overload. Instead of
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
addPreferencesFromResource(R.xml.pref_movies);
}
which is not called, I had to use
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_movies);
}
Now the settings are shown.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I've made a test app. The main activity has a button. If you click that button, you go to another activity.
But it just doesnt get installed on my phone. I've compiled with API 23 for my Kitkat device. I just cant build an app with two classes when I use intent.
Here is the main activity:
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("android.intent.action.GOTHAM");
startActivity(i);
}
});
}
}
The second activity:
public class gotham extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gotham);
}
}
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".gotham"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.GOTHAM" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
First of all, you should name your java classes beginning with an upper-case letter, like Gotham and not gotham.
Also in the manifest you can delete the intent filter as it is not needed at all, and you can create the intent in you MainActivity like this:
Intent intent = new Intent(this, Gotham.class);
After you've renamed your gotham activity to Gotham.
If these changes does not solve your problem, check the logcat output and also any error that eclipse produces when trying to build your app.
OK, so the problem I'm having occurs when the startGame button is pressed. The app crashes when the button is pressed. The activity is instantiated in the manifest so I'm not sure where the error is. The code for the intent is a copy of the other one (which works) so I have no clue where I went wrong.
Error Log:
02-25 14:46:51.064: E/AndroidRuntime(1261): FATAL EXCEPTION: main
02-25 14:46:51.064: E/AndroidRuntime(1261): Process: com.example.hegemony, PID: 1261
02-25 14:46:51.064: E/AndroidRuntime(1261): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hegemony/com.example.hegemony.PlayerTurn}: java.lang.NullPointerException
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hegemony"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hegemony.SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.StartScreen" >
<intent-filter>
<action android:name="com.example.hegemony.STARTSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.SetupHomeScreen" >
<intent-filter>
<action android:name="com.example.hegemony.SETUPHOMESCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.SetupPlayer"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.hegemony.SETUPPLAYER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.hegemony.PlayerTurn"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.hegemony.PLAYERTURN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Java Code of sending activity:
public class SetupHomeScreen extends Activity{
private ArrayList<Player> p = GameMaster.players;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup_home_screen);
getActionBar().hide();
updatePlayers();
Button gotoInput = (Button) findViewById(R.id.btnSetupPlayer);
gotoInput.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toInput = new Intent("com.example.hegemony.SETUPPLAYER");
startActivity(toInput);
}
});
Button startGame = (Button) findViewById(R.id.btnStartGame);
startGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toStart = new Intent("com.example.hegemony.PLAYERTURN");
startActivity(toStart);
}
});
}
public void updatePlayers(){
TextView playerList = (TextView) findViewById(R.id.playerList);
String msg = "";
for(int i=0;i < p.size();i++)
msg = msg + "\n - "+p.get(i).getName();
playerList.setText(msg);
if(p.size() >=2){
Button enable = (Button) findViewById(R.id.btnStartGame);
enable.setEnabled(true);
}
}
}
Java code of receiving activity:
public class PlayerTurn extends Activity {
final ActionBar actionBar = getActionBar();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_turn);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
};
}
}
Button startGame = (Button) findViewById(R.id.btnStartGame);
startGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toStart = new Intent(SendingActivity.this, NewActivity.class);
startActivity(toStart);
}
});
I think this is how you should start an intent. Maybe its missing a context or the "activityname" is not the right way to do it. I tried to start an activity the way you are trying and it gave me an error as well, not the same one, but it didn't work.
I'm sorry if the solution I'm trying to provide doesn't work. This is really the first time I'm trying to help someone. I hope it works.
As per the logcat error message, your activity class is failing to instantiate. Instantiation involves the allocation and assignment of any class member variables. In the case of PlayerTurn, the only one is this:
final ActionBar actionBar = getActionBar();
The call to getActionBar() throws a NullPointerException because the activity's window has not been built yet - you should call getActionBar() after setContentView() has been called in onCreate(). You can resolve this by simply moving that line of code into onCreate().
If you still want to retain this as a class member variable, declare it but don't assign it:
ActionBar actionBar;
...and then do the assignment in onCreate():
actionBar = getActionBar();