Android:尝试在相对布局上绘制画布,而 Eclipse 想要初始化一个不需要它的变量

发布于 2024-10-27 12:28:32 字数 3987 浏览 1 评论 0原文

你好EclipeE想要初始化:

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);
        }
    }
}

Hello EclipeE wants to initialise:

CustomDrawableView mCustomDrawableView;

Any ideas?

Cheers

Phil

Here's my code:

    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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2024-11-03 12:28:32

您没有在任何地方初始化 mCustomDrawableView,因此 mCustomDrawableView.setLayoutParams(lp); 将导致 NullPointerException

你缺少类似的东西

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

You aren't initializing mCustomDrawableView anywhere so mCustomDrawableView.setLayoutParams(lp); is going to cause a NullPointerException.

You are missing something like

mCustomDrawableView = (CustomDrawableView) this.findViewById(R.id.my_custom_view);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文