2014-09-30 129 views
1

爲了在運行時動態顯示加載視圖,我創建了一個self_pro_bar.xml並創建了一個普通的java類ShyProgressBar。將自定義視圖添加到代碼的主視圖中,然後,如果在Activity類中調用findViewById,它可以找到添加的自定義視圖,但它會從普通Java類調用findViewById,它總是返回null。在Activity之外調用findViewById返回null

self_pro_bar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/selfprogressbar" 
    android:gravity="center_horizontal" 
    android:layout_marginTop="20dp" 
    android:layout_width="100dp" 
    android:layout_height="30dp" 
    android:orientation="horizontal" 
    android:background="#eee"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="loading..." 
     /> 

</LinearLayout> 

ShyProgressBar類

public class ShyProgressBar { 
     private Context context; 
     public ShyProgressBar(Context context) { 
      this.context = context; 
     } 

     public View get() { 
      LayoutInflater inflater = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      return inflater.inflate(R.layout.self_pro_bar, null); 
     } 

     public boolean exist(Activity activity) { 
      return activity.findViewById(R.layout.self_pro_bar) != null; 
     } 
} 

主要業務XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="load_data" 
     android:text="@string/click_me" /> 

    <LinearLayout 
     android:id="@+id/dataview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    </LinearLayout> 

</RelativeLayout> 

主要活動

public class MainActivity extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
     } 
     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.main, menu); 
      return true; 
     } 
     public void load_data(View v) { 
      ShyProgressBar ins = new ShyProgressBar(this); 
      View p_b = ins.get(); 
      if(!ins.exist(MainActivity.this)) { 
       LinearLayout rl = (LinearLayout) findViewById(R.id.dataview); 
       rl.addView(p_b); 
       Object c = ins.exist(MainActivity.this) // this code still returns null! 
      } 
     } 
} 
  1. 不工作 - 在ShyProgressBar中調用findViewById時,對象c值仍然爲空。
  2. 如果使用below語句判斷視圖是否存在於Activity類的load_data方法中, 查看p_b_v = findViewById(R.id.selfprogressbar);它工作正常,值不爲空。

不確定爲什麼findViewById在活動中工作,但不適用於非活動類。

+0

爲什麼有人會提出我的問題?我怎麼知道誰投入了?我對此很生氣! – user2574853 2014-09-30 12:19:50

+0

你不知道布羅冷靜下來:) – 2014-09-30 12:22:30

+0

@BatuhanC,這是一個不用任何理由獻身的好行爲...感謝您的意見。 – user2574853 2014-09-30 12:23:27

回答

0

因爲findViewById(R.id.dataview)是Activity類的一個方法。

它會在您使用Activity擴展您的課程時出現。 如果你想在非活動類中使用方法參數,你可以傳遞它。 例如: -

void methodName(Activity activity){ 
} 

,並同時通過使用YourActivity.this

+0

請參閱我的代碼,我已經將Activity作爲參數傳遞,但它不起作用。 – user2574853 2014-09-30 12:22:09

+0

我已經通過許多關於這個問題的線索,並嘗試了很多方法,但他們都沒有爲我工作。 – user2574853 2014-09-30 12:24:47

+0

您正在傳遞Activity作爲參數,但您沒有使用它:) – Okas 2014-09-30 12:25:28

1

你的存在的方法是不正確的。在那裏你再次膨脹你的Main_activity.xml,然後檢查R.layout.self_pro_bar是否存在。這當然不是,因爲這個項目不是Main_activity佈局的一部分。一旦完成此項檢查,您就會丟棄剛膨脹的佈局 - 您不會將其附加到任何地方。不需要

法「存在」,相反在您的活動代碼,您可以檢查是否self_pro_bar已經加入到主要佈局與下面的代碼:

public void load_data(View v) { 
    ShyProgressBar ins = new ShyProgressBar(this); 
    if(findViewById(R.id.selfprogressbar) == null) { 
     LinearLayout rl = (LinearLayout) findViewById(R.id.dataview); 
     View p_b = ins.get(); 
     rl.addView(p_b); 
     } 
    } 
+0

這是我的錯誤,即使我修復了代碼,它仍然返回null。請參閱我更新的代碼。謝謝。 – user2574853 2014-09-30 12:30:40

+1

您正在檢查錯誤的ID。你正在尋找R.layout.self_pro_bar,而應該尋找R.id.selfprogressbar。 R.layout.self_pro_bar是項目中佈局資源的標識,R.id.selfprogressbar是您要添加的RelativeLayout的標識。 – Okas 2014-09-30 12:34:28

+0

夥計,你救了我的一天!我愚蠢的錯誤!非常感謝!!我真的想投你兩次! :-) – user2574853 2014-09-30 12:38:45

0

不能使用findViewById方法Activity之外,但如果你想使用TextView,Button或其他只是在活動oncreate中聲明並將其設置爲靜態方法。從該靜態方法調用組件。

UPDATE 第1步:

內活動的onCreate方法()

Button b= (Button) findViewById(R.id.yourComponentID); 

步驟2:

創建類:

public class xyz{ 
Button x ; 
public static void save_Button(Button b){ 
x = b; 
} 
public static Button get_button(){ 
return x; 
} 
} 

步驟3: 在您的活動調用此方法save_Button e.g

save_Button(b); 

第4步:

你可以叫get_button()從您的應用程序的任何地方。

+0

對不起,您能寫一個例子嗎?你的意思是我必須爲self_pro_bar創建一個Activity來匹配self_pro_bar.xml? – user2574853 2014-09-30 12:30:03

+0

不太明白「將其設置爲靜態方法,從該靜態方法調用組件」 – user2574853 2014-09-30 12:34:18