2015-04-30 41 views
0

我有延伸的F類公共類A訪問時,類A有一個方法Aa.Then我有一個公共類B,其具有內部BB的公共方法BB I有一個對象k A類和D類實現接口E。在類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

你能不能張貼一些代碼來說明問題類型A? – Bubletan

+0

我已經給出了整個代碼 –

+0

你可以用實際的類名更新你的問題,所以我們可以找出你在問什麼? – nasch

回答

0

當然,F類沒有Aa方法。如果k是A的一個實例(但是放入F類具有多態性),那麼您可以像這樣投射:((F)k).Aa

0

您試圖調用Object上的方法a()F,當a()A類的方法。它僅在該類中定義,而不在F的基類中定義。

可以鑄造((F)k).a();稱之爲也許考慮對你的變量k如使用A k = new A();

+0

我已經作出的變量k A型..但它仍然不工作.. –

+0

你在內部類註釋行不是方法中 - 將其放在裏面的方法之前,你可以叫它 –