this is the current code
import android.os.Bundle;
import android.app.Activity;
import android.opengl.GLSurfaceView;
public class OpenglstencilActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView view = new GLSurfaceView(this);
view.setEGLConfigChooser(8, 8, 8, 8, 16, 4);
view.setRenderer(new OpenGLRenderer());
setContentView(view);
}
}
and i updated the manifest to include
if i change the stencilsize from 4 to 0 then the app will load. any value other than 0 and i get an illegal argument exception. i need the stencil size set as im trying to use the stencil buffer.
any ideas?
If you are using the Android emulator, none of the EGL configurations are suitable to enable stencil.
So no luck for using stencil on the emulator.
If you are using a device, then it is a different matter and this answer would be wrong.
Related
I'm trying to make a simple app that changes the background color so many times a second. For example, here I'm trying to do it every 12.5 ms (how do I enter the .5 part?)
I'm extremely new to programming, and can't figure out how to call this method/timer to start running.
I run the app, and it opens the activity with a white background and nothing happens.
Since I'm so new, when I look into the Timer() documentation, I'm not really sure what I'm looking at here.... If someone could help that would be amazing.
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.util.Timer;
import java.util.TimerTask;
import static android.support.v7.appcompat.R.attr.background;
/**
* Created by Spader on 3/17/2017.
*/
public class FlashingScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.flashing_screen);
new Timer().scheduleAtFixedRate(new TimerTask(){
#Override
public void run(){
runOnUiThread(new Runnable() {
#Override
public void run() {
//stuff that updates ui
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
getWindow().getDecorView().setBackgroundColor(Color.BLACK);
}
;
});
}
},0,12);
}
}
1.
getWindow().getDecorView().setBackgroundColor(Color.WHITE); getWindow().getDecorView().setBackgroundColor(Color.BLACK);
This always set the black color since it is not on a conditional basis. You may want to use a boolean to switch color every time it executes the runnable.
You shouldn't use getDecorView(). Instead find the root View from your layout xml and change the color.
In Java there is no floating value for times. So you can't have 12.5 ms.
My Android app is simple. It has only 1 activity. I created two layouts for the same activity: one for the portrait position (inside the res/layout folder) and one for the landscape position (inside the res/layout-land folder). I give the code for both at the end of this question.
I have nothing special inside the myActivity.java file, I just inflate the layout:
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
public class MCentralActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
}
}
Everything works fine at this point. If I hold my device in the portrait position, the app will call the appropriate XML. It works like a charm too, in case I decide to hold it in the landscape position.
The problem arises when I decide to add a little bit of code to the aforementioned myActivity.java file; it is still really simple!
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MCentralActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageButton ibPacientes;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
ibPacientes = (ImageButton)findViewById(R.id.imageButton_Pacientes);
ibPacientes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//NOTHING INSIDE HERE!!
}
});
}
}
After implementing that little code, if I decide to go landscape, the App will stop abruptly saying "Unfortunately myApp has stopped". Interesting enough this won't occur if I don't implement onClickListener!
The exact error given by LogCat is as shown:
03-28 13:39:27.870: E/SurfaceFlinger(157): DRAW orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: E/SurfaceFlinger(157): STATE orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: I/SurfaceFlinger(157): ######## orientation:1, transformOri:4
Don't think the layout XML files has to do much in my problem, but I will copy and paste them in this link (for portrait) and this other link (for landscape), in order to not to make this question excessively long.
Thanks in advance!
In your landscape XML file you have the ImageButton called imageButton_Users instead of imageButton_Pacientes. Rename it and everything should work fine.
You're trying to find a view that's not there and so your app will crash.
Just making a test application that should display a log message on changing orientation, but when i change orientation using ctrl + f11, there is nothing shown in the logcat.
what is the issue in the coding
A Part From Manifest
<activity
android:name="com.example.orientationtest.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize" >
A part from Java
package com.example.orientationtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d("error", " orientation changes");
}
}
try this if target api 13 and above
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
or
android:configChanges="orientation|keyboardHidden|screenLayout"
for lower than 13 ..... this will help you... :)
Using configChanges is not a good idea if you only want to know if the screen rotated. It basically means that you plan to handle the whole rotation change by yourself in the code.
If you don't plan to do this, just check what is the current rotation when your Activity is created:
int currentOrientation = getResources().getConfiguration().orientation;
Then just do whatever you want using this check:
if(currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
//if landscape
} else {
//if portrait
}
This way you don't need to touch your manifest file and you let your app manage the configuration changes by itself.
You can keep the previous orientation used in the activity saved instance if you need to compare the previous and the current one.
The issue is that you prevented the android from changing its orientation
android:configChanges="orientation|screenSize"
that line of code is preventing it to configuration change and that why your not getting any response..
I'm working with a friend trying to develop an android application to track movement by numerically integrating acceleration. We are mechanical engineering students and having trouble understanding how the order of events is occurs within the threads used in an android application. For example it appears that the UI thread runs before the code that called it can finish executing. See the following code that just attempts to get the current acceleration and store the previous acceleration for later use in trapezoidal integration:
import java.util.Arrays;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
float[] current;
float[] last;
SensorManager sm;
Sensor accelerometer;
TextView Display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
current = new float[] {0, 0, 0};
last = new float[] {0, 0, 0};
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Display = (TextView)findViewById(R.id.text_box);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onSensorChanged(SensorEvent event) {
last = current;
current = event.values;
if (last != current) {
Display.setText(Arrays.toString(current)+"\n"+Arrays.toString(last)+"\nThe numbers are not the same!!!");
} else {
Display.setText(Arrays.toString(current)+"\n"+Arrays.toString(last)+"\nThe numbers are the same!!!");
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
The result is that the TextView always displays "the numbers are the same" text.The acceleration values change but the printed current and last values are always the same (ie "[.4423,.12234,9.8234]\n[.4423,.12234,9.8234]\nThe values are the same"). We put a counter in the code to see how often the if statement detects that the values are different and it only increments to one, which I assumes happens on the first iteration. I can't come up with any good theory why the values would change with time but the current and last values are never different. Can the UI thread somehow get the value of last before it is updated by the sensor manager thread? Should I have to deal with handling the order of thread execution if I didn't explicitly create one?
OK so the problem was a fundamental misunderstanding of how Java works. We were passing around the pointers to the array. I have worked with c++ before and thought that since java doesn't use pointers that setting arrays equal to one another was equivalent to passing by value. Doing this actually just set the reference to the current and last variables equal to the reference to the events.value array, which means that the setText function was told to print the values from the same reference. This was a painful way to learn, the Professor who gave us this project is the third person who said to me "if you understand C++, you'll understand java", when he should have said go read a book on the specifics...
To run the above code running we changed:
last = current;
current = event.values;
to this:
System.arraycopy(current,0,last,0,3);
System.arraycopy(event.values, 0, current, 0, 3);
(Solved) Thanks to NetCat: it was a UI XML problem :)
Today I have started Android development, i'm using a Mac with Eclipse and the Android SDK install and all working (I have successfully managed to get working a few "Hello World" type apps working) and for the Android device i'm using my new HTC Incredible S.
So the code below should work and their are no error is the debugger when I run it on my phone, but each time I do before it even loads, the phone pops up a message saying "The application Count (process com.count) has stopped unexpectedly. Please try agin."
I have re created the project several times with different SDK versions 1.5 and 2.2 but still no luck.
The code is from another tutorial I successfully worked through but i have changed some of the variables to make a slightly different app. Can you tell me what is wrong with the following code:
package com.count;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class count extends Activity implements OnClickListener {
//Declare widgets
Button btnSave, btnUp, btnDown;
TextView lblCurrentCount, lblCountCard;
//Declare variables
int intCount=0;
int intCardCount=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Sets up the link between java and the XML widgets
btnSave = (Button)findViewById(R.id.btnSave);
btnUp = (Button)findViewById(R.id.btnUp);
btnDown = (Button)findViewById(R.id.btnDown);
lblCurrentCount = (TextView)findViewById(R.id.lblCurrentCount);
lblCountCard = (TextView)findViewById(R.id.lblCountCard);
//Initialize widgets
lblCurrentCount.setText(String.valueOf(intCount));
lblCountCard.setText("");
//Define button listeners
btnSave.setOnClickListener(this);
btnUp.setOnClickListener(this);
btnDown.setOnClickListener(this);
}
//When any button is clicked
#Override
public void onClick(View src) {
//Actions when buttons are clicked
switch(src.getId()) {
case R.id.btnSave:
lblCountCard.append("\n#" + intCardCount + " " + intCount);
intCardCount++;
intCount=0;
lblCountCard.setText(String.valueOf(intCount));
break;
case R.id.btnUp:
intCount++;
lblCountCard.setText(String.valueOf(intCount));
break;
case R.id.btnDown:
intCount--;
lblCountCard.setText(String.valueOf(intCount));
break;
}
}
}
Thanks Dave
You must supply a layout_width
attribute.
That's probably it. You've got a layout element without a layout_width (and probably, if you make the same kinds of mistakes I do, a layout_height) attribute. Find it, add the attributes, and see what comes next.