2011-03-28 43 views
0

你好EclipeE要初始化變量:安卓:試圖提取上相對佈局的畫布和Eclipse要初始化不應該需要它

CustomDrawableView mCustomDrawableView;

任何想法?

乾杯

菲爾

這裏是我的代碼:

package com.android.phil.graphtoggle; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.OvalShape; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageButton; 
import android.widget.RelativeLayout; 

public class MainActivity extends Activity 
{ 
    public int graph_toggle = 0; 
    public int data_toggle=0; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final ImageButton graph_toggle_button = (ImageButton) findViewById(R.id.graph_toggle); 
     final ImageButton graph_settings_button = (ImageButton) findViewById(R.id.graph_type); 
     final ImageButton data_toggle_button = (ImageButton) findViewById(R.id.data_toggle); 

     CustomDrawableView mCustomDrawableView; 

     RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.relativeLayout1); 

     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,200); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 

     // add here other layout params rules to make your 
     // custom view stay below the buttons 

     mCustomDrawableView.setLayoutParams(lp); 
     mainLayout.addView(mCustomDrawableView); 

     graph_toggle_button.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View arg0) 
      { 
       if (graph_toggle==2) 
       { 
        graph_toggle=0; 
       } 
       else 
       { 
        graph_toggle++; 
       } 

       if (graph_toggle==0) 
       { 
        graph_settings_button.setImageResource(R.drawable.close); 
       } 
       if (graph_toggle==1) 
       { 
        graph_settings_button.setImageResource(R.drawable.ohlc_bars); 
       } 
       if(graph_toggle==2) 
       { 
        graph_settings_button.setImageResource(R.drawable.candles); 
       }    
      }   
     }); 
     data_toggle_button.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View arg0) 
      { 
       if (data_toggle==2) 
       { 
        data_toggle=0; 
       } 
       else 
       { 
        data_toggle++; 
       } 

       if (data_toggle==0) 
       { 
        data_toggle_button.setImageResource(R.drawable.ohlc_bars_daily); 
       } 
       if (data_toggle==1) 
       { 
        data_toggle_button.setImageResource(R.drawable.ohlc_bars_weekly); 
       } 
       if(data_toggle==2) 
       { 
        data_toggle_button.setImageResource(R.drawable.ohlc_bars_monthly); 
       }    
      }   
     }); 
    } 
    public class CustomDrawableView extends View 
    { 
     private ShapeDrawable mDrawable; 

     public CustomDrawableView(Context context) 
     { 
      super(context); 

      int x = 10; 
      int y = 100; 
      int width = 300; 
      int height = 50; 

      mDrawable = new ShapeDrawable(new OvalShape()); 
      mDrawable.getPaint().setColor(0xff74AC23); 
      mDrawable.setBounds(x, y, x + width, y + height); 
     } 

     protected void onDraw(Canvas canvas) 
     { 
       mDrawable.draw(canvas); 
     } 
    } 
} 
+0

「......並希望文摘初始化不應該需要它的變量。」是的,它應該被初始化。這不是日食,它是編譯器。 – ernazm 2011-03-28 15:35:36

回答

0

您還沒有初始化mCustomDrawableView任何地方,所以mCustomDrawableView.setLayoutParams(lp);會引起NullPointerException

你缺少像

mCustomDrawableView = (CustomDrawableView) this.findViewById(R.id.my_custom_view); 
+0

這是一個編譯錯誤,甚至不是運行時錯誤... – ernazm 2011-03-28 15:33:49

+0

是的。編譯器知道它會拋出一個NPE,因爲它沒有被初始化,所以它不允許它編譯。 – 2011-03-28 15:34:26

+0

那麼在XML中,我需要首先定義視圖? – Phil 2011-03-28 15:37:45