2013-05-31 44 views
1

         您好,我是新來的android(也是java),所以我嘗試創建一些簡單的應用程序。這個應用程序將有一個基礎佈局從XML和動態佈局,這將是從代碼創建,並將插入到該基本佈局。但是,當我使用findViewbyId它不會工作,它返回爲null。
       後,我試圖努力findViewbyId也乾淨項目之前插入「的setContentView」 ......,它仍然得到了空.. 這裏是我的代碼..findViewById仍然返回null(新手)

package com.app.something; 
import android.os.Build; 
import android.os.Bundle; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.GridLayout; 
import android.widget.GridView; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity { 
LinearLayout lLayout; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.activity_main); 
    GridLayout grid = new GridLayout(this); 
    grid.setColumnCount(5); 
    for (int i = 0;i<20;i++){ 
     ImageButton ib = new ImageButton(this); 
     ib.setImageResource(R.drawable.ic_launcher); 
     ib.setId(i+1); 
     final int index = i; 
     ib.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i("TAG","The index is" + index); 
      } 
     }); 
     grid.addView(ib); 
    } 
    lLayout = (LinearLayout)findViewById(R.layout.activity_main); 
    if(lLayout == null)Log.i("TAG", "It said, NULLL"); 
    else { 
     lLayout.addView(grid); 
     setContentView(lLayout); 
    } 

} 

@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; 
} 

} 

,這是基地佈局activity_main(空白)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
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" 
android:background="#ffffff00" 
tools:context=".MainActivity" > 

</RelativeLayout> 
+0

謝謝你的所有評論..沒有它的工作是啊! 我的意思是我忘了添加該ID並設置相同的佈局,以及如何使用setcontentview ..thx 我感謝你的幫助! –

回答

4

Firt你想投一個RelativeLayoutLinearLayout。其次你錯過了id屬性爲您LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
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" 
android:background="#ffffff00" 
android:id="@+id/myId" 
tools:context=".MainActivity" > 

</LinearLayout> 

修復,你可以檢索你的佈局這種方式後:

lLayout = (LinearLayout)findViewById(R.id.myId); 
+0

它就像一個魅力,謝謝 –

+0

不客氣 – Blackbelt

1

變化

lLayout = (LinearLayout)findViewById(R.layout.activity_main); 

RelativeLayout lLayout = (RelativeLayout)findViewById(R.id.activity_main); 

因爲activity_main佈局conatin RelativeLayout佈局根視圖,而不是LinearLayout,你也需要定義android:id爲RelativeLayout的佈局。

,或者你可以做到這一點爲使用LayoutInflater

View lLayout = getLayoutInflater().inflate(R.layout.activity_main); 
if(lLayout == null) 
    Log.i("TAG", "It said, NULLL"); 
else { 
     lLayout.addView(grid); 
     setContentView(lLayout); 
} 
1
lLayout = (LinearLayout)findViewById(R.layout.activity_main); 

1.you必須通過ID爲這一觀點,加android:id屬性相對佈局。 2.you不要有任何的線性佈局在你的代碼 3.you有一個相對佈局投這RelativeLayout

1

不能使用

lLayout = (LinearLayout)findViewById(R.layout.activity_main); 

按照方法名它會查找視圖的視圖ID,而不是像您正在使用的資源ID。您可以通過由佈局使用

android:id="@+id/someid" 

然後從代碼

lLayout = (LinearLayout)findViewById(R.id.someid); 
0

findViewById搜索設置ID訪問視圖,因此如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/layout_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" 
android:background="#ffffff00" 
tools:context=".MainActivity" > 

</RelativeLayout> 

的,你可以使用它:

(RelativeLayout) findViewById(R.id.layout_main);