gpt4 book ai didi

java - 不幸的是(android应用程序)已经停止了。空指针异常

转载 作者:行者123 更新时间:2023-12-01 18:34:19 25 4
gpt4 key购买 nike

嗨,每当我尝试运行此应用程序时,我都会收到它已停止的错误消息。在 LogCat 上,我在 OnCreate 方法和行中的“setUpImage()”行上收到空指针异常。 “relativeLayout.addView(电话)”。我现在只是使用考拉和水母图像作为占位符,所以不用担心这些。只是应用程序启动后,图像没有显示在屏幕上。然后应用程序就会因该消息而崩溃。我知道这很愚蠢,但如果您能帮助我,那就太好了...她是我的主要 Activity

<小时/>
package com.example.togglesilentmode;

import android.graphics.Point;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;


public class MainActivity extends Activity {

int displayWidth, displayHeight;
RelativeLayout relativeLayout;
boolean mPhoneIsSilent=true;

private AudioManager mAudioManager;

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

mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);

checkIfPhoneIsSilent();
setUpImage();

relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);

}

//SETTING UP THE IMAGE
private void setUpImage(){

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
displayWidth = size.x;
displayHeight = size.y;

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);

params.leftMargin = displayWidth/4;
params.topMargin = displayHeight/4;

ImageView phone = new ImageView(this);
phone.setOnClickListener(myOnClickLister);
phone.setLayoutParams(params);

if (mPhoneIsSilent) phone.setImageResource(R.drawable.jellyfish);
else phone.setImageResource(R.drawable.koala);

relativeLayout.addView(phone); <----- NULL POINTER EXCEPTION
}

//IMAGE ON CLICK LISTENER
OnClickListener myOnClickLister = new OnClickListener() {

@Override
public void onClick(View v) {

if(mPhoneIsSilent) {


mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;

}else {


mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent = true;

}
setUpImage();

}



};

//CHECK IF PHONE IS SILENT
private void checkIfPhoneIsSilent(){

int ringerMode = mAudioManager.getRingerMode();

if (ringerMode == mAudioManager.RINGER_MODE_SILENT) mPhoneIsSilent = true;
else mPhoneIsSilent = false;
}

//ON RESUME METHOD
@Override
protected void onResume(){
super.onResume();

checkIfPhoneIsSilent();
setUpImage();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

And here is my activity_main.xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >



</RelativeLayout>

最佳答案

在检索相对布局之前,您正在调用 setUpImage。那时它仍然是null

所以你必须首先找到你的布局,然后你可以调用setUpImage()

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

mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);


relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);

checkIfPhoneIsSilent();
setUpImage();

}

NullPointerException 通常很容易解决。既然您识别了它,只需逐步执行您的代码,看看为什么它没有正确初始化。

关于java - 不幸的是(android应用程序)已经停止了。空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22745143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com