我在main.xml文件和rows.xml文件中有以下代码。 main.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>
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>
我想在每8行添加一个textview(从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++;
}
}
}
}
答案 0 :(得分:0)
你没有任何带有id信息的线性布局,所以当程序试图在这里找到它时
View linearLayout = findViewById(R.id.info);
代码将中断
您尝试从代码中访问listview @+id/info
,但在onCreate
的代码中,您将内容视图设置为R.layout.main
,因此代码会尝试查找@+id/info
在main.xml中,代码将找不到它,因为你在rows.xml中声明了它,解决方法是你可以尝试动态地添加rows.xml(使用inflater)或者我喜欢创建另一个视图的方式扩展表格布局(或其他布局并创建自己的TableLayout
)并使用view.addView
动态添加另一个视图的java文件。
这是一个简单的例子(示例扩展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构造函数 - &gt;循环多少行并添加行 - &gt;每次行构造函数调用 - &gt;循环将添加多少个单元
我希望你理解我的解释,至少为什么你的代码会给你错误。