I got a problem when I navigate between two activities, it shows me error and I don't know what is the problem. I am very sure that my code is correct, because it just simple Intent navigate by on click Button.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one). Note that both activity has background image.
Fist Activity
public class firstActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);//has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(firstActivity.this, secondActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
Second Activity
public class secondActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);// has a background img and one button
}
public void nextPage(View view){
Intent StartNewActivity = new Intent(secondActivity.this, thirdActivity.class);
startActivity(StartNewActivity);
overridePendingTransition(R.layout.slide_in_up, R.layout.slide_out_up);
}
}
This is the error message
Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
Also, I did not use any ripple drawable in my app.Even though I don't know what does it mean?
Thanks,
Something you have wants to to find a refwrence to that ripple component, you need to find out what.
Otherwise, you can try to make sure you have added a reference to the support.v7.widget in the second activity and see if the exception goes away.
Aside from that, we would need to see more code to help further.
When I Press the button to go to the next activity it returns me to the fist activity (not the desire one)
It means that your app crashes when loading your new activity, so it comes back the first one.
Check your activity layout, style configuration => clean your project => Run again.
Hope it can help.
I solved my problem by resizing the background images of the activities, and I added this extra attribute in the manifests file
<application
android:largeHeap="true" >
</application>
I am working on an android app that needs to load a UnityPlayer instance in an activiy, using code from the following forum post as a guide:
http://forum.unity3d.com/threads/98315-Using-Unity-Android-In-a-Sub-View .
Initially the application is correctly displaying the UnityPlayer inside an activity called "UnityActivity.java".
The problem starts when the user navigates back to the MainActivity (by either pressing the hardware back button or clicking on the ActionBar back button) and then tries to re-open the UnityActivity - in which case a black screen is shown instead of the UnityPlayer. A user in the forums suggested forwarding the onPause and onResume lifecycle events to the UnityPlayer, as shown in the code bellow. When doing that, however, the following errors show up and the app crashes.
This is logged when navigating to the UnityActivity for the first time:
W/libc(21095): pthread_create sched_setscheduler call failed: Operation not permitted
This error is logged when clicking the back button:
W/Choreographer(20963): Already have a pending vsync event. There should only be one at a time.
This error is logged when navigating to the UnityActivity for the second time:
A/libc(21095): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 21176 (Thread-5073)
...at which point I get kicked out of the application.
Code
This is an excerpt of the main activity MainActivity.java :
public void startUnityActivity(View view) {
Intent intent = new Intent(this, UnityActivity.class);
startActivity(intent);
}
This is an excerpt of the Unity activity UnityActivity.java :
public class UnityActivity extends ActionBarActivity {
UnityPlayer m_UnityPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unity);
m_UnityPlayer = new UnityPlayer(this);
int glesMode = m_UnityPlayer.getSettings().getInt("gles_mode", 1);
m_UnityPlayer.init(glesMode, false);
FrameLayout layout = (FrameLayout) findViewById(R.id.unityView);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
layout.addView(m_UnityPlayer, 0, lp);
m_UnityPlayer.windowFocusChanged(true);
m_UnityPlayer.resume();
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
m_UnityPlayer.windowFocusChanged(hasFocus);
}
#Override
public void onPause() {
super.onPause();
m_UnityPlayer.pause();
}
#Override
public void onResume() {
super.onResume();
m_UnityPlayer.resume();
}
This is how the activities are described in the manifest ../AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.package.example.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.package.example.UnityActivity"
android:label="#string/title_activity_unity"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:parentActivityName="com.package.example.MainActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
</application>
This is how the layout of the UnityActivity is defined ../res/layout/activity_unity.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.package.example.UnityActivity"
tools:ignore="MergeRootFrame" >
<FrameLayout
android:id="#+id/unityView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</FrameLayout>
I'd be thankful for any tips and solutions pointing me in the right direction.
Ok, easy things first
W/libc(21095): pthread_create sched_setscheduler call failed: Operation not permitted
There is nothing you can do about it. You even get this when you compile directly from Unity for Android, so it's a problem inside the engine.
Basic Setup
The guide you linked is pretty outdated. You no longer need to copy files from various locations to create a simple Android project.
Create a Android project by setting Build Settings -> Android -> Google Android project
You now have a complete package ready to import into Eclipse or Android Studio
Compile and deploy
Using UnityPlayer in a subactivity
The class UnityPlayerNativeActivity in your new Android project shows you how to setup the UnityPlayer and what events you need to forward. Here is the version used by Unity 4.3.4
package de.leosori.NativeAndroid;
import com.unity3d.player.*;
import android.app.NativeActivity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class UnityPlayerNativeActivity extends NativeActivity
{
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// UnityPlayer.init() should be called before attaching the view to a layout - it will load the native code.
// UnityPlayer.quit() should be the last thing called - it will unload the native code.
protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGB_565);
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
View playerView = mUnityPlayer.getView();
setContentView(playerView);
playerView.requestFocus();
}
protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.
protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.onKeyMultiple(event.getKeyCode(), event.getRepeatCount(), event);
return super.dispatchKeyEvent(event);
}
}
Although UnityPlayerNativeActivity extends NativeActivity you can still extend from ActionBarActivity instead without any problems as far as I can tell. At least it worked during my experiments.
The most important part you are missing is the call to mUnityPlayer.quit() during onDestroy(). Trying to create a new instance of UnityPlayer while the old one is still running will lead to crashes, hanging activities and endless suffering.
Unexpected behavior of mUnityPlayer.quit()
Fixing that you may be surprised that now your whole App simply closes when you return from your UnityActivity. mUnityPlayer.quit() will kill the process it is running inside. Not a single method will execute after calling mUnityPlayer.quit(), not even the onDestroy() method will finish.
The path to victory is to start your UnityActivity as a new process by adding the parameter android:process=":UnityKillsMe to your activty inside your AndroidManifest.xml.
In your case it would look like this
<activity
android:name="com.package.example.UnityActivity"
android:label="#string/title_activity_unity"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:process=":UnityKillsMe"
android:parentActivityName="com.package.example.MainActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
I'm not sure about the parameter unityplayer.ForwardNativeEventsToDalvik... The project created in the beginning sets it to false and the official (outdated) documentation mentions
Since touch/motion events are processed in native code, Java views would normally not see those events. There is, however, a forwarding mechanism in Unity which allows events to be propagated to the DalvikVM.
In my small example project I could not see a difference
The road ahead
You need to find a workflow to integrate your development with Unity with the Android project or vice versa. Exporting again with Unity would conflict with the changes you made in your Android project, so you would need to export into a separate folder and link to the Unity part from your Android project.
According to the aforementioned documentation you may be able to integrate your compiled Android classes and AndroidManifest.xml as plugins into Unity.
The resulting .class file(s) should be compressed into a .jar file and placed in the Assets->Plugins->Android folder. Since the manifest dictates which activity to launch it is also necessary to create a new AndroidManifest.xml. The AndroidManifest.xml file should also be placed in the Assets->Plugins->Android folder.
Good luck!
Question is two year ago, but I still struggled to find a detailed guide about it.
So I wrote one
The main Idea is to take the Unity project and use it as a library in the native android app.
I hope this guide will help someone.
well, i dont have clear answer for this
but i have found couple of interesting psots
1-
pthread_create warning on android
talks about W/libc(21095): pthread_create sched_setscheduler call failed: Operation not permitted
the post starts with After calling pthread_create function I receive next message:
i quote from the post:
This error means, that the process trying to create the thread hasn't
the appropriate privileges to set the scheduling priorty as specified.
and the post marked as answer have some good info to. have a look there
but on your code you are NOT calling pthread_create() -- [1]
2-
Meaning of Choreographer messages in Logcat
W/Choreographer(20963): Already have a pending vsync event. There should only be one at a time
i quote
Choreographer lets apps to connect themselves to the vsync, and
properly time things to improve performance.
and
Yes, I am. I understand the Choreographer is probably the component
that handles animations and when it doesn't get enough cpu cycles, it
skips some frames and outputs this debug message
so this is related to animation on UI ---[2]
so?
based on [1] and [2]: its an issue with the animation generated by Unity,
and its not under your control,
my own conclusion and opinion is:
this is a Bug at the Unity code, that need to be fixed by Unity ? maybe?
sorry, if not helping but i have tried.
i have never worked with Unity, but i tried to get conclusion from some other posts.
good luck
You can see this link
Integrate Unity3d view into Android activity
public class UnityPlayerNativeActivity extends NativeActivity
{
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// UnityPlayer.init() should be called before attaching the view to a layout - it will load the native code.
// UnityPlayer.quit() should be the last thing called - it will unload the native code.
protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGB_565);
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
View playerView = mUnityPlayer.getView();
setContentView(playerView);
playerView.requestFocus();
}
protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.
protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.onKeyMultiple(event.getKeyCode(), event.getRepeatCount(), event);
return super.dispatchKeyEvent(event);
}
}
I want to create project that reacts to OnCreate() method.
So, for example, I have activity
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
//do something
}
}
And i want my AspectJ class to do something before and after OnCreate method call.
public aspect onCreate
{
pointcut captureOnCreate() : (execution(* onCreate(Bundle)));
before(): captureOnCreate()
{
System.out.println("Aspect BEFORE called");
}
after(): captureOnCreate()
{
System.out.println("Aspect AFTER called");
}
}
I tried to convert project to AspectJ and run it as Android application project, but it doesn't work. What is wrong?
SOLVED
Solved it myself.
In Eclipse AspectJ tools -> Inpath -> Add External JARs and link it to aspectjrt.jar file.
And executoin looks like that:
execution(* onCreate(*))&& !within(com.xxx.automation.onCreate);
I have Activity that contains few Fragments, now I would like to test one of this Fragment but I would like to separate test and test only core functionality of selected Fragment not bothering what is happening in main Activity.
My idea is to create a mock Activity which will just add Fragment in onCreate() method. Then I will make some tests. But I would not like to include mock Activity to my main project, I would rather include it to test project. So I did something like this:
I have created MockActivity:
public final class ActivityMock extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentTransaction t = getFragmentManager().beginTransaction();
MyFragment f = new MyFragment();
t.add(f, "MY_FRAGMENT");
t.commit();
}
}
I want to test it like this:
public final class MyFragmentTest extends
ActivityInstrumentationTestCase2<ActivityMock> {
public MyFragmentTest() {
super(ActivityMock.class);
}
public void testSomething() {
ActivityMock mActivity = getActivity();
//do some assertions
}
}
The problem is that I I get error:
java.lang.RuntimeException: Unable to resolve activity for: Intent {
act=android.intent.action.MAIN flg=0x10000000
cmp=com.example/.test.ActivityMock }
Ok next I tried to modify test project AndroidManifest.xml
<activity android:name=".ActivityMock" />
But I got same error. I think that is because anyway during test run main project is searched for ActivityMock. So I tried to add
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.test" />
I don know if it is a good idea, but main thought is that test project will be able to test (instrument) itself. But now I get:
junit.framework.AssertionFailedError: Exception in constructor: testSomething
(java.lang.NoClassDefFoundError: com.example.test.ActivityMock
So I think that modified AndroidManifest.xml worked but still ActivityMock class is being searched in main project, though it is in test project.
I assume that getActivity() method always look for activity class in main project.
Does anybody tried to test Fragment this way and was able to create Activity mock?
Cheers
The following code was copied from project one and pasted into project two. No errors in project one. In project two I get:
The method onClick(View) of type new View.OnClickListener((){} must override a superclass method
implements androd.view.View.OnClickListener.onClick
The project settings look the same, but I must be missing something.
private Button mCompany = null;
public class About extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
setTitle(R.string.title_about);
mCompany = (Button)findViewById(R.id.about_company_button);
mCompany.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getResources().getString(R.string.app_company_website)));
startActivity(intent);
}
});
Found a solution on another site but I would like to know why this will work. It said to comment out the Override. Actually, it now compiles fine but crashes when the about item is clicked.