我嘗試清理項目並使用膨脹,但它沒有工作。 但也許我沒有使用充氣權。 我需要一些幫助來使用充氣權或任何想法來使指針正確。findViewById在自定義視圖中返回null
這裏是代碼
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其它指針均爲空
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[[email protected]]
我不知道發生了什麼。 任何人有任何想法?
我認爲,因爲在您的初始方法中沒有活動視圖。這意味着android不知道在哪裏可以找到這些id。查看main = anotherView.findViewById(R.layout.activity_main); – 2014-09-04 07:03:59
你在哪裏打電話給initial()?如果在構造函數中它的錯誤地方,請嘗試onFinishInflate() – pskink 2014-09-04 07:22:45
請發佈您的'FaceGridView'構造函數。 – laalto 2014-09-04 07:50:20