0

I have a problem with my application. It crashes each time whenever I launch my app on an Emulator and I switch to landscape mode. But it work fine in portrait mode. Please what can i do to solve this. Thanks in advance!

I put "android:configChanges="orientation|screenSize|keyboardHidden" in the activity declaration in my manifest but still crash.

This is the error:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a1697759.projetintra/com.example.a1697759.projetintra.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

This is my java code:

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import static com.example.a1697759.projetintra.R.id.etTotalPrix;

public class MainActivity extends AppCompatActivity {

Button btnInscription;
EditText etQuantite;
Spinner spinner;
Spinner spinner2;    


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnInscription = (Button) findViewById(R.id.btnInscription);
    etQuantite = (EditText) findViewById(R.id.etQuantite);
    EditText etTotalPrix;
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner2 = (Spinner) findViewById(R.id.spinner2);

    // Use a TextWatcher to disable/enable button
    etQuantite.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int 
arg3) {}
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
int arg3) {}

        @Override
        public void afterTextChanged(Editable string) {
            if(string.length() > 0) {
                btnInscription.setEnabled(true);
            } else {
                btnInscription.setEnabled(false);
            }
        }
    });


    btnInscription.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){

            //Demarrer le nouvelle activite
            Intent intent = new Intent(MainActivity.this, 
ResumeActivity.class);

            //Spinner 1
            Bundle b = new Bundle();
            b.putString("name", spinner.getSelectedItem().toString());
            intent.putExtras(b);

            //Spinner 2
            Bundle b2 = new Bundle();
            b2.putString("name2", spinner2.getSelectedItem().toString());
            intent.putExtras(b2);

            //EditText Quantite
            Bundle q = new Bundle();
            q.putString("name3", etQuantite.getText().toString());
            intent.putExtras(q);


            //Reset spinner and editText
            spinner.setSelection(0);
            spinner2.setSelection(0);
            etQuantite.setText("");

            Toast.makeText(MainActivity.this, "Vous avez fait votre 
commande",Toast.LENGTH_SHORT).show();

            startActivity(intent);

        }
    });
  }
}

E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.a1697759.projetintra, PID: 21317
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a1697759.projetintra/com.example.a1697759.projetintra.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4974)
    at android.app.ActivityThread.-wrap21(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6688)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.a1697759.projetintra.MainActivity.onCreate(MainActivity.java:56)
    at android.app.Activity.performCreate(Activity.java:6912)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4974) 
    at android.app.ActivityThread.-wrap21(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6688) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
bkausbk
  • 2,740
  • 1
  • 36
  • 52

1 Answers1

0

You have to create a landscape layout for that.

  • Just go to the design tab in your xml file
  • click on the Layout Variants button.
  • Select the Create Landscape Variation

a landscape layout will be created. Then you are free to change the layout according to your requirements.