findViewById在自定义视图中返回null

时间:2014-09-04 06:59:22

标签: java android

我尝试清理项目并使用膨胀,但它没有工作。 但也许我没有使用膨胀权利。 我需要一些帮助来使用膨胀权或任何想法来使指针正确。

这是代码

MainActivity.java - > activity_main.xml - > FaceGridView.java

MainActivity.java

public class MainActivity extends FragmentActivity {
FaceGridView myGridview;

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

        myGridview = (FaceGridView) findViewById( R.id.facegridview);
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.example.ngvvp.FaceGridView
    android:id="@+id/facegridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
   >
   <RelativeLayout
        android:id="@+id/mainRLayout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
    >
         <android.support.v4.view.ViewPager
              android:id="@+id/pager" 
              android:layout_width="fill_parent" 
              android:layout_height="285px"
              android:layout_above="@+id/pagerIndicator"
               /> 

  <com.example.ngvvp.CirclePageIndicator
       android:id="@+id/pagerIndicator" 
       android:layout_width="fill_parent" 
       android:layout_height="15px" 
       android:layout_marginBottom="5dp" 
       android:layout_marginTop="5dp" 
       android:padding="3dp" 
       android:layout_alignParentBottom="true"/>
   </RelativeLayout>

FaceGridView.java

public class FaceGridView extends ViewGroup {
public PageIndicator mIndicator;
private ViewPager awesomePager;
private PagerAdapter pm;
private RelativeLayout mainRLayout;
public Context myContext;
public View MAIN ;

public void initial() {
    MAIN = (View)findViewById(R.layout.activity_main);
    mIndicator = (PageIndicator)findViewById(R.id.pagerIndicator);
    awesomePager = (ViewPager)findViewById(R.id.pager);
    mainRLayout = (RelativeLayout)findViewById(R.id.mainRLayout);
    System.out.println("MAIN["+MAIN+"] \n"+
                       "mIndicator["+mIndicator+"] \n"+
                       "awesomePager["+awesomePager+"] \n"+
                       "mainRLayout["+mainRLayout+"] \n"+
                       "myContext["+myContext+"]\n" );
}
 public FaceGridView(Context context) {
    super(context);
    this.myContext = context;
    initial();
    InsideMethod();
}

public FaceGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.myContext = context;
    initial();
    InsideMethod();
}

除了myContext,其他指针都是null

09-04 14:52:46.644: I/System.out(23503): MAIN[null] 
09-04 14:52:46.644: I/System.out(23503): mIndicator[null] 
09-04 14:52:46.644: I/System.out(23503): awesomePager[null] 
09-04 14:52:46.644: I/System.out(23503): mainRLayout[null] 
09-04 14:52:46.644: I/System.out(23503): myContext[com.example.ngvvp.MainActivity@41ea2858]

我不知道发生了什么。 有人有任何想法吗?

2 个答案:

答案 0 :(得分:0)

你应该这样使用

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.activity_main, parent, false);

 mIndicator = (PageIndicator) row.findViewById(R.id.textView1);
awesomePager = (ViewPager) row.findViewById(R.id.imageView1);

答案 1 :(得分:0)

我的同学尝试的结果可以使指针正确。 它不是我想要的方式,但结果是。它有效。将它发给那些遇到同样问题的人。

<强> MainActivity.java

public class MainActivity extends FragmentActivity {
FaceGridView myGridview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main); 
        myGridview = (FaceGridView) findViewById( R.id.facegridview);
        myGridview.initial(this);
    }

}

<强> FaceGridView.java

public class FaceGridView extends ViewGroup {
public PageIndicator mIndicator;
private ViewPager awesomePager;
private PagerAdapter pm;
private RelativeLayout mainRLayout;
public Context myContext;
public View MAIN ;
ArrayList<Category> codeCategory;

public void initial(Context ctx) {

    this.myContext = ctx;


    mIndicator = (PageIndicator)   ((Activity) myContext).findViewById(R.id.pagerIndicator); 
    awesomePager = (ViewPager)   ((Activity) myContext).findViewById(R.id.pager); 

    System.out.println( "myContext["+myContext+"] \n"+
                        "mIndicator["+mIndicator+"] \n"+
                        "awesomePager["+awesomePager+"] \n" );

    InsideMethod();
}

public FaceGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
    System.out.println( "FaceGridView(Context context, AttributeSet attrs) ["+context+"] ");
//  this.myContext = context;
//        initial();
//        InsideMethod();
}

我第一次在 stackoverflow 上提出问题,我从没想过在如此短的时间内收到这么多回复,你们让我感受到爱情 TAT ,谢谢大家。