0

我在 main.xml 文件和 rows.xml 文件中有以下代码。主要的.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="*" >

    <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>

行.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>

我想每 8 行添加一个带有星期几的文本视图(从 0 开始)。如果我想将 textview 添加到 main.xml 文件中,我可以添加 textview,但是如果我将相同的代码放在 rows.xml 文件中(并将其从 main.xml 文件中删除),则应用程序将无法运行(没有出现错误,但 Eclipse 会显示调试选项卡,其中包含以下内容:)

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

动态文本视图的代码

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++;
        }
    }
}
}
4

1 回答 1

0

你没有任何带有 id 信息的线性布局,所以当程序试图在这里找到它时

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

代码将中断

您尝试@+id/info从您的代码中访问列表视图,但在您的代码中,onCreate您将内容视图设置为,R.layout.main因此代码将尝试@+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构造函数->循环多少行并添加行->每次调用行构造函数->循环将添加多少个单元格

我希望你至少能理解我的解释,为什么你的代码会给你错误。

于 2013-01-18T05:57:52.933 回答