2013-01-18 61 views
0

我在main.xml文件和一個rows.xml文件中有以下代碼。 main.xml中動態添加一個textview不工作

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" > 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="0.40" 
      android:gravity="center" 
      android:text="Time"> 
     </TextView> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="Module"> 
     </TextView> 

     <TextView 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="Lecturer"> 
     </TextView> 
    </TableRow> 


    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </ListView> 

    </TableLayout> 

rows.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" > 

    <ListView 
     android:id="@+id/info" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </ListView> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/time" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="0.40" 
      android:gravity="center" > 
     </TextView> 

     <TextView 
      android:id="@+id/module" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center"> 
     </TextView> 

     <TextView 
      android:id="@+id/lecturer" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" > 
     </TextView> 
    </TableRow> 
</TableLayout> 

我想添加一個TextView與周每8行(從0開始)的日子。如果我想將textview添加到main.xml文件中,我可以添加textview,但是如果我將相同的代碼放入rows.xml文件(並將其從main.xml文件中移除),則該應用程序將無法工作(沒有錯誤出現,但日食出現調試選項卡與以下:)

Thread [<1> main] (Suspended (exception RuntimeException)) 
    <VM does not provide monitor information> 
    ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1651  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1667 
    ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117 
    ActivityThread$H.handleMessage(Message) line: 935 
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 130 
    ActivityThread.main(String[]) line: 3687  
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
    Method.invoke(Object, Object...) line: 507 
    ZygoteInit$MethodAndArgsCaller.run() line: 867 
    ZygoteInit.main(String[]) line: 625 
    NativeStart.main(String[]) line: not available [native method] 
