When you start a project with libgdx it automatically makes the class extend Android Application. I did not think about this until later and now I want to change it to the Game and Screen classes. But unfortunately without success...
My first question is, how to I change the android project?
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class AndroidClass extends AndroidApplication {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new SplashScreen(), cfg);
}
}
My second question: How do I change the deskop project:
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class DeskopClass {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "MyApp";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new SplashScreen(), cfg);
}
}
Third question: How do I change the SplashScreen:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class SplashScreen implements ApplicationListener{
#Override
public void create() {
// TODO Auto-generated method stub
}
Thanks!
The Game class is just an ApplicationListener. AndroidApplication and Game are not interchangeable classes as they accomplish two different things.
You need an AndroidApplication class to pass events on to your ApplicationListener classes. If you want a Game class in your app then you can always create your own.
public class Game implements ApplicationListener {
#Override
public void dispose () {
}
#Override
public void pause () {
}
#Override
public void resume () {
}
#Override
public void render () {
}
#Override
public void resize (int width, int height) {
}
}
Game class in libgdx is itself an ApplicationListener.
You can create a class that extends Game and directly pass its object to initialize (for android) and LwjglApplication (for desktop).
This way you can use setScreen without a problem.
Related
import android.app.Application;
import com.estimote.coresdk.observation.region.Region;
import com.estimote.coresdk.service.BeaconManager;
import java.util.UUID;
public class MyApplication extends Application {
private BeaconManager beaconManager;
#Override
public void onCreate(){
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override
// 비콘 uuid,major minor
public void onServiceReady() {
beaconManager.startMonitoring(new Region());
}
});
}
}
this is my code
when I use this code
-beaconManager.startMonitoring(new Region());-
get error named cannot be instanted
how can I fixed??
http://loveiskey.tistory.com/207
this is the site where I referd
Estimote Region is an interface. You cannot instantiate interfaces. You need to instantiate some class that implements that interface instead.
I created a ListDialog extending a DialogFragment class and I have a problem with understanding of this code in the DijalogX class
((MainActivity)getActivity()).setTextField(selectedItem);
I understand that with this code above I put selected String variable to the setTextField method as an argument and after that this variable is showed in TextView on MainActivity class.
My questions:
Why I need a cast from getActivity() to the MainActivity and how I get access from DijalogX(fragment) to the method setTextField in MainActivity? Please explain a little about this process.
I also tried instead of ((MainActivity)getActivity()).setTextField(selectedItem)
use an Interface and everything works nice and I got the same resoult but I am wondering what is better solution here Interface or ((MainActivity)getActivity()).setTextField(selectedItem)?
MainActivity
package com.example.dezox.dijaloglist;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
private Button btnStartDialog;
private TextView tvSelectedOption;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidgets();
setupListener();
}
private void initWidgets() {
btnStartDialog = findViewById(R.id.btnDialog);
tvSelectedOption = findViewById(R.id.tvselectedOption);
}
private void setupListener() {
btnStartDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DijalogX dijalogX = new DijalogX();
dijalogX.show(getSupportFragmentManager(), "dx");
tvSelectedOption.setText("");
}
});
}
public void setTextField(String odabrano){
tvSelectedOption.setText(odabrano);
}
public String getTextField(){
return tvSelectedOption.getText().toString();
}
}
DijalogX
package com.example.dezox.dijaloglist;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class DijalogX extends DialogFragment {
private String[] languageList;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initListResource();
}
private void initListResource() {
languageList = getResources().getStringArray(R.array.language_list);
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
android.R.style.Theme_Material_Dialog_Alert)
.setTitle("Select Language: ")
.setItems(languageList, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String selectedItem = languageList[which];
//THIS PART OF THE CODE I DONT UNDERSTAND:
((MainActivity)getActivity()).setTextField(selectedItem);
}
});
return builder.create();
}
}
You have declared a method in MainActivity called setTextField. If you called
Activity a = getActivity();
you would not be able to call your custom method (it is on your derived class, not the base Activity class).
a.setTextField(selectedIte); // WON'T WORK - NO SUCH METHOD
If instead you call
MainActivity ma = (MainActivity)getActivity();
it is now cast as your derived class and you can then call
ma.setTextField(selectedItem);
Doing it in two lines like this is the same as calling the one-liner in your code
((MainActivity)getActivity()).setTextField(selectedItem);
As far as casting vs. an interface, an interface is a bit more flexible of an approach. If you tried to use this fragment in a different activity (not MainActivity) the casting approach would fail. If you are only ever going to use the fragment in this Activity then either would work.
When i am calling "private class Engine extends CanvasWatchFaceService.Engine", i'm getting the error of 'No enclosing instance of type 'android.support.wearable.watchface.CanvasWatchFaceService' is in scope'
I have called imported the class import android.support.wearable.watchface.CanvasWatchFaceService; but it says it's a unused import statement
UPDATED: This is the whole of my MyWatchFace.java
package com.projects.kainowitzke.googlewatchface;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.wearable.watchface.CanvasWatchFaceService;
import android.support.wearable.watchface.WatchFaceService;
import android.view.SurfaceHolder;
import android.support.wearable.watchface.CanvasWatchFaceService;
public class MyWatchface {
}
public class AnalogWatchFaceService extends MyWatchface {
#Override
public WatchFaceService.Engine onCreateEngine() {
/* provide your watch face implementation */
return new CanvasWatchFaceService.Engine();
}
/* implement service callback methods */
private class MyWatchfac extends CanvasWatchFaceService.Engine {
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
/* initialize your watch face */
}
#Override
public void onPropertiesChanged(Bundle properties) {
super.onPropertiesChanged(properties);
/* get device features (burn-in, low-bit ambient) */
}
#Override
public void onTimeTick() {
super.onTimeTick();
/* the time changed */
}
#Override
public void onAmbientModeChanged(boolean inAmbientMode) {
super.onAmbientModeChanged(inAmbientMode);
/* the wearable switched between modes */
}
#Override
public void onDraw(Canvas canvas, Rect bounds) {
/* draw your watch face */
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
/* the watch face became visible or invisible */
}
}
}
MyWatchface must extend CanvasWatchFaceService.
package com.example.activitylifecycle;
import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;
public class MyApplication extends Application {
static final String TAG = "MyApplication";
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
#Override
public void onLowMemory() {
super.onLowMemory();
Log.d(TAG, "onLowMemory");
}
#Override
public void onTerminate() {
super.onTerminate();
Log.d(TAG, "onTerminate");
}
Help me to understand this concept by adding some keys (Codes).
I am new to android, What are the purpose of using Application objects?
If you want to learn Android i have to recommend the google documentation:
there is also a description of the lifecycle and the application itself
http://developer.android.com/training/index.html
and her is also a tutorial to extends Application
http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
I am trying to run this Android unit test, following this tutorial ::
http://developer.android.com/resources/tutorials/testing/helloandroid_test.html
and in doing so get a SuperNotCalledException
Here's the test class code ::
package com.example.helloandroid2.test;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
import com.example.helloandroid2.HelloAndroid2Activity;
public class HelloAndroid2Test extends ActivityInstrumentationTestCase2<HelloAndroid2Activity>
{
private HelloAndroid2Activity mActivity;
private TextView mView;
private String resourceString;
public HelloAndroid2Test()
{
super("com.example.helloandroid2", HelloAndroid2Activity.class);
}
#Override
protected void setUp() throws Exception
{
super.setUp();
mActivity = this.getActivity();
mView = (TextView) mActivity.findViewById(com.example.helloandroid2.R.id.textview);
resourceString = mActivity.getString(com.example.helloandroid2.R.string.hello);
}
public void testPreconditions()
{
assertNotNull(mView);
}
public void testText()
{
assertEquals(resourceString,(String)mView.getText());
}
}
The class I'm actually testing ::
package com.example.helloandroid2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
}
}
I've set the project API levels at 2_3_1 and am using an avd set at the same.
Am running Eclipse with ADT on Windows Vista.
All wisdom greatfully recieved. Thanks in advance.
Chris
Your onCreate() method in HelloAndroid2Activity needs to call super.onCreate(savedInstanceState);
public class HelloAndroid2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}