无法从嵌套类访问方法

时间:2015-04-30 16:27:10

标签: java android

我有一个扩展了类F的公共类A,类A有一个方法Aa。然后我有一个公共类B,它在Bb中有一个公共方法Bb我有一个类A的对象k和一个D类实现了一个接口E.Iinside class DI无法访问k.Aa.为什么?请帮助

package com.example.sreeharsh.layoutcreate;



import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class LDObservableScrollView extends ScrollView {





    private MainActivity.LDObservableScrollViewListener scrollViewListener = null;

    public LDObservableScrollView(Context context) {
        super(context);
    }

    public LDObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LDObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(MainActivity.LDObservableScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }







    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if(scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

}





package com.example.sreeharsh.layoutcreate;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TextView;



public class MainActivity extends ActionBarActivity {


    public interface LDObservableScrollViewListener {

        void onScrollChanged(LDObservableScrollView scrollView, int x, int y, int oldx, int oldy);

    }



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





        final LDObservableScrollView scroll=new LDObservableScrollView(this);




        final LinearLayout layout =new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lparams.setMargins(0, 10, 0, 10);
        lparams.gravity=Gravity.CENTER;




        int i;

        for(i=0;i<5;i++) {

            ImageView imView = new ImageView(this);
            imView.setLayoutParams(lparams);

            Drawable new_image = getResources().getDrawable(R.drawable.rakthasakshi);
            imView.setBackgroundDrawable(new_image);

            layout.addView(imView, lparams);

        }
        scroll.addView(layout);
        setContentView(scroll);


        class DetectHere implements LDObservableScrollViewListener {


                scroll.setScrollViewListener(this);   //******* here cannot access the method setScrollViewListener .. 


            @Override



            public void onScrollChanged(LDObservableScrollView scrollView, int x,
                                        int y, int oldx, int oldy) {
                // TODO Auto-generated method stub
                if ((scrollView.getHeight() + y) >= layout.getHeight()) {


                    ImageView imView = new ImageView(MainActivity.this);
                    imView.setLayoutParams(lparams);

                    Drawable new_image = getResources().getDrawable(R.drawable.rakthasakshi);
                    imView.setBackgroundDrawable(new_image);

                    layout.addView(imView, lparams);



                }
            }
        }    


    }



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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }





}

2 个答案:

答案 0 :(得分:0)

当然,F级没有Aa方法。如果k是A的一个实例(但是放入具有多态性的F类),那么你可以这样强制转换:((F)k).Aa

答案 1 :(得分:0)

a() 时,您尝试在类Object的{​​{1}}上调用方法Fa()。它仅在该类中定义,而不是在A的基类中定义。

可以通过投射F 来调用它,或者可以考虑使用((F)k).a();

将变量k设为A类型