不幸的是(Android应用程序)已停止。空指针异常

时间:2014-03-30 14:27:46

标签: java android nullpointerexception

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


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>

1 个答案:

答案 0 :(得分:4)

在检索相对布局之前,您正在调用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通常很容易解决。由于您已经确定了它,因此只需逐步执行代码,看看为什么没有正确初始化它。