应用程序在横向模式下崩溃?

时间:2017-10-24 12:33:56

标签: android

我的申请有问题。每次我都会崩溃     在模拟器上启动我的应用程序,然后切换到横向模式。但它有效     在肖像模式下很好。请问我能做些什么来解决这个问题。谢谢     预先!

我把“android:configChanges =”orientation | screenSize | keyboardHidden“放在     我的清单中的活动声明但仍然崩溃。

这是错误:java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.a1697759.projetintra / com.example.a1697759.projetintra.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'null对象引用上的void android.view.View.setOnClickListener(android.view.View $ OnClickListener)'

这是我的java代码:

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) 

1 个答案:

答案 0 :(得分:0)

你必须create a landscape layout

  • 只需转到xml文件中的design tab
  • 即可
  • 点击Layout Variants按钮。
  • 选择Create Landscape Variation

将创建横向布局。然后您可以根据自己的要求自由更改布局。