I have a custom class 'Game' which i init at top of activity code. I then go to another activity, usually i pass arraylists etc but i want to move to passing my custom class.....
My custom class 'game' is a bunch of strings and arraylists with getter and setter mehtods.
i get a
Game is not a parcelable or serializable object
error when I try to add it to the intent. Any ideas what i can do here?
//Init Instance of Game class
Game newGame = new Game();
Set my listener. It works for
//Setup onclick listeners
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(this_Activity.this, next_Activity.class);
i.putExtra("players", myList);
i.putExtra("newGame", (Parcelable) newGame);
startActivityForResult(i, 0);
}
});
Also, your class Game may to implement interface Serializable:
public class Game implements Serializable {
...
}
You have to change listener in first activity:
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(this_Activity.this, next_Activity.class);
i.putExtra("players", myList);
i.putExtra("newGame", newGame);
startActivityForResult(i, 0);
}
});
And change method onCreate in next_Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Game newGame = (Game) getIntent().getExtras().getSerializable("newGame");
}
Game class need to implement Parcelable.
Related
I read some example on this website and other but still have error.
It's not a compilation error but my application crash when I click on the button.
There is code of my MainActivity.java (only interesting part) :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//blablabla
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myLayout= (LinearLayout) findViewById(R.id.layoutProp);
Button button = (Button) findViewById(R.id.buttonValider);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT, text); // this one does not work
intent.putExtra(EXTRA_NUMBER, nbTextView);
startActivity(intent);
}
}
And code in Activity2.java
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
int nbTextView = intent.getIntExtra(MainActivity.EXTRA_NUMBER, 0);
MyTextView[] text = (MyTextView[]) intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
TextView textView1 = (TextView) findViewById(R.id.TVtest);
textView1.setText(""+ nbTextView);
} }
if I comment : MyTextView[] text =(MyTextView[])intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
the application does not crash.
Thank you all very much for your help
You cannot pass views through intents. Views are part of the layout and cannot be shared between activities.
If you want to pass array with custom objects you must implement a Parcelable or Serializable class and use:
getIntent().getParcelableExtra("array");
or
getIntent().getSerializableExtra("array");
Parcelable class will be faster but it is harder to implement.
Another way is to use a library like Gson to parse your data to Json and pass it as a string.
I have one EditText which values i want to store with a button click in an array, in second Activity i want to display these values in Listview. I have some problems with storing and displaying values in another activity.
Use callback.
in ClassA with the stringList:
Create interface
MyCallback callback;
viod setCallback(MyCallback callback){
this.callback = callback;
}
viod onStop(){
callback = null;
}
interface MyCallback{
void doSomething(String string);
}
in ClassB:
implement MyCallback
public class ClassB implements ClassA.MyCallback
set reference in onCreate
ClassA classA = new ClassA();
classA.setCallback(this);
// override method doSomething
#override
void doSomething(String string){
//get your string from your EditText…
}
when the job is done inside class A call:
callback.doSomething(string);
destroy reference inside class B in onStop()
classA.onStop();
You can use an intent when starting the second Activity from your button click.
Intent intent = new Intent(this, SecondActivity.class);
intent.putStringArrayListExtra("EXTRA_ARRAY", arrayList);
startActivity(intent);
In your second activity...
List<String> arrayList = getIntent().getStringArrayListExtra("EXTRA_ARRAY");
you can use a static arrayList inside your activity like that :
class YourActivity extends AppCompatActivity{
public static ArrayList yourArray;
#Override
void onCreate(Bundle savedInstanceState){
.......
// you can use you array to display its content
}
}
and inside your button action do like that
botton.setOnClickListener(new OnClickListener{
#Override
void onClick(View view){
YourActivity.yourArray = this.arrayList;
startActivity(new Intent(getContext , YourActivity.class));
}
});
I'm a newbie here learning how to create a game in android and stumbled on a tutorial http://williammora.com/a-running-game-with-libgdx-part-1
i already finished the game but it lacks menu and score. I am getting overwhelmed by the number of classes and i don't know where to put the menu screen. there is a java class there that is named Android Launcher and i think its the one that calls the game to start so I created a layout xml menu and try to call it after pressing a button this is my code. there's no error but the game crashes
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = findViewById(R.id.btnLetsgo);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new RollOut(), config);
}
});
}
}
ive read some tutorials and its possible to create a layout for the menu. ill add another class Main.class and `public class Main extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = findViewById(R.id.btnLetsgo);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gotoNext();
}
});
}
private void gotoNext(){
Intent i = new Intent(this, AndroidLauncher.class);
startActivity(i);
}
}`
and the androidlauncher class
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new RollOut(), config);
Intent i = getIntent();
}
}
but its still crashing
It sounds that maybe you are very new in libgdx. It very hard to say with you where should be puth your Menu Screen. Let spend your time to study Screen Class. Which you can extend to create you MenuScreen. Here is more information I hope useful for you:
example: https://www.gamedevelopment.blog/full-libgdx-game-tutorial-menu-control/
wiki: https://github.com/libgdx/libgdx/wiki/Scene2d
class MainScreen extends Screen{ public MainScreen{} }
And in game class:
this.setScreen(new MainScreen()) ;
When i add another button (nextButton2) in (main activity. x m l) !
Now i want to make button open (third screen.x m l)
can you help me to to add listener to it in this android project in (main.java) but failed??
link original project
my project link
followed by existing logic from your code, this is how it could look like:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.nextButton).setOnClickListener(new handleButton());
findViewById(R.id.nextButton2).setOnClickListener(new handleButton2());
}
class handleButton implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Main.this, Screen2.class);
startActivity(intent);
}
}
class handleButton2 implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Main.this, Screen3.class);
startActivity(intent);
}
}
}
It assumes that you already created new activity called Screen3.java and add it to manifest file:
<activity android:name="your.project.package.Screen3" android:label="#string/app_name" />
There are a number of ways to achieve that:
Anonymous listeners
If you need the listeners only once:
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(new OnClickListener() {
// your first listener here
});
button2.setOnClickListener(new OnClickListener() {
// your second listener here
});
}
}
Multiple nested classes
If you want to re-use the listeners in the same class:
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(new Listener1());
button2.setOnClickListener(new Listener2());
}
class Listener1 implements OnClickListener {
// your first listener here
}
class Listener2 implements OnClickListener {
// your second listener here
}
}
Multiple top-level classes
If you want to re-use the listeners in more than one class:
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(new Listener1());
button2.setOnClickListener(new Listener2());
}
}
class Listener1 implements OnClickListener {
// your first listener here
}
class Listener2 implements OnClickListener {
// your second listener here
}
My Activity has multiple lists so I have defined MyClickListener as below:
My question is how I should instantiate this class:
MyClickListener mMyClickListener = new MyClickListener();
Or maybe it is better to instantiate inside the onCreate(Bundle) and just define above. Whats considered the better way? I don't want too much in onCreate() its already full of stuff. Any thoughts on the declaration and instatiation? Whats the best way?
private class MyClickListener implements OnClickListener
{
#Override
public void onClick(View view) {
}
}
I use same kind of class mechanism as you mentioned in the question.
this is the way i use,
public class myActivity extends Activity
{
private MyListener listener = null;
private Button cmdButton = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cmdButton = (Button) findViewById(R.id.cmdButton);
cmdButton.setOnClickListener(getListener());
}
// method to fetch the listener object
private MyListener getListener()
{
if (listener == null)
{
listener = new MyListener();
}
return listener;
}
private class MyListener implements Button.OnClickListener
{
public void onClick(View v)
{
}
}
}
Why are you instantiating a listener like that in the first place? Just create a new one when you assign it to your listView.
listView.setOnClickListener( new MyListener());