景观模式错误



我正在开发一个Android应用程序。在着陆页和标志/注册部分上工作。我在景观模式上有一些问题。

对于方法,setFontType((和添加tobuttons((涉及"正常"按钮的零件正在运行,而涉及" landscape"按钮的部分不正常。

对于字体方法,我会收到以下错误消息:java.lang.nullpointerexception:尝试调用虚拟方法'void android.widget.button.settypeface(android.graphics.typeface('null对象参考

对于声音方法,我会收到以下错误消息:java.lang.nullpoInterException:尝试调用虚拟方法'void android.widget.button.setonClickListener(android.view.view.view $ onclicklistener('null对象参考

以下是代码:

public class LandingPage extends AppCompatActivity {
Button log_in_normal, sign_up_normal, log_in_landscape, sign_up_landscape;
Typeface tfc_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_landing_page);
    log_in_normal = (Button) findViewById(R.id.LogIn_Button_Normal);
    sign_up_normal = (Button) findViewById(R.id.SignUp_Button_Normal);
    log_in_landscape = (Button) findViewById(R.id.SignUp_Button_Landscape);
    sign_up_landscape = (Button) findViewById(R.id.LogIn_Button_Landscape);
    setFontType();
    addSoundtoButtons();
}
public void addSoundtoButtons(){
    //Add Sound to the Buttons
    final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.button_click_sound);
    log_in_normal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaPlayer.start();
        }
    });
    sign_up_normal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaPlayer.start();
        }
    });
    log_in_landscape.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaPlayer.start();
        }
    });
    sign_up_landscape.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaPlayer.start();
        }
    });
}
public void setFontType(){
    //Set Font Type for Buttons
    tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");
    log_in_normal = (Button) findViewById(R.id.LogIn_Button_Normal);
    sign_up_normal = (Button) findViewById(R.id.SignUp_Button_Normal);
    log_in_landscape = (Button) findViewById(R.id.LogIn_Button_Landscape);
    sign_up_landscape = (Button) findViewById(R.id.SignUp_Button_Landscape);
    log_in_normal.setTypeface(tfc_button);
    sign_up_normal.setTypeface(tfc_button);
    log_in_landscape.setTypeface(tfc_button);
    sign_up_landscape.setTypeface(tfc_button);
}
public void OnClick(View view){
    if (view.getId() == R.id.LogIn_Button_Normal){
        Intent intent = new Intent(this, SignInPage.class);
        startActivity(intent);
    }else if (view.getId() == R.id.SignUp_Button_Normal){
        Intent intent = new Intent(this, SignUpPage.class);
        startActivity(intent);
    }else if (view.getId() == R.id.LogIn_Button_Landscape){
        Intent intent = new Intent(this, SignInPage.class);
        startActivity(intent);
    }else if (view.getId() == R.id.SignUp_Button_Landscape){
        Intent intent = new Intent(this, SignUpPage.class);
        startActivity(intent);
    }
}
}

为了简单起见,想象一下您在res/layout/目录中有此布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/LogIn_Button_Normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log in"/>
    <Button
        android:id="@+id/SignUp_Button_Normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign up"/>
</LinearLayout>

此外,您在res/layout-land/目录中有此布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/LogIn_Button_Landscape"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log in"/>
    <Button
        android:id="@+id/SignUp_Button_Landscape"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign up"/>
</LinearLayout>

要理解的关键之处在于,您的活动只会膨胀这些之一(基于设备的方向(,而不是两者兼而有之。这意味着:

  • 如果设备处于肖像模式,则LogIn_Button_NormalSignUp_Button_Normal将在布局中
  • 如果设备处于景观模式,则LogIn_Button_LandscapeSignUp_Button_Landscape将在布局中
  • 无论您的设备所在的方向如何

通常,处理此操作的方法是确保您的两个不同的布局为每个元素使用相同的id s。也就是说,您的每个布局都不应具有LogIn_Button,而不是具有LogIn_Button_NormalLogIn_Button_Landscape。这样,无论两种布局都被夸大了哪个,您将始终有一个LogIn_Button按钮。

因此,在两个布局中将您的ID更改为LogIn_ButtonSignUp_Button,然后将代码更改为:

public class LandingPage extends AppCompatActivity {
    Button log_in, sign_up;
    Typeface tfc_button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing_page);
        log_in = (Button) findViewById(R.id.LogIn_Button);
        sign_up = (Button) findViewById(R.id.SignUp_Button);
        setFontType();
        addSoundtoButtons();
    }
    public void addSoundtoButtons() {
        //Add Sound to the Buttons
        final MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.button_click_sound);
        log_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.start();
            }
        });
        sign_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.start();
            }
        });
    }
    public void setFontType() {
        //Set Font Type for Buttons
        tfc_button = Typeface.createFromAsset(getAssets(), "fonts/TEMPSITC.TTF");
        log_in.setTypeface(tfc_button);
        sign_up.setTypeface(tfc_button);
    }
    public void OnClick(View view) {
        if (view.getId() == R.id.LogIn_Button) {
            Intent intent = new Intent(this, SignInPage.class);
            startActivity(intent);
        }
        else if (view.getId() == R.id.SignUp_Button) {
            Intent intent = new Intent(this, SignUpPage.class);
            startActivity(intent);
        }
    }
}

最新更新