Extended Application can not run correctly - android

everyone. I am new to android.
Recently, I tried to learn how to extend Application, but there is
a strange problem. I have tried a lot of methods, but still can not
solve it. So, thanks in advance if anyone can help me.
The code section is like this:
package com.example.xw.myfirstapplication;
import android.app.Application;
/**
* Created by Administrator on 2016/8/26.
*/
public class MyApplication extends Application {
public static MyApplication mApplication;
#Override
public void onCreate(){
super.onCreate();
mApplication = this;
}
public static MyApplication getMyApplication(){
return mApplication;
}
}
And, Manifest.xml is like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xw.myfirstapplication"
>
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:name="com.example.xw.myfirstapplication.MyApplication"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
>
<!--<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>-->
<!--<uses-permission android:name="android.permission.INTERNET"></uses-permission>-->
<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="testNewTech.DisplayMessageActivity" />
<activity android:name="testNewTech.NotificationActivity"></activity>-->
</application>
</manifest>
But, when I run the code, something happens very quickly, like this:
In addition:
1. I tried not to use extended Application but default Application, everything is good.
I add Log.v("MainActivity", "start onCreate"); in method onCreate() of class MainActivity. But, there is nothing in logcat, so I think the method onCreate() of class MainActivity is not processed.
So, anyone has any advice? Thank you so much!

If you can see in your manifest.xml file then you can pass another class
<application android:name="com.example.xw.myfirstapplication.MyApplication"
...
...
>
just change
<application android:name="com.example.xw.myfirstapplication.Application"
...
...
>

Related

The Crosswalk framework for Android

I have a few questions about Crosswalk, I tried searching on Google, but since I don't know the exact names of some functions, I can't find anything.
Is it possible to use Crosswalk to render a online website?
If the answer to question 1 is no, Is it possible to write a HTML5+JS app, that renders an iframe, with in there a online site?
If question 1 is yes, can you post a link to a documentation or a tutorial?
Yes it is possible to do both action.
here is some help on the official web site. and here is a code that might work for you too
import android.app.Activity;
import android.os.Bundle;
import java.util.logging.Logger;
import org.xwalk.core.XWalkView;
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
Logger logger = Logger.getLogger("xwl");
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
XWalkView view = new XWalkView(this, this);
logger.severe("loadeding ...");
view.load("https://www.google.com", null);
logger.severe("loaded");
setContentView(view);
}
}
Here is the manifest file
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wxl"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher" android:hardwareAccelerated="true"
android:largeHeap="true">
<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>
</application>
</manifest>
The two permissions are needed.
The only problem I found is that it renders blurry :( on android.
Good luck.

About android.intent.action.BOOT_COMPLETED [closed]

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
I write a sample to learn BroadcastReceiver..But When I reboot my phone, the app broke down. Here is my source code and manifest.xml:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class BootCompleteReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.boot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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>
<receiver android:name=".BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You do not have a class named com.example.boot.BootCompleteReceiver, at least based on the code that you have shown in your answer.
While you have a class named BootCompleteReceiver:
It is an inner class of MainActivity, and so BootCompleteReceiver is not named com.example.boot.BootCompleteReceiver
It is a regular inner class of MainActivity (no static keyword), and so Android could not create an instance of it anyway, even if you had the right name in the manifest
Either move BootCompleteReceiver to be a regular standalone Java class in its own .java file, or make it be a public static class inside MainActivity. In the latter case, the fully-qualified class name for your manifest entry would be com.example.boot.MainActivity$BootCompleteReceiver.

Is it possible to have an Activity over a NativeActivity and still getting inputs in the NativeActivity?

I have a NativeActivity which works by itself and I try to add some SDKs ( more precisely Admob/Chartboost/Facebook ).
On the other hand I've got custom scripts which manage all the SDK only if we pass an Activity in the parameters.
My goal is to manage all of the SDKs with a custom Activity over my NativeActivity.
My questions :
Is It even possible ?
Is there a simpler solution than mine ?
So far my inputs are stucked in the Activity.
Here's my NDK class ( from the android-ndk examples ) :
public class TeapotNativeActivity extends NativeActivity {
// Some previous code
public void showUI()
{
// Some previous code
ASE_GUIActivity.pNatAct = this;
Intent startNewActivityOpen = new Intent(this, ASE_GUIActivity.class);
this.startActivity(startNewActivityOpen);
}
}
Here's my activity class :
public class ASE_GUIActivity extends Activity {
public static NativeActivity pNatAct;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
pNatAct.onTouchEvent(ev);
return super.onTouchEvent(ev);
}
}
And finally my AndroidManifest.xml :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.teapot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-feature android:glEsVersion="0x00020000"></uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:hasCode="true"
android:name="com.sample.teapot.TeapotApplication"
>
<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="com.sample.teapot.TeapotNativeActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden">
<!-- Tell NativeActivity the name of or .so -->
<meta-data android:name="android.app.lib_name"
android:value="TeapotNativeActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.littleworlds.ase.ASE_GUIActivity"
android:theme="#android:style/Theme.Translucent"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
</manifest>
As far as I know, you can not instantiate an activity inside an activity. You can only use intent mechanism, in which case you will only have one activity running at the same time. Your design choice does not seem suitable for Android environment.
Why don't you try to launch the SDK activities you mention from native side? For example, if you want to launch a Facebook login screen when user clicks a button, get the necessary input inside native activity and then using JNI calls and intent mechanism, launch the Facebook activity.

