/ / Android: findViewById sa vracia null? java, android, null, findviewbyid

Android: findViewById sa vracia null? java, android, null, findviewbyid

V mojej hlavnej činnosti sa snažím získať prístup k programu LinearLayout, ktorý mám vo svojom hlavnom programe ScrollView.My ScrollView je implementovaný nasledovne:

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

<LinearLayout
android:id="@+id/graph"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
/>



</ScrollView>

V mojej hlavnej činnosti nafúkam scrollView a potom sa pokúsim získať prístup k lineárnemu rozloženiu takto:

LayoutInflater inflater;
inflater = (LayoutInflater)  this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

ScrollView mainLayout = (ScrollView) inflater.inflate(R.layout.scrollview,
null);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph);

Prečo by sa moja FindViewById vrátila nula? Najskôr som si myslela, že sa to pokúša nájsť skôr, než skončí nafúknutie, ale snažil som sa chvíľu očakávať, že dokončí nafukovanie a to ani nepomôže.

odpovede:

4 pre odpoveď č. 1

musíte mu dať nafúknuté usporiadanie

LinearLayout layout = (LinearLayout) mainLayout.findViewById(R.id.graph);

0 pre odpoveď č. 2

Môžete skontrolovať, či najprv zadajte nesprávne usporiadanie v inflater.inflate ().