When the myincome button is clicked, my app crashes, and i'm not sure why. My manifest looks right to me. What is causing it to crash? Most other questions similar to mine were answered by missing to include the activity on the manifest, but mine is already on there. I appreciate any feedback.
StartingActivity:
public class StartingActivity extends AppCompatActivity
{
private TextView myincome;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting);
myincome = (TextView) findViewById(R.id.myIncome);
myincome.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Start DetailActivity
Intent j = new Intent(StartingActivity.this, DetailActivity.class);
startActivity(j);
}
});
}
}
DetailActivity:
public class DetailActivity extends AppCompatActivity
{
private TextView back;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); //default
back = (TextView)findViewById(R.id.back);
//back button
back.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
}
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".StartingActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DetailActivity">
</activity>
<activity android:name=".CategoriesActivity">
</activity>
</application>
Look at Run log. It must be said something like
java.lang.NullPointerException: Attempt to invoke virtual method 'setOnClickListener()' on a null object reference
pointing at line
myincome.setOnClickListener(new View.OnClickListener()
So myincome is null because you didn't assign anything to it.
Related
Foreground service class code for calling the shutDown broadcast receiver.
public class SmsForegroundService extends Service{
private BroadcastReceiver receiver;
public static class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// do something
Log.d(TAG, "onReceive: "+intent.getAction());
if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
Log.d(TAG, "onReceive: ScreenOff");
}
}
// constructor
public MyReceiver(){
}
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
receiver = new MyReceiver();
this.registerReceiver(receiver,filter);
}
#Override
public void onDestroy() {
Log.d(TAG, "onDestroy: service destroyed");
super.onDestroy();
this.unregisterReceiver(receiver);
}
//
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG ,"service running");
final String CHANNEL_ID = "foreground Service ID";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
CHANNEL_ID,
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
.setContentText("Service is running")
.setContentTitle("Service is enabled")
.setSmallIcon(R.drawable.ic_launcher_background);
startForeground(1001, notification.build());
}
return super.onStartCommand(intent, flags, startId);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return null;
}
}
AndroidManifest file, here I added only the permission for Foreground services, I think there is no other permission is required for action_shutdown in manifest for versions greater than Oreo.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mywork.smsbroadcast">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
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/Theme.SmsBroadcast">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".SmsForegroundService">
</service>
</application>
</manifest>
MainActivity I have called the service class inside on Create method MainActivity class.
public class MainActivity extends AppCompatActivity {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(!foregroundServiceRunning())
{
Intent serviceIntent = new Intent(this, SmsForegroundService.class);
ContextCompat.startForegroundService(this,serviceIntent);
}
}
public boolean foregroundServiceRunning(){
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for(ActivityManager.RunningServiceInfo serviceInfo: activityManager.getRunningServices(Integer.MAX_VALUE)){
if(SmsForegroundService.class.getName().equals(serviceInfo.service.getClassName())){
return true;
}
}
return false;
}
}
My app to read incoming sms using broadcast receiver is working fine in many mobiles but not working in samsung j5 mobile. Can you give me proper solution for that problem.
My MainActivity.java is
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyReceiver.bindListener(new SmsListener.OTPListener() {
#Override
public void messageReceived(String messageText, String messageSender) {
Toast.makeText(MainActivity.this,"message is "+messageText+ " and sender is "+messageSender ,Toast.LENGTH_SHORT).show();
}
});
}
}
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
private static SmsListener.OTPListener mListener;
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
Bundle data = intent.getExtras();
Object[] pdus = new Object[0];
if(data != null)
{
pdus = (Object[]) data.get("pdus");
}
if(pdus != null)
{
for(Object pdu : pdus ){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdu);
String sender = smsMessage.getDisplayOriginatingAddress();
String messageBody = smsMessage.getMessageBody();
if (mListener!=null)
{
mListener.messageReceived(messageBody, sender);
break;
}
}
}
}
public static void bindListener(SmsListener.OTPListener listener) {
mListener = listener;
}
public static void unbindListener() {
mListener = null;
}
}
SmsListener.java
public interface SmsListener {
interface OTPListener{
void messageReceived(String messageText,String messageSender);
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.aaa.aaa">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.BROADCAST_SMS"
tools:ignore="ProtectedPermissions" />
<application
android:allowBackup="true"
android:icon="#drawable/b"
android:label="#string/app_name"
android:roundIcon="#drawable/b"
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>
<receiver android:priority="2147483647"
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS"
>
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
This is my application code. I want to toast the message and sender when a new message is arrived (even app is killed or destroyed), but in samsung j5 mobile, nothing happened.
You are probably running into a permission issue. The Samsung J5 is an older Samsung model, so I assume your other "mobiles" are also older phones running older android releases. Which leads me to think that you maybe running into a permission issue with the J5 as it would probably be running a newer android release. Here is how you can get permission to read SMS on newer android releases!
I pass an integer value (guide) to the next class(activity) like this:
in different activities.. first:
public void onClick(View v) {
Intent intent002a = new Intent(getApplicationContext(), GameOver002a.class);
intent002a.putExtra("guide", 11001001);
startActivity(intent002a);
}
second:
public void onClick(View v) {
Intent intent003a = new Intent(getApplicationContext(), GameOver002a.class);
intent003a.putExtra("guide", 11002001);
startActivity(intent003a);
}
and so on..
Then I receive the intent:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game_over002a);
onNewIntent(getIntent());
}
#Override
protected void onStart() {
super.onStart();
UnityAds.changeActivity(this);
UnityAds.init( this, "*******", this);
UnityAds.setDebugMode(true);
UnityAds.setTestMode(true);
TextView neinTextView = (TextView) findViewById(R.id.nein_textview002a);
if (neinTextView != null) {
neinTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Startbildschirm.class);
startActivity(intent);
}
});
}
TextView jatextview = (TextView) findViewById(R.id.ja_textview002a);
if (jatextview != null) {
jatextview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(GameOver002a.this)
.setMessage("Watch a short ad to continue at the last checkpoint?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
if (UnityAds.setZone("rewardedVideo") && UnityAds.canShow()) {
UnityAds.show();
}}
}).create().show();
}
});
}
}
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage(R.string.zurück_zum_menü_frage)
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(getApplicationContext(), Startbildschirm.class);
startActivity(intent);
}
}).create().show();
}
#Override
public void onHide() {
}
#Override
public void onShow() {
}
#Override
public void onVideoStarted() {
}
#Override
public void onVideoCompleted(String s, boolean b) {
levelAuswahl();
}
#Override
public void onFetchCompleted() {
}
#Override
public void onFetchFailed() {
TextView jatextview = (TextView) findViewById(R.id.ja_textview002a);
if (jatextview != null) {
jatextview.setText(R.string.videoladen_fail);
jatextview.setClickable(false);
}
}
#Override
public void onResume()
{
super.onResume();
UnityAds.changeActivity(this);
onNewIntent(getIntent());
}
public void levelAuswahl() {
int guide;
guide = getIntent().getIntExtra("guide",0);
if (guide == 11001001) {
Intent intent = new Intent(getApplicationContext(), Story001a.class);
startActivity(intent);
}else if (guide == 11002001) {
Intent intent = new Intent(getApplicationContext(), Story002b.class);
startActivity(intent);
}else if (guide == 11007001) {
Intent intent = new Intent(getApplicationContext(), Story004a.class);
startActivity(intent);
} else {
Intent intent = new Intent(getApplicationContext(), Startbildschirm.class);
startActivity(intent);
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
}
to choose the right activity.. EVERY PART OF THE STORY HAS HIS OWN ACTIVITY
following scenario:
I start the app and fail at the Story002a activity .. then I pass an integer value (guide) with an intent .. After watching an ad succesful the method levelAuswahl() is called (level chooser). it receives the intent and takes the right if clause and I start again at Story001.. THIS PART WORKS CORRECT
BUT.. if I fail at Story003a the value guide is passed with an intent exact as in Story002a..but this time in the the same activitie opens although I use onNewIntent()..
what do I need to change? Any ideas?
Thanks in advance!
here is the manisfest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".Startbildschirm"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Tutorial"
android:screenOrientation="landscape"/>
<activity android:name=".Support"
android:screenOrientation="landscape"/>
<activity android:name=".GameOver"
android:screenOrientation="landscape"/>
<activity android:name=".Story001a"
android:screenOrientation="landscape"/>
<activity android:name=".Story002a"
android:screenOrientation="landscape"/>
<activity android:name=".Story002b"
android:screenOrientation="landscape"/>
<activity android:name=".Story003a"
android:screenOrientation="landscape"/>
<activity android:name=".Story003b"
android:screenOrientation="landscape"/>
</application>
getIntent() returns the Intent that the Activity was started with (ie: the one that caused onCreate() to be called. If you are returning to an existing instance of GameOver, and onCreate() is not being called, then you will get the new Intent delivered in onNewIntent(). The Intent passed to onNewIntent() should contain the correct extra value.
If this is not your case, please give more details about your situation.
Instead of using
android:launchMode="singleTask" //or singleTop
is used
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
and everything works fine
use onNewIntent() like this to get your new intent
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
I am following this tutorial to make a step counter and it works well as an android project but when I make the android project a library and want to use it in unity3d, it is crashing and giving me an error class not found: exception
My unity code is as follows:
void Start ()
{
#if UNITY_ANDROID
AndroidJNI.AttachCurrentThread();
androidClass = new AndroidJavaClass("com.test.testapp.MainActivity");
#endif
}
#if UNITY_ANDROID
public void checkMyIntOnJava(string message){
if (message == "READY") {
int myInt = androidClass.CallStatic<int>("getMyInt");
guiText.text = "My Int: " + myInt;
}
}
and my android code is as follows:
public class MainActivity extends UnityPlayerActivity
implements OnDataPointListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
{
public static void callbackToUnityMethod(int num, String gameObject,String methodName){
_myInt = num;
Log.e("GoogleFit", "in callbackToUnityMethod");
UnityPlayer.UnitySendMessage(gameObject, methodName, "READY");
}
}
After making an android jar, I keep it in the plugin folder in unity3d.
Did I miss anything?
My Android Manifest file is as following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.testapp" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:label="#string/app_name">
<activity android:name="com.xxx.testapp.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="com.xxx.testapp.UnityPlayerActivity"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:label="#string/app_name" android:launchMode="singleTask" android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
</application>
</manifest>
Here's the two things I would try.
First change the following line in you AndroidManifest.xml so that the package scoping matches on the android:name property.
<activity android:name="com.xxx.testapp.MainActivity"
android:label="#string/app_name">
to
<activity android:name="com.test.testapp.MainActivity"
android:label="#string/app_name">
The other potential problem is that your class isn't being compiled because it is missing concrete implementations of the abstract methods in the parents classes and interfaces. To fix that add these implementations to MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
#Override
public void onDataPoint(DataPoint dataPoint) {
}
This is my source code, it show no error in the Ide but, it stop on the device.
I use speech to say something to the program, and the program depending of the record, show me differents answers.
I Dont know what more do. The problem apparently starts from the methodod setText.
Thank U
protected static final int RESULT_SPEACH =1;
private Button record, speak;
TextToSpeech t1;
String SpokedText;
private EditText txtView;
private EditText txtView2;
TextToSpeech t1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mai);
record = (Button) findViewById(R.id.record);
speak = (Button) findViewById(R.id.speak);
speak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
record();
}
});
}
public void record() {
Intent reconize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
reconize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
reconize.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
reconize.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.Say_something));
try {
startActivityForResult(reconize, RESULT_SPEACH);
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(), "Tu dispositivo no puede ejecutar este tipo de operaciones", Toast.LENGTH_LONG);
t.show();
}
}
#Override
public void onActivityResult (int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode,resultCode,Data);
switch (requestCode) {
case RESULT_SPEACH: {
if (resultCode == RESULT_OK && null != Data) {
SpokedText = data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
txtView2.setText(SpokedText);
setText(SpokedText);
}break;
}
}
}
public void setText(String string){
if (txtView2.equals("Good Morning")){
txtView.setText("Good Morning Sir");
}else if(txtView2.equals("I need your help")){
txtView.setText("Ok, ¿How can i help you?");
}
}
#Override
public void onInit(int status) {
if (status== TextToSpeech.SUCCESS){
int result = t1.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS", "This Lenguaje is not supported");
}else{
speakOut();
}
}
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void speakOut(){
String text = txtView.getText().toString();
t1.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
Could the problem may be in the AndroidManifest Xml?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dower.myapplication">
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".PantallaDeInicio"
android:label="#string/StarScreen"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.PICK_ACTIVITY"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<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.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Casanova"
android:label="#string/casaNova"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.PICK_ACTIVITY"></action>
<category android:name="android.intent.category.APP_BROWSER"></category>
<action android:name="android.intent.action.VOICE_COMMAND"></action>
<category android:name="android.intent.category.VOICE"></category>
</intent-filter>
</activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
The problem is that you can't equate a control with a string.
if (txtView2.equals("Good Morning")){
Should be
if (txtView2.getText().toString().equals("Good Morning")){
And you need to do the same each time you want to check the string in TextView controls