Android Google Map API:Force Close Error

I have received forced closed error message on running the sample code of Google API.Is update a project is a solution to this type of project? If so then how to update project on window.The method given here don't work on my case.
Here is code
Filename:HelloGoogleMaps.java
package com.hellogooglemaps.practices;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
public class HelloGoogleMaps extends MapActivity {
/** Called when the activity is first created. */
#Override
protected boolean isRouteDisplayed(){
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
#Override
protected boolean isLocationDisplayed(){
return true;
}
}
HelloGoogleMaps Manifest
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".HelloGoogleMapsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelloGoogleMaps" android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" />
<uses-library android:name="com.google.android.maps"/>
</application>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0_hYeORLgQUROOOr_RXN2TWG2u2pCDoBYfLCV_w"
/>
As you had mentioned in your code, You have only one Activity called HelloGoogleMaps and in your AndroidManifest you have declared two activities!! your HelloGoogleMapsActivity is useless. You are getting the error because you set HelloGoogleMapsActivity as the Launcher activity and this activity don't even exist!!
Solution:
change your AndroidManifest.xml like that and you will get your app work if you had managed to get the apikey for your map correctly:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
android:name=".HelloGoogleMaps" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"/>
</application>
After facing Force close Error many time what I have found is that these type of error is so common and come due to the undefined behavioral.Like when we declare Button and without assigning the view on it we use it on another function. It is also appear due to not handling the exception correctly. or Due to unavailable of resource your are trying to access in emulator like contact number. Best method to solve these type of problem is using log and printing some message on place where we feel confusing and using logcat.
In above case I had received force close error due to undefined behavior in HelloGoogleMapsActivity class

ServiceTestCase is not launching my service

I am attempting to write some JUnit tests for my android application. The application is a service.
I have tried several things to get the ServiceTestCase to launch the service but it fails to.
HOWEVER, when i debug the ServiceTestCase it WILL launch the service. I belive this is because the ServiceTestCase is calling setup and not giving enough time for the service to launch before it kills it...
I am not completely sure, this is the first time i have ever used junit testing for android. VERY new to this. Any suggestions on what i can do to fix this problem?
I was thinking of creating some timer loop or something, but that seems REALLY dirty. Would like a much cleaner aproach.
Android Manifest for the service.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dataservice.server"
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="dataservice.server.DataServer" android:process=":remote" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="dataservice.DataService.BIND.1" />
</intent-filter>
<intent-filter>
<action android:name= ".DataServer" />
</intent-filter>
</service>
<receiver android:name="dataservice.server.Receiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"></action>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
The ServiceTestCase i am attempting.
public class Publish extends ServiceTestCase<DataServer> {
private final static String TAG = "Publish Unit Test";
private Context mSystemContext;
public Publish() {
super(DataServer.class);
}
public Publish(Class<DataServer> serviceClass) {
super(serviceClass);
}
#Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
// this is where i am attempting to start the service.
// i have attempted other methods, but none of those worked either.
startService(new Intent(this.getContext(), DataServer.class));
}
public void testPublish() {
}
}
I was referencing one of the dependant projects incorrectly. Once this was fixed, it resolved the issue. So, check and make sure you are referencing your projects correctly if you are having the same issue.

Categories

Resources