这是我的班级和布局.. testlayout dot java
package com.example.project1;
import android.app.ListActivity;
public class testLayout extends ListActivity
{
Project1 mAct;
void setNewWindow()
{
mAct=Project1.getMAct(); //Project1 context (Project1 - MainActivity)
mAct.setContentView(R.layout.testlayout);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
setListAdapter(new MyPerformanceArrayAdapter(mAct, values));
}
}
testlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
MyPerformanceArrayAdapter.java
package com.example.project1;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Project1 context1;
private final String[] names1;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public MyPerformanceArrayAdapter(Project1 context, String[] names) {
super(Project1.getMAct(), R.layout.rowlayout, names);
context1 = Project1.getMAct();
names1 = names;
Log.v("4", "GOOD");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
Log.v("3", "GOOD");
// reuse views
if (rowView == null) {
LayoutInflater inflater = Project1.getMAct().getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.label);
viewHolder.image = (ImageView) rowView.findViewById(R.id.icon);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names1[position];
holder.text.setText(s);
Log.v("2", "GOOD");
if (s.startsWith("Windows7") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
holder.image.setImageResource(R.drawable.no);
} else {
holder.image.setImageResource(R.drawable.ok);
}
Log.v("1", "GOOD");
return rowView;
}
}
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:layout_weight="21.23"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="21.23" >
</ListView>
</LinearLayout>
和ERROR的
at android.app.Activity.getSystemService(Activity.java:4410)
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.Activity.setContentView(Activity.java:1867)
at android.app.ListActivity.ensureList(ListActivity.java:312)
at android.app.ListActivity.setListAdapter(ListActivity.java:263)
at com.example.project1.testLayout.setNewWindow(testLayout.java:19)
at com.example.project1.SecondLayout$2.onClick(SecondLayout.java:65)
什么是错的?
那是我的SecondView.java
package com.example.project1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class SecondLayout
{
private static Project1 mAct;
testLayout tL= new testLayout();
public void setNewWindow()
{
mAct = Project1.getMAct();
mAct.setContentView(R.layout.secondlayout);
TextView textView1 = (TextView) mAct.findViewById(R.id.textView4);
TextView textView2 = (TextView) mAct.findViewById(R.id.textView5);
ResClass res = Project1.getMAct().getRes();
textView1.setText(res.getV1("status"));
textView2.setText(res.getV1(2,"posts", "author"));
int len= res.getLen();
List<Map<String, String>> arList = new ArrayList<Map<String,String>>();
for (int i=0; i<len; i++)
{
Map <String, String> resMap = new HashMap <String, String>();
resMap.put("authors", res.getV1(i, "posts", "author"));
resMap.put("titles", res.getV1(i, "posts", "title"));
arList.add(resMap);
}
ListView listView1= (ListView) mAct.findViewById(R.id.listView1);
SimpleAdapter adp = new SimpleAdapter (mAct, arList, android.R.layout.simple_list_item_2, new String[] {"authors","titles"}, new int[] {android.R.id.text1, android.R.id.text2});
listView1.setAdapter(adp);
res.resFree();
Button button1 = (Button) mAct.findViewById(R.id.button4);
Button button2 = (Button) mAct.findViewById(R.id.button5);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mAct.StartWindow();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.v("ON CLICK", "GOOD");
tL.setNewWindow();
}
});
}
}
Project1是我的MainActivity.java类 我从SecondLayout打电话给TestLayout getMAct - 给出Project1 prj1 = this;
public static Project1 getMAct()
{
return prj1;
}