Theres a image button that should bring my to a different activity but it closes my app. Any one have any ideas as to why?
ImageButton imagebtn2 = (ImageButton) findViewById(R.id.imagebtn2);
imagebtn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(Scrape.this, MainActivity.class);
startActivity(intent);
}
});
This is my code, the MainActivity is the same code just different names. Below is my manifest.
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Scrape"
android:label="#string/app_name1"
android:theme="#style/AppTheme.NoActionBar" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</activity>
This is my xml.
<ImageButton
android:id="#+id/imagebtn1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="15dp"
android:src="#drawable/got" />
And the second image buttons xml
If its on line with setOnClickListener then its NullPointerException because the IDs are different and you will get null in imageBtn2.
If you have debugger, the exception would help a lot .. thats why the exceptions are there!
Related
I built a chat application using firebase cloud messaging and firebase functions. But I have two problems currently.
1. When the app is in open and a new message comes in, the app will automatically move to the main activity of the application.
2. And secondly, I want notifications to be received only when it is in the background.
How do I achieve this?
Below is my onMessageReceived
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
}
}
private void showNotification(String name, String click_action, String title) {
Intent intent;
if (click_action.equals("Download")) {
intent = new Intent(this, Download.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else if (click_action.equals("Student_SystemsDevt")) {
intent = new Intent(this, Student_SystemsDevt.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
else {
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(name)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
My Manifest File
<application
android:name=".GTUCONLINE"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
android:screenOrientation="portrait"
android:theme="#style/MyTheme" />
<activity android:name=".MainActivity">
</activity>
<activity
android:name=".ChoiceActivity"
android:screenOrientation="portrait"
android:theme="#style/MyTheme" />
<activity
android:name=".WelcomeActivity"
android:screenOrientation="portrait"
android:theme="#style/MyTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Base.Theme.AppCompat" />
<activity
android:name=".StudentSignUp"
android:screenOrientation="portrait"
android:theme="#style/MyTheme" />
<activity
android:name=".LecturerSignUp"
android:screenOrientation="portrait" />
<activity android:name=".ProgrammeActivity" />
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".TopicActivity"
android:theme="#style/MyTheme" />
<activity android:name=".MIS" />
<activity android:name=".GB" />
<activity android:name=".FIN" />
<activity android:name=".ENGINEERINGM" />
<activity android:name=".BDM" />
<activity android:name=".SCM" />
<activity android:name=".TE" />
<activity android:name=".TM" />
<activity android:name=".BET" />
<activity android:name=".ICT" />
<activity android:name=".T" />
<activity android:name=".IT" />
<activity android:name=".AM" />
<activity android:name=".OGM" />
<activity android:name=".I" />
<activity android:name=".PEF" />
<activity android:name=".QM" />
<activity android:name=".EPM" />
<activity android:name=".PM" />
<activity android:name=".HM" />
<activity android:name=".StudentsList">
<intent-filter>
<action android:name="StudentsList" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".LecturersList" />
<activity android:name=".AdminActivity" />
<activity
android:name=".ChatActivity"
android:parentActivityName=".StudentsList" />
<activity android:name=".LecturerMainActivity" />
<activity
android:name=".Download"
android:parentActivityName=".MIS">
<intent-filter>
<action android:name="Download" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MIS_Information"
android:parentActivityName=".MIS" />
<activity android:name=".Terms" />
<activity
android:name=".Admin_Login"
android:parentActivityName=".LoginActivity" />
<activity
android:name=".Manipulation"
android:parentActivityName=".Admin_Login" />
<activity android:name=".Admins" />
<activity
android:name=".SystemsDevt"
android:parentActivityName=".MIS" />
<activity
android:name=".SysGroupChat"
android:parentActivityName=".SystemsDevt" />
<activity
android:name=".EntSystems"
android:parentActivityName=".MIS" />
<activity android:name=".DownloadEnt">
<intent-filter>
<action android:name="DownloadEnt" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".student_mis" />
<activity
android:name=".Student_SystemsDevt"
android:parentActivityName=".student_mis">
<intent-filter>
<action android:name="Student_SystemsDevt" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Student_EntSys"
android:parentActivityName=".student_mis" />
<activity
android:name=".SystemsDevt_Add"
android:parentActivityName=".SystemsDevt" />
<activity android:name=".EntSystemsAdd" />
<activity android:name=".EntSystemsInfo" />
<activity
android:name=".DatabaseSystems"
android:parentActivityName=".MIS" />
<activity android:name=".DownloadDatabaseSys" />
<activity
android:name=".Student_DatabaseSystems"
android:parentActivityName=".student_mis" />
<activity android:name=".DatabaseInfo" />
<activity
android:name=".DatabaseAdd"
android:parentActivityName=".DatabaseSystems" />
<activity
android:name=".IntroToOil"
android:parentActivityName=".GB" />
<activity
android:name=".DownloadIntro"
android:parentActivityName=".IntroToOil" />
<activity
android:name=".IntroToOilInfo"
android:parentActivityName=".IntroToOil" />
<activity
android:name=".IntroToOilAdd"
android:parentActivityName=".IntroToOil" />
<activity android:name=".student_gb" />
<activity android:name=".StudentIntro" />
<activity android:name=".MIS_students_new"
android:parentActivityName=".SystemsDevt">
</activity>
</application>
1- Actually onMessageReceived is only triggered when your application is in foreground as described enter link description here. So if you don't override onMessageReceived, you will not receive push when your app is in foreground
2- According to fcm docs, when your app is in background, onMessageReceived is not triggered but
the data payload is delivered in the extras of the intent of your launcher Activity.
It explains why your push redirects to your main activity (your launcher).
So far, you can handle the redirection from the intent.
You can also specify the activity where it sould be redirected like enter link description here:
{
"to":"some_device_token",
"content_available": true,
"notification": {
"title": "hello",
"body": "test message",
"click_action": "OPEN_ACTIVITY_1"
},
"data": {
"extra":"juice"
}
}
don't forget intent filter for the desired activity:
<intent-filter>
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Also add Tasks permission in manifest.
private boolean isAppForeground() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
if(isAppForeground()){
// Handle notification silently without displaying in notification tray
}else{
// Do your regular stuff
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
}
}
I have 2 activity in my app - first and second. Currenly my default is first.
<activity
android:name=".FirstActivity"
android:label="randomlabel"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
Now i want to change default activity so i change like this:
<activity
android:name=".FirstActivity"
android:label="randomlabel"
>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity
and app now didn't work, what is bad here ?
error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.russian.app/ru.russian.app.FirstActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Edit2:
FirstActivity
public class FirstActivity extends AppCompatActivity {
Button button10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
button10.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
goToSecond();
}
});
}
private void goToSecond() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
It looks like from your stacktrace that you forgot to bind the button in FirstActivity. You need to bind it by calling
button10 = (Button) findViewById(R.id.my_button_id);
Replace my_button_id with the id that you set for the button in your activity_first layout.
Try this:
<activity
android:name=".FirstActivity"
android:label="randomlabel">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity
it looks like you are trying to set a onClick() listener in your activity on a view that hasn't been initialised correctly.
you need to initialise the button from the inflated view.
add
button10 = (Button) findViewById(R.id.button10);
I have a Parse notification in my app. If I open my app. then it will receive the notification.
If I restart my phone. No notification will be received, until I open my app.
My question is:
How Can I initiate / run, my ParsePushBroadcastReceiver whenever the phone has been restarted. or how can I make this Receiver to be always running even if user close the app / kill the app ?
I though by adding android.intent.action.BOOT_COMPLETED would work. but it don't
Here is my manifest:
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.my.app.core.MyParseReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.my.app" />
</intent-filter>
</receiver>
<meta-data
android:name="com.parse.push.notification_icon"
android:resource="#drawable/ic_launcher" />
My Class
....
public class MyParseReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.i(TAG, "onReceive Called");
if (intent == null) {
Log.e(TAG, "Receiver intent null");
}
else {
// Parse push message and handle accordingly
Log.d(TAG, "Receiver intent data => " + intent.toString());
}
}//end onReceive
#Override
public void onPushOpen(Context context, Intent intent) {
...
I wouldn't recommend it but you could make an onBacKeyPressed() method and just make it look like the app has been killed.
manifest code for the built in search, using the searchview.
can anyone tell me please what is wrong with it?
the SearchableActivity is created in the scr -> presentation, and it is using an xml layout called search.xml. I am new to android and can't find out what is wrong with my code. when launching the search view typing a word and press on "Go" on the keyboard the debugger opens a ActivityThread.perfor.... and tells me source not found.
here is my hole manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myTV.Android"
android:versionCode="4"
android:versionName="1.12" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="12" />
<uses-feature
android:name="com.google.android.tv"
android:required="true" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="#drawable/mytv"
android:label="#string/app_name"
android:theme="#style/Theme.Transparent" >
<activity android:name="myTV.Presentation.Main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="myTV.Presentation.Player" >
</activity>
<activity android:name="myTV.Presentation.VODCatalogGenre" >
</activity>
<activity android:name="myTV.Presentation.VODCatalog" >
</activity>
<activity android:name="myTV.Presentation.HomePage" >
</activity>
<activity android:name="myTV.Presentation.Programs" >
</activity>
<activity android:name="myTV.Presentation.Episodes" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity android:name="myTV.Presentation.ChannelsListing" >
</activity>
<activity android:name="myTV.Presentation.Packages" >
</activity>
<activity android:name="myTV.Presentation.SpecialOfferContent" >
</activity>
<activity android:name="myTV.Presentation.linkedpage" >
</activity>
<activity android:name="myTV.Presentation.howtolinkdevice" >
</activity>
<activity android:name="myTV.Presentation.OnlineRegister" >
</activity>
<activity android:name="myTV.Presentation.SpecialOffer" >
</activity>
<activity android:name="myTV.Presentation.MyVOD" >
</activity>
<activity android:name="myTV.Presentation.SearchEpisodes" >
</activity>
<activity android:name=".Countries" />
<activity android:name=".Genres" />
<activity android:name=".WeeklyRecap" />
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
import myTV.Android.R;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class SearchableActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
public void doMySearch(String query){
//new Request().Search(query);
//new SearchEpisodes().Search(query);
Context context = getApplicationContext();
CharSequence text = query;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
Here is my searchable.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/SearchHint" >
</searchable>
Try adding the below in your <intent-filter>.
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
I think, your activity is not visible outside your Application.
Try android:exported="true".
On the app I am making, on the home menu screen that you get to after the splash screen, when you click one of the buttons, nothing happens. I don't know what the problem is?
Here is the src file, it implements View.OnClickListener:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bPlay:
Intent ourIntentPlay = new Intent(PartyActivity.this, Play.class);
startActivity(ourIntentPlay);
break;
case R.id.bFacts:
Intent ourIntentFacts = new Intent(PartyActivity.this, Facts.class);
startActivity(ourIntentFacts);
break;
case R.id.bInfo:
Intent ourIntentInfo = new Intent(PartyActivity.this, Info.class);
startActivity(ourIntentInfo);
break;
case R.id.bHelp:
Intent ourIntentHelp = new Intent(PartyActivity.this, Help.class);
startActivity(ourIntentHelp);
break;
}
}
And here is the manifest, inside the application tags:
<activity
android:name=".Splash"
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=".PartyActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.PARTYACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Info"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.INFO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Play"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.PLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Help"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="w.m.HELP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Thanks
Are you assigning a listener to your buttons? Do this in your onCreate() method:
findViewById(R.id.bPlay).setOnClickListener(clickListener);
If your activity implements OnClickListener, then clickListener will be this. Do this for all of your buttons.