Thread [<8> Binder Thread #2] (Running) 
Thread [<7> Binder Thread #1] (Running) 

我在哪裏出錯了,我該如何解決它?

編輯: logcat的

01-18 02:44:14.656: W/ActivityThread(893): Application com.example.timetable is waiting for the debugger on port 8100... 
01-18 02:44:14.664: I/System.out(893): Sending WAIT chunk 
01-18 02:44:14.882: I/System.out(893): Debugger has connected 
01-18 02:44:14.882: I/System.out(893): waiting for debugger to settle... 
01-18 02:44:15.078: I/System.out(893): waiting for debugger to settle... 
01-18 02:44:15.281: I/System.out(893): waiting for debugger to settle... 
01-18 02:44:15.484: I/System.out(893): waiting for debugger to settle... 
01-18 02:44:15.679: I/System.out(893): waiting for debugger to settle... 
01-18 02:44:15.898: I/System.out(893): waiting for debugger to settle... 
01-18 02:44:16.093: I/System.out(893): waiting for debugger to settle... 
01-18 02:44:16.296: I/System.out(893): debugger has settled (1410) 
01-18 02:44:16.476: I/ApplicationPackageManager(893): cscCountry is not German : VDI 

代碼動態的TextView

View linearLayout = findViewById(R.id.info); 

int a = 0; 
while(x < 41) 
{ 
      //The textview is only printed out every 8 rows because there is only 8 classes a day and the day is printed out at the start 
    if(x == 0 || x == 8 || x == 16 || x == 24 || x == 32) 
    { 

      TextView valueTV = new TextView(this); 
      valueTV.setText(fetch2.get(a)); //fetch2 has the days of the week stored in it 
      valueTV.setId(x); 
      valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
      a++; 

     ((LinearLayout) linearLayout).addView(valueTV); 
     x++; 
    } 
    else 
    { 
     lv = (ListView) findViewById(R.id.listview); 
     adapter = new CustomAdapter(MainActivity.this, R.id.listview, fetch); //fetch has the time, module and lecturer stored 
     lv.setAdapter(adapter); 
     x++; 
    } 
} 

編輯:Main.java

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.LinearLayout; 


public class Main extends Activity { 
private ListView lv; 
private CustomAdapter adapter; 
private ArrayList<Custom> fetch = new ArrayList<Custom>(); 
private ArrayList<String> fetch2 = new ArrayList<String>(); 
private int x = 0; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    fetch2.add("Monday"); 
    fetch2.add("Tuesday"); 
    fetch2.add("Wednesday"); 
    fetch2.add("Thursday"); 
    fetch2.add("Friday"); 


    InputStream inputStream = getResources().openRawResource(R.raw.timetable); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

    int ctr; 
    try { 
     ctr = inputStream.read(); 
     while (ctr != -1) { 
      byteArrayOutputStream.write(ctr); 
      ctr = inputStream.read(); 

     } 
     inputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 

     //Parse the data into jsonobject to get original data in form of json. 
     JSONObject jObject = new JSONObject(byteArrayOutputStream.toString()); 

        //Grabs the data for each day 
     JSONArray Monday = jObject.getJSONArray("Monday"); 
     JSONArray Tuesday = jObject.getJSONArray("Tuesday"); 
     JSONArray Wednesday = jObject.getJSONArray("Wednesday"); 
     JSONArray Thursday = jObject.getJSONArray("Thursday"); 
     JSONArray Friday = jObject.getJSONArray("Friday"); 

     String time= ""; 
     String module = ""; 
     String lecturer = ""; 

        //Adding each days data 
     for (int i = 0; i < Monday.length(); i++) 
     { 
      time = Monday.getJSONObject(i).getString("time"); 
      module = Monday.getJSONObject(i).getString("module"); 
      lecturer = Monday.getJSONObject(i).getString("lecturer"); 
      Custom a = new Custom(time, module, lecturer); 
      fetch.add(a); 
     } 
     for (int i = 0; i < Tuesday.length(); i++) 
     { 
      time = Tuesday.getJSONObject(i).getString("time"); 
      module = Tuesday.getJSONObject(i).getString("module"); 
      lecturer = Tuesday.getJSONObject(i).getString("lecturer"); 
      Custom a = new Custom(time, module, lecturer); 
      fetch.add(a); 
     }  
     for (int i = 0; i < Wednesday.length(); i++) 
     { 
      time = Wednesday.getJSONObject(i).getString("time"); 
      module = Wednesday.getJSONObject(i).getString("module"); 
      lecturer = Wednesday.getJSONObject(i).getString("lecturer"); 
      Custom a = new Custom(time, module, lecturer); 
      fetch.add(a); 
     } 
     for (int i = 0; i < Thursday.length(); i++) 
     { 
      time = Thursday.getJSONObject(i).getString("time"); 
      module = Thursday.getJSONObject(i).getString("module"); 
      lecturer = Thursday.getJSONObject(i).getString("lecturer"); 
      Custom a = new Custom(time, module, lecturer); 
      fetch.add(a); 
     } 
     for (int i = 0; i < Friday.length(); i++) 
     { 
      time = Friday.getJSONObject(i).getString("time"); 
      module = Friday.getJSONObject(i).getString("module"); 
      lecturer = Friday.getJSONObject(i).getString("lecturer"); 
      Custom a = new Custom(time, module, lecturer); 
      fetch.add(a); 
     }    

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    View linearLayout = findViewById(R.id.info); 

    int a = 0; 
    while(x < 41) 
    { 
        //The textview is only printed out every 8 rows because there is only 8 classes a day and the day is printed out at the start 
     if(x == 0 || x == 8 || x == 16 || x == 24 || x == 32) 
     {  
      TextView valueTV = new TextView(this); 
      valueTV.setText(fetch2.get(a)); 
      valueTV.setId(x); 
      valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
      a++; 

      ((LinearLayout) linearLayout).addView(valueTV); 
     } 
     else 
     { 
        //Adding each days data 
      lv = (ListView) findViewById(R.id.listview); 
      adapter = new CustomAdapter(MainActivity.this, R.id.listview, fetch); 
      lv.setAdapter(adapter); 
      x++; 
     } 
    } 
} 
} 
+1

你能告訴我logcat錯誤日誌嗎?和你的代碼動態添加,所以也許我可以知道你的代碼出錯的地方:) –

+0

沒有logcat錯誤,但我已經添加了這個代碼和動態textview 。 – Mary

+0

其中是LinearLayout和View linearLayout = findViewById(R.id.info); – NaserShaikh

回答

0

你沒有用的ID信息,因此任何線性佈局當程序試圖在這裏找到它時

View linearLayout = findViewById(R.id.info); 

的代碼將打破

您嘗試從您的代碼,但在您設置的內容視圖R.layout.mainonCreate代碼訪問列表視圖@+id/info這樣的代碼將嘗試找到@+id/info在main.xml中並且代碼不會找到它,因爲您在rows.xml中聲明瞭它,解決方案是您可以嘗試動態添加rows.xml(使用inflater)或者我喜歡使用擴展表的java文件創建另一個視圖佈局(或其他佈局並創建您自己的TableLayout)並使用view.addView動態添加另一個視圖。

這兒有你一個簡單的例子(這個例子擴展LinearLayout

public class ChildMyBadge extends LinearLayout { 

private Context context; 
private ChildTopModel contentModel; 

public ChildMyBadge(Context context) { 
    super(context); 
} 

public ChildMyBadge(Context context, ChildTopModel contentModel) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    this.context = context; 
    this.setOrientation(VERTICAL); 
    this.contentModel = contentModel; 
    LayoutParams lpMyBadge = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    lpMyBadge.gravity = Gravity.CENTER; 
    this.setLayoutParams(lpMyBadge); 
    this.setGravity(Gravity.CENTER); 
    initView(); 
} 

private void initView() { 

    LinearLayout llContainerInfo = new LinearLayout(context); 
    llContainerInfo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,Util.getDisplayWidth(context)/20)); 
    llContainerInfo.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
    TextView tvInfo = new TextView(context); 
    LayoutParams lpInfo = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    lpInfo.setMargins(Util.getDisplayWidth(context)/100, Util.getDisplayWidth(context)/100, 
      Util.getDisplayWidth(context)/100, Util.getDisplayWidth(context)/100); 
    tvInfo.setLayoutParams(lpInfo); 
    tvInfo.setText(R.string.MyBadgeInfo); 
    tvInfo.setTextSize(7); 

    llContainerInfo.addView(tvInfo); 
    this.addView(llContainerInfo); 
    ArrayList<Integer> temp_friend_id = new ArrayList<Integer>(); 
    ArrayList<Integer> friend_id = contentModel.getFriend_id(); 
    Log.i(BaseID.TAG, "Size : " + friend_id.size()); 
    for(int i=0;i<8;i++) 
    { 
     temp_friend_id.add(friend_id.get(i)); 
    } 
    Log.i(BaseID.TAG, temp_friend_id.toString()); 
    this.addView(new ChildMyBadgeRow(context,1,temp_friend_id)); 
    for(int j=0;j<4;j++) 
    { 
     temp_friend_id.clear(); 
     for(int i=(8+j*6);i<(14+j*6);i++) 
     { 
      temp_friend_id.add(friend_id.get(i)); 
     } 
     Log.i(BaseID.TAG, temp_friend_id.toString()); 
     this.addView(new ChildMyBadgeRow(context, 2,temp_friend_id)); 
    } 
    } 
} 

你可以看到我在動態添加我的行,每一行也將動態地增加該小區這樣的過程將是:

MAINVIEW構造 - >循環多少行並添加行 - >每次排構造方法中調用 - >循環多少個單體電池將被添加

我希望你明白我的解釋至少,爲什麼你的代碼給你的錯誤。

+0

當我將ListView更改爲LinearLayout時,會發生同樣的情況,但是當我將它放入main.xml文件中時卻起作用,但當我將它放入rows.xml文件中時它不起作用(調試選項卡出現) – Mary

+0

你知道我要去哪裏嗎? – Mary

+0

我仍然無法找到您在xml中放置@ + id/info的位置? –