App launches normally. When I try to minimize it and reopen through the recent apps history, again it works fine. But when I minimize and try to re open it with the app icon, it crashes.
MainActivity.java
public class MainActivity extends AppCompatActivity implements ResetPasswordDialog.Listener {
public static final String TAG = MainActivity.class.getSimpleName();
private LoginFragment mLoginFragment;
private ResetPasswordDialog mResetPasswordDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
loadFragment();
}
}
private void loadFragment(){
if (mLoginFragment == null) {
mLoginFragment = new LoginFragment();
}
getFragmentManager().beginTransaction().replace(R.id.fragmentFrame,mLoginFragment,LoginFragment.TAG).commit();
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String data = intent.getData().getLastPathSegment();
Log.d(TAG, "onNewIntent: "+data);
mResetPasswordDialog = (ResetPasswordDialog) getFragmentManager().findFragmentByTag(ResetPasswordDialog.TAG);
if (mResetPasswordDialog != null)
mResetPasswordDialog.setToken(data);
}
#Override
public void onPasswordReset(String message) {
showSnackBarMessage(message);
}
private void showSnackBarMessage(String message) {
Snackbar.make(findViewById(R.id.activity_main),message, Snackbar.LENGTH_SHORT).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<!--singleTask => flag of launchMode -->
<application
android:name="io.github.froger.xinger.InstaMaterialApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="io.github.froger.xinger.ui.activity.MainActivity"
android:launchMode="singleTop"
android:theme="#style/AppTheme.LoginRegister"
android:screenOrientation="portrait">
<intent-filter>
<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" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="learn2crack"
android:scheme="http" />
</intent-filter>
</activity>
<activity
android:name="io.github.froger.xinger.ui.activity.DashboardActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name="io.github.froger.xinger.ui.activity.CommentsActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.TransparentActivity" />
<activity
android:name="io.github.froger.xinger.ui.activity.UserProfileActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.TransparentActivity"
android:launchMode="singleInstance" />
<activity
android:name="io.github.froger.xinger.ui.activity.TakePhotoActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.TransparentActivity" />
<activity
android:name="io.github.froger.xinger.ui.activity.PublishActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme"
android:windowSoftInputMode="stateHidden">
</activity>
</application>
Error
04-01 00:51:26.015 25525-25525/io.github.froger.instamaterial D/AndroidRuntime: Shutting down VM
04-01 00:51:26.016 25525-25525/io.github.froger.instamaterial E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.github.froger.instamaterial, PID: 25525
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference
at io.github.froger.xinger.ui.activity.MainActivity.onNewIntent(MainActivity.java:44)
at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1245)
at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1257)
at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2804)
at android.app.ActivityThread.performNewIntents(ActivityThread.java:2816)
at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2825)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
The stack trace clearly indicates that the call to intent.getData() at line 44 of your MainActivity is returning a null. You should check for the null, and not try to interrogate the Intent if it is null.
You could store string data in a static variable, and then check to see if it's null. If it's null use the static data. I know static variables are frowned upon and you should address the root problem, but this will fix your immediate problem.
static String yourStaticString;
...
String data;
if (intent.getData()==NULL)
{
data = yourStaticString;
}
else
{
if (intent.getData().getLastPathSegment()!=NULL)
{
data = intent.getData().getLastPathSegment();
yourStaticString = data;
} else return;
}
Related
I've found some opensource document scanner app, first (default) "DocumentScannerActivity" of this app is a camera for capturing images of documents for further processing. Works good, without any error.
Then I've add my own (new) "MainActivity" as default with button inside, with onclick listener that opens "DocumentScannerActivity":
scanActivityBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DocumentScannerActivity.class);
startActivity(intent);
}
});
This code opens "DocumentScannerActivity", but after pressing on "Capture image" application crashes with error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at az.deep.archiverabitabank.DocumentScannerActivity.saveDocument(DocumentScannerActivity.java:1042)
at az.deep.archiverabitabank.DocumentScannerActivity.saveDocument(DocumentScannerActivity.java:1129)
at az.deep.archiverabitabank.ImageProcessor.processPicture(ImageProcessor.java:161)
at az.deep.archiverabitabank.ImageProcessor.handleMessage(ImageProcessor.java:91)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.os.HandlerThread.run(HandlerThread.java:65)
Here is the problematic part of code (app tries to save captured image to storage) in DocumentScannerActivity:
Uri fileUri = null;
if (intent.getAction().equals("android.media.action.IMAGE_CAPTURE")) {
fileUri = ((Uri) intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT));
Log.d(TAG, "intent uri: " + fileUri.toString()); //LINE 1042, where error occurs
try {
fileName = File.createTempFile("onsFile", ".jpg", this.getCacheDir()).getPath();
} catch (IOException e) {
e.printStackTrace();
return;
}
isIntent = true;
}
Here is the related cut from AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
...
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DocumentScannerActivity"
android:label="#string/title_activity_document_scanner"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/FullscreenTheme.NoActionBar">
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What I'm doing wrong, how to prevent this error?
I'm developing an app that should detect NFC tags.
My problem is that my activity reopens every time the app scans a tag. It should just open when the app is closed. But when it is active, I just want the data from the tag.
Manifest:
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.corinna.nfc_testapp" >
<uses-permission android:name="android.permission.NFC" />
<uses-sdk android:minSdkVersion="14"/>
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<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"
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>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
</manifest>
Activity onCreate & handleIntent
rotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Fragment ReadOrWrite
fRead = (Fragment_Read) Fragment.instantiate(getApplicationContext(), Fragment_Read.class.getName(), null);
Fragment_ReadOrWrite fReadOrWrite = (Fragment_ReadOrWrite) Fragment.instantiate(getApplicationContext(), Fragment_ReadOrWrite.class.getName(), null);
fReadOrWrite.setFragment_Read(fRead);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.id_activity_main, fReadOrWrite);
fragmentTransaction.commit();
//NFC
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
String type = intent.getType();
if (MIME_TEXT_PLAIN.equals(type)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefReaderTask ndefReader = new NdefReaderTask(fRead);
ndefReader.execute(tag);
} else {
System.out.println("Wrong mime type: " + type);
}
}
}
Just use launchMode="singleTop" in your main activity manifest.
This will ensure that if your activity is at the top of your task stack, it will not be recreated.
Be aware that onCreate is no longer called in this case, so if you want to read the content from intent, you need to override the onNewIntent Activity method.
I have a Uri scheme which is pretty standard. My manifest is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enayet.minigma"
android:versionCode="11"
android:versionName="1.5.1" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".multiscreen"
android:label="#string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
<data android:scheme="http" android:path="minigma"/>
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings" >
</activity>
</application>
<uses-sdk
android:maxSdkVersion="19"
android:minSdkVersion="15"
android:targetSdkVersion="19" />
</manifest>
and in my main activity (this is the activity I want the Uri to go to with the information I parse from the Uri) I have a getIntent() in the OnCreate function. The problem is, even though I have an if statement which guards against this, Android gives me the following error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.enayet.minigma/com.enayet.minigma.multiscreen}: java.lang.NullPointerException
This is my OnCreate function:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiscreen);
Intent intent = getIntent();
Uri data = intent.getData();
if (data != null) {
List<String> params = data.getPathSegments();
String message = params.get(0);
EditText msg = (EditText) findViewById(R.id.messageEdit);
if (message != null) msg.setText(message);
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
Im trying to make an application with Google Cloud Messaging and i followd all the exact steps for implementing GCMclient on developer page, code just seems to crash on launch. Is there any fault in activity main? or some logical flaw of how android sets up its application on creation? im new to android programming.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iotproj.clandestine"
android:versionCode="1"
android:versionName="1.0" >
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.iotproj.clandestine.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>
<receiver
android:name="GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.iotproj.clandestine" />
</intent-filter>
</receiver>
<service android:name="GcmIntentService" />
</application>
</manifest>
public class MainActivity extends Activity {
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;TextView mDisplay;
Button mButton;
GoogleCloudMessaging gcm;
Context appContext = getApplicationContext();
// this is quite important dude
String registrationId = null;
String SENDER_ID = "xxxxxxxxx";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDisplay = (TextView) findViewById(R.id.pool);
mButton = (Button) findViewById(R.id.getid);
// Check device for Play Services APK.
if (!checkPlayServices()) {
mDisplay.setText("device not supproted !"); button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
gcm = GoogleCloudMessaging.getInstance(appContext);
try{
registrationId = gcm.register(SENDER_ID);
}
catch(IOException e){
mDisplay.setText(e.getMessage());
}
}
});
}`
Register your broadcast receiver like this way.
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.iotproj.clandestine" />
</intent-filter>
</receiver>
for Service
<service android:name=".GCMIntentService" />
I've been struggling with this error for a lot and I just gave up. Every time I try to send a message using GCM this error appears on LogCat. What I'm failing to do? I've followed Android examples to set up GCM Notifications.
This is the LogCat Error
Edit: The message actually gets through but I don't think this error is normal.
08-12 17:13:15.888: W/GTalkService(2237): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.testing.encuesta] (has extras) }
AndroidManifest.xml
<permission android:name="com.testing.encuesta.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.testing.encuesta.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.testing.encuesta.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=".Inicio">
</activity>
<receiver android:name=".GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.testing.encuesta" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.testing.encuesta" />
</intent-filter>
</receiver>
My class GCMBroadcastReceiver
public class GCMBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
String action=intent.getAction();
if(action.equals("com.google.android.c2dm.intent.REGISTRATION"))
{
String registrationID=intent.getStringExtra("registration_id");
Log.d("ID",registrationID);
String error=intent.getStringExtra("error");
String unregistered=intent.getStringExtra("unregistered");
}
else if(action.equals("com.google.android.c2dm.intent.RECEIVE"))
{
String data1=intent.getStringExtra("data1");
String data2=intent.getStringExtra("data2");
Toast.makeText(context, data1, Toast.LENGTH_LONG);
}
} catch (Exception e) {
Log.d("Error", "error en C2DM"+e.toString());
}
}
Fixed, you just need to add setResultCode(Activity.RESULT_OK); at the end of the onReceive(); method