I am using the library scringo on Android. "openChat" function doesn't seem to be working. It does absolutely nothing. Here is my code.
I read through their API:
http://www.scringo.com/docs/api/android/
openChat function should open the 1-on-1 chat with the other user. But that doesnt happen. Nothing happens. All the other functions are working fine.
It doesn't even log any errors or warning.
public class MainActivity extends Activity implements OnClickListener {
private Scringo scringo;
private Activity mainactivity;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainactivity = this;
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
Scringo.setAppId("MY-APP-ID");
Scringo.setDebugMode(true);
scringo = new Scringo(this);
scringo.init();
scringo.addSidebar();
Scringo.loginWithEmail("a#testapp.com", "hi", new ScringoSignUpListener(){
#Override
public void onError(String arg0) {
}
#Override
public void onSuccess(String arg0) {
Log.w("user",Scringo.getUserId());
}
});
}
#Override
public void onClick(View arg0) {
//I am using the ID of another user.
//This does not work. Nothing happens. No error or warning either.
Scringo.openChat(this, "Qk8vJs4fRE");
//This works fine.
//Scringo.openChatRooms(this);
}
}
You should call the openChat after getting the user:
Scringo.getUserByScringoId("SOME_ID...", new ScringoGetUserListener() {
#Override
public void gotUser(ScringoUser user) {
Scringo.openChat(MainActivity.this, "SOME_ID...");
}
});
Related
I hope you can help me, I am starting with UnityAds, I have an application on Android Studio (Java) where the SplashActivity has a button.
What I want is that, after pressing the button, the UnityAds interstitial is shown and when the ad ends, the MainActivity is shown.
The code I made shows the interstitial after the button but I don't know how to make it open the next activity to the MainActivity because after the interstitial it returns to the SplashActivity
public class SplashActivity extends AppCompatActivity {
String GameID = "123456";
String adUnitId = "Interstitial";
Boolean TestMode = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro_uno);
//iniciador Unity
UnityAds.initialize(SplashActivity.this, GameID, TestMode, new IUnityAdsInitializationListener() {
#Override
public void onInitializationComplete() {
}
#Override
public void onInitializationFailed(UnityAds.UnityAdsInitializationError unityAdsInitializationError, String s) {
}
});
//iniciador Unity
}
public void onClick(View view) {
IUnityAdsShowListener iUnityAdsShowListener = new IUnityAdsShowListener() {
#Override
public void onUnityAdsShowFailure(String s, UnityAds.UnityAdsShowError unityAdsShowError, String s1) {
}
#Override
public void onUnityAdsShowStart(String s) {
UnityAds.load(adUnitId);
UnityAds.show(SplashActivity.this,adUnitId);
}
#Override
public void onUnityAdsShowClick(String s) {
}
#Override
public void onUnityAdsShowComplete(String s, UnityAds.UnityAdsShowCompletionState unityAdsShowCompletionState) {
}
};
UnityAds.load(adUnitId);
UnityAds.show(SplashActivity.this,adUnitId);
}
}
Just add
startActivity(new Intent(MainActivity.this,MainActivity2.class));
In onUnityAdsShowComplete method
---In this above line MainActivity is the Current Activity and MainActivity2 is the second Activity
no button feedback in android studio, i just want to guide from one activity to another! :(
no errors
here's the code
public abstract class OrderActivity extends AppCompatActivity {
private Button clickButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
clickButton =(Button) findViewById(R.id.IDbutton2);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity();
}
});
}
public void openActivity(){
Intent intent = new Intent(OrderActivity.this, orderPage.class);
startActivity(intent);
}
}
using nexus 6 emulator
please help guys
You haven't set a content view, so your OrderActivity has no layout. As such, there is no view with ID IDbutton2. This causes clickButton to be null, and not do anything.
You can see a very similar example in the Android training (I've removed the irrelevant bits):
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
}
I wrote a compound component and was adding a custom listener to react.
Inside the class for the compound component which uses an xml file.
public class VerticalCounterBlock extends LinearLayout {
public interface VerticalCounterBlockListener {
public void onCountChanged(int newCount);
}
private VerticalCounterBlockListener mVerticalCounterBlockListener = null;
public void setVerticalCounterBlockListener(VerticalCounterBlockListener listener){
mVerticalCounterBlockListener = listener;
}
// ... Other functions
}
I got my interface, I got the listener and I got the setter and I engage the listener like this in the button I have in the compound component. I can see that toast that is showing there when I test
addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
count++;
counttv.setText(String.format("%1$d", count));
Toast.makeText(getContext(), "VCB", Toast.LENGTH_SHORT).show();
if(mVerticalCounterBlockListener != null) {
mVerticalCounterBlockListener.onCountChanged(count);
}
}
});
In my main activity
m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(new VerticalCounterBlock.VerticalCounterBlockListener() {
#Override
public void onCountChanged(int newCount) {
increasePreachCountTotal();
Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
}
});
I do not see that toast nor does it engage the function call. What am I missing?
I can suggest you several improvement scope here mainly restructuring the current format.
Lets not keep the interface as a inner class. So here's your VerticalCounterBlockListener.java
public interface VerticalCounterBlockListener {
public void onCountChanged(int newCount);
}
Now implement this interface in your MainActivity
public class MainActivity implements VerticalCounterBlockListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(this);
}
// ... Other methods
// Override the onCountChanged function.
#Override
public void onCountChanged(int newCount) {
increasePreachCountTotal();
Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
}
}
You might consider removing the Toast from the addBtn click listener which might create exception.
addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
count++;
counttv.setText(String.format("%1$d", count));
if(mVerticalCounterBlockListener != null) {
mVerticalCounterBlockListener.onCountChanged(count);
}
}
});
This was good there was something wrong with my system. i uninstaklled app and restarted computer and it worked as expected.
I have implemented an otto bus example. It works fine, but ONLY on the second time I visit the activity.
For example, when I load the app and hit the secret message button I am taken to the activity but the toast does not show. Then I hit the back button to return to the MainActivity and hit the show secret message button again and when I am taken to the secret message activity the toast is displayed. I realize it works the second time because I have created a leak by not unregistering the event.
Is there something I am missing about the logic?
MainActivity:
public class MainActivity extends AppCompatActivity {
Button buttonSecretMessage;
Intent intentToMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentToMessage = new Intent(MainActivity.this, SecretMessageActivity.class);
buttonSecretMessage = (Button) findViewById(R.id.buttonSecretMessage);
buttonSecretMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EventBus.getInstance().post(new MakeMySecretMessageEvent());
startActivity(intentToMessage);
}
});
}
}
Secret Message Activity:
public class SecretMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secret_message);
}
#Subscribe
public void getMySecretMessage(MakeMySecretMessageEvent event){
Toast.makeText(this, event.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
protected void onStart(){
super.onStart();
EventBus.getInstance().register(this);
}
#Override
protected void onStop() {
super.onStop();
//EventBus.getInstance().unregister(this);
}
}
MakeMySecretMessageEvent:
public class MakeMySecretMessageEvent {
public MakeMySecretMessageEvent() {
}
public String getMessage() {
String message = "YOU ARE AWESOME!";
return message;
}
}
EventBus:
public final class EventBus extends Bus{
private static final EventBus Bus = new EventBus();
public static Bus getInstance() {
return Bus;
}
private EventBus() {
}
}
You can send sticky event using EventBus library. It allows you to send events to component which is not created yet.
You`ll find more info here.
Here EventBus has applied in wrong scenario, when you can simply send data via intent or bundle. Which is more reliable in communication with one activity with another. You will never ever receive event on first click, as event fire is instant and your activity creation will take some time accordingly.
So try to use bundle or intent to setup communication b/w to activity one after another.
Thanks to contributors I now have a better understanding of the activity life cycle and how it fits in with event bus. That is you cannot send an event from the MainActivity to its children, but the other way around instead. Below reflects how to implement an otto event bus to pass a simple object from an activity back to the main activity. Hopefully someone else can find this useful :) And if this can be improved upon please comment. Thanks.
Main Activity:
public class MainActivity extends AppCompatActivity {
Button buttonSecretMessage;
Intent intentToMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getInstance().register(this);
intentToMessage = new Intent(MainActivity.this, SecretMessageActivity.class);
buttonSecretMessage = (Button) findViewById(R.id.buttonSecretMessage);
buttonSecretMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intentToMessage);
}
});
}
public MakeMySecretMessageEvent event;
#Subscribe
public void getMySecretMessage(MakeMySecretMessageEvent event) {
Toast.makeText(this, event.getMessage(), Toast.LENGTH_SHORT).show();
}
protected void onStop() {
super.onStop();
if(event != null ){
EventBus.getInstance().unregister(this);
}
}
}
SecretMessageActivity (this is where the secret message is created)
public class SecretMessageActivity extends AppCompatActivity {
Button buttonClickToMeToSeeMessage;
Intent intentToMain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secret_message);
intentToMain = new Intent(SecretMessageActivity.this, MainActivity.class);
buttonClickToMeToSeeMessage = (Button) findViewById(R.id.buttonClickToMeToSeeMessage);
buttonClickToMeToSeeMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MakeMySecretMessageEvent makeMySecretMessageEvent = new MakeMySecretMessageEvent();
EventBus.getInstance().post(makeMySecretMessageEvent);
startActivity(intentToMain);
}
});
}
}
MakeMySecretMessageEvent
public class MakeMySecretMessageEvent {
public MakeMySecretMessageEvent() {
}
public String getMessage() {
String message = "YOU ARE AWESOME!";
return message;
}
}
EventBus:
public final class EventBus extends Bus{
private static final EventBus Bus = new EventBus();
public static Bus getInstance() {
return Bus;
}
private EventBus() {
}
}
I write a simple demo to test it.
------code--------
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String s = null;
s.length();
}
});
}
}
The log shows nullPointerException, but app doesn't crash, just like it has been try-catched.
If I write [ String s = null;s.length() ] in onCreate directly,app crash.