2014-03-30 47 views
0

嗨每當我嘗試運行這個應用程序,我收到它已停止的錯誤消息。在LogCat上,我在OnCreate方法和行中的「setUpImage()」行上得到一個空指針異常。 「relativeLayout.addView(電話)」。我現在只是使用圖像考拉和海蜇作爲佔位者,所以不要擔心這些。只是應用程序啓動,圖像不顯示在屏幕上。然後,該應用程序只是與該消息崩潰。我知道這是愚蠢的東西,但它會是非常好的,如果你能幫我......她是我的主要活動不幸的是(android應用程序)已停止。空指針異常


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> 

回答

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通常很容易解決。既然您已經確定了它,那麼請逐步執行您的代碼並查看爲什麼尚未正確初始化。

相關問題