I'm learning to program for Android on the Android Studio .
When I start the application, the following message appears on the AVD " Unfortunalety , GuitarStoreV2 has stopped ."
Can anybody help me ?
(Sorry if some stupid mistake , do not have much familiarity with the language , and excuse the error Portuguese , because I am Brazilian and I do not speak English fluently )
Grateful for the attention
Mark Tonial
public class MainActivity extends AppCompatActivity {
Button btn_iniciaapp;
public void Inicia ()
{
Button btn_inciaapp = (Button) findViewById(R.id.btn_iniciaapp);
}
// Iniciando a tela de produtos
public void iniciaProd()
{
Intent ActivityProd = new Intent(this, ActivityProd.class);
startActivity(ActivityProd);
}
// Evento ao clicar no Botão
public void IniciaListener()
{
this.btn_iniciaapp.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
MainActivity.this.iniciaProd();
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
this.Inicia();
this.IniciaListener();
}
}
Button btn_iniciaapp;
public void Inicia ()
{
Button btn_inciaapp = (Button) findViewById(R.id.btn_iniciaapp);
}
Your program is likely crashing with a NullPointerException. Here, Inicia isn't setting the value of your class's btn_iniciaapp: it's creating a locally scoped Button which is shadowing your class's btn_iniciaapp. You're setting the value of the local Button, not the class's.
Since the class's btn_iniciaapp is never set, it remains null, and the call to this.btn_iniciaapp.setOnClickListener will trigger a NullPointerException.
Related
My app supports 3 languages (english, french and arabic). I have already translated all ressources (values string and drawables files) and it work perfect according to the language set in the user device.
The app consists principally of two activities : mainActivity and Game Activity. mainActivity contain a button (Play) which leads to the GameActivity according to the device language. For example if language is english it will launch Game Activity, if f language is french it will launch GameFr Activity, and if language is arabic it will launch GameAr Activity.
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Locale.getDefault().getLanguage().equals("ar")){
Intent intgame=new Intent(MainActivity.this,GameAr.class);
startActivity(intgame);
}
else {
if (Locale.getDefault().getLanguage().equals("fr")){
Intent intgame=new Intent(MainActivity.this,GameFr.class);
startActivity(intgame);
}
else {
Intent intgame=new Intent(MainActivity.this,Game.class);
startActivity(intgame);
}
}
}
});
However, I would like to add 3 ImageView (flags) in the MainActivity through which users can change the language of the application, for this I added the following:
en = (ImageView) findViewById(R.id.en);
fr = (ImageView) findViewById(R.id.fr);
ar = (ImageView) findViewById(R.id.ar);
en.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("en");
Intent uo = new Intent(MainActivity.this,Game.class);
startActivity(uo);
}
});
fr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("fr");
Intent uo = new Intent(MainActivity.this,GameFr.class);
startActivity(uo);
}
});
ar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("ar");
Intent uo = new Intent(MainActivity.this,GameAr.class);
startActivity(uo);
}
});
Nevertheless, when a user with a device which the language is set to english click the French flag, it will get successfully the french activity. However, if he comes back to the previous page and click on the button (Play), the page displayed is the one corresponding to the activity in English but with the resources (string values and Drawables) French.
This is because the following function:
if (Locale.getDefault().getLanguage().equals("ar"))
always test the language of the device, not the language that the user chose in the app.
is there a function that can give me the language chosen by the SetLocale function? or I should use a variable transfer between activities?
how can I fix this, do you have better suggestions?
use this
Locale.setDefault("Your Locale");
Hi there I am new to Android Programming
I am trying to create an application in which, the user clicks button on the first page
the text color in the buttons change color and the change is reflected in another activity page.
To do this I have
1) one fragment class(BookLockerFragment) which reference to an xml file containing the buttons
2) The parent activity file (TabActivity.java)
3) The activity file to reflect the change ( complainResponse.java)
Here is the code:
LodgeComplaintFragment.java
ArrayList<String>userSelectedOptions = new ArrayList<String>();
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS");
}
Button but = (Button) root.findViewById(R.id.searchButton);
.....
but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
buttonListener.onMakeBookingButtonPressed(userSelectedOptions);
}
});
TabMainActivity.java
public void onMakeBookingButtonPressed(ArrayList<String> list) {
// TODO Auto-generated method stub
Intent intent = new Intent(TabMainActivity.this,
complainResponse.class);
intent.putStringArrayListExtra("userSelectOptions",list);
startActivity(intent);
}
complainResponse.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
setContentView(R.layout.complainresponse);
userInput = intent.getStringArrayListExtra("userSelectOptions");
// Creates the window used for the UI
if (userInput != null) {
if (userInput.get(0) != null) {
textview1 = (TextView) findViewById(R.id.textView1);
textview1.setText(userInput.get(0));
}
}
}
Error occurs at this line:
if (userInput != null) {
//of complainResponse.java
Logcat:
java.lang.IndexOutOfBoundsException
Please help
There's nothing in the ArrayList that you pass to your activity.
I suspect this bit of code isn't being executed -
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS"); <------------ never gets here
}
To verify this, run the application in debug mode, and place a breakpoint at the if statement
userInput.get(0) != null
this is the cause of error in my opinion, list can be initialized but empty.
instead you should use,
if (!userInput.isEmpty())
I have seen some related questions but none focusing on the specific problem I have:
I'm using the PayPal MPL Library.
I build my PayPalPayment object, then create the activity for the checkout to occur. That runs fine. My problem is, on the ResultDelegate I need to call a function from my activity, that occurs after the payment and makes some changes (such as storing SharedPreferences, etc.).
So something like this:
public class ResultDelegate implements PayPalResultDelegate, Serializable {
public void onPaymentSucceeded(String payKey, String paymentStatus) {
System.out.println("SUCCESS, You have successfully completed your transaction.");
System.out.println("PayKey: "+payKey);
System.out.println("PayStatus: "+paymentStatus);
callMyCustomAfterPaymentFunction();
}
...
}
Now the thing is, I tried to create a constructor for ResultDelegate that accepts my activity. My existing code is:
//On the activity class
public class MainMenuActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
...
Button buy = (Button) findViewByID(R.id.buy_button);
buy.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
new PurchaseTask(activity).execute();
}
}
}
}
public class PurchaseTask extends AsyncTask <String, Void, String> {
protected String doInBackground()
{
...
PayPal pp = PayPal.getInstance();
CheckoutButton cb = pp.getCheckoutButton(...);
cb.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ResultDelegate delegate = new ResultDelegate(myActivity);
Intent checkout = PayPal.getInstance().checkout(paument, activity, delegate);
activity.StartActivity(checkoutIntent);
}
}
}
}
//On the ResultDelegate class
public class ResultDelegate implements PayPalResultDelegate, Serializable {
private Activity myActivity;
public void onPaymentSucceeded(String payKey, String paymentStatus) {
System.out.println("SUCCESS, You have successfully completed your transaction.");
System.out.println("PayKey: "+payKey);
System.out.println("PayStatus: "+paymentStatus);
myActivity.performAfterPaymentOperations();
}
...
}
So the goal is to call the activity function from the ResultDelegate. Or even simpler, just to be able to store some SharedPreference changes when the ResultDelegate onPaymentSucceeded() fires.
But I get a NotSerializableException mentioning that the my MyActivity field is not Serializable.
So, then I added the transient identifier to my activity field inside the ResultDelegate, but now I get a NullPointerException.
Paypal Mobile Chekout guide
Implementation provided on paypal website is different from yours. They are doing startActivityForResult() to start PaypalActivity. and when in onActivityResult() method they are checking statusCode to check transaction status and act accordingly.
Follow that document for your implementation.
Here in your code, I donot find a point for using AsyncTask. Your ResultDelegate is Serializable where as Activity is not thats why it is throwing NotSerializableException.
Edit:
As you are developing for Google Android platform, then why not to use Google Checkout In-App?
Edit:
This method will be called when your PaypalActivity will finish. That activity will pass resultCode to this onActivityResult method.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case Activity.RESULT_OK:
// The payment succeeded
String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
// Tell the user their payment succeeded
break;
case Activity.RESULT_CANCELED:
// The payment was canceled
// Tell the user their payment was canceled
break;
case PayPalActivity.RESULT_FAILURE:
// The payment failed -- we get the error from the EXTRA_ERROR_ID
// and EXTRA_ERROR_MESSAGE
String errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
String errorMessage = data
.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
// Tell the user their payment was failed.
}
}
regards,
Aqif Hamid
You can create you custom listener something like this :
Create a custom listener :
OnDoubleTap mListener;
// Double tap custome listenre to edit photoes
public interface OnDoubleTap {
public void onEvent(Uri imgPath, int mPos);
}
public void setDoubleTapListener(OnDoubleTap eventListener) {
mListener = eventListener;
}
Now call this wherever you want like this :
mListener.onEvent(Uri, 1));
Now whenever you call this listener this will fire in your activity where you use this listener like this :
myCanvas.setDoubleTapListener(new OnDoubleTap() {
#Override
public void onEvent(Uri imgPath, int Pos) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "LISTENER WORKING !!!", Toast.LENGTH_SHORT).show();
}
});
Where myCanvas is object of class where you create you listener.
Try this solution:
PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(price),getResources().getString(R.string.curruncy_code), getResources().getString(R.string.app_name),
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(CreateEventStep4.this, PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_PAYPAL_PAYMENT);
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.e("paymentExample", confirm.toJSONObject().toString());
JSONObject jsonObj=new JSONObject(confirm.toJSONObject().toString());
String paymentId=jsonObj.getJSONObject("response").getString("id");
System.out.println("payment id:-=="+paymentId);
screenShotFile = takeScreenshot(frmTemplate);
uploadImage(myEvent.getEvent_id(),screenShotFile);
} catch (JSONException e) {
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
}
I'm in the middle of developing a small app for Android using the Android UI and activities for the most part of the interaction, however one key aspect requires the use of LibGDX (using 3D models and physics). I want to be able to click a button in my app (my "Activate" class) which will open the "AndroidApplication" class (my "Bobble" class) that initializes and runs all the LibGDX code.
My problem is that I can't use an "Intent" to start an AndroidApplication class (only an Activity as far as I can tell). I'm sure that people have had to work around this issue in the past so any help would be fantastic.
Here's my code so far:
public class Activate extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
setContentView(R.layout.activate_screen);
Button b_Run = (Button) findViewById(id.bActiveRun);
b_Run.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent to_Bobble = new Intent(v.getContext(), Bobble.class);
startActivity(to_Bobble);
}
});
}
catch (Exception e)
{
Log.e("Activate", "Error in activity", e);
Toast.makeText(getApplicationContext(),
e.getClass().getName() + " " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
public class Bobble extends AndroidApplication {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LifeCycle loop = new LifeCycle();
loop.ddgSettings = new ddgSystemSettings(this);
initialize(loop, false);
}
}
Ok I can now confirm that there is no issue at all with the above code. The issue was that I hadn't declared my "Bobble" class/file in the AndroidManifest file, and that was causing the runtime error.
Hi I'm making an App that checks internet connection, if it's not connected it goes to an activity that has an error message and a button that I want to link to the wireless and network settings. But I'm not sure how to do it, can anyone help me ?
Here's what I've got so far.
public class NetworkActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.networkact);
Button settings = (Button) findViewById(R.id.btn_settings);
// Listening to button event
settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Starting a new Intent
Intent gotoSettings = new Intent(getApplicationContext(),
HomeActivity.class);
startActivity(gotoSettings);
}
});
}
}
At the moment it goes to another activity but I want it to go to the wireless & network settings.
I believe, what you want is this:
btn = (Button)this.findViewById(R.id.ButtonNet);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
});
If you use Settings.ACTION_SETTINGS then user can go in both settings mobile network and wifi.
you can use this code for open page mobile network
startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));