我正在开发Android应用程序。使用LandingPage和SignIn / SignUp部分。我有一些景观模式的问题。
对于方法setFontType()和addSoundtoButtons(),引用“普通”按钮的部分正在运行,而引用“横向”按钮的部分不起作用。
对于字体方法,我收到以下错误消息: java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.Button.setTypeface(android.graphics.Typeface)'
对于声音方法,我收到以下错误消息: java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'
以下是代码:
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);
}
}
}
答案 0 :(得分:0)
为简单起见,假设您在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_Normal
和SignUp_Button_Normal
将位于布局中LogIn_Button_Landscape
和SignUp_Button_Landscape
将位于布局中通常,处理此问题的方法是确保两个不同的布局对每个元素使用相同的id
。也就是说,您的每个布局都应该只有LogIn_Button_Normal
,而不是LogIn_Button_Landscape
和LogIn_Button
。这样,无论两种布局中的任何一种都膨胀,您总会有一个LogIn_Button
按钮。
因此,在两种布局中将您的ID更改为LogIn_Button
和SignUp_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);
}
}
}