我的 Admob 的布局 XML 导致了一些错误。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxxxxxxx"
ads:loadAdOnCreate="true" />
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/sailor" />
<TextView
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/trans"
android:padding="10dp"
android:textColor="@color/menu_text"
android:textSize="22sp"
android:textStyle="normal" />
</LinearLayout>
活动
public class ListViewOne extends Activity {
// we use a string to hold the name of our extra,
// it must include the full package name
public final static String ID_EXTRA = "com.faarn.navyslang._ID";
private DatabaseHelper dbDataBaseHelper = null;
private Cursor ourCursor = null;
private DataBaseAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// this is our ListView element, obtained by id from our XML Layout
ListView myListView = (ListView) findViewById(R.id.list_view);
// create our database Helper
dbDataBaseHelper = new DatabaseHelper(this);
// we call the create right after initializing the helper, just in
// case
// they have never run the app before
dbDataBaseHelper.createDatabase();
//
// open the database!! Our helper now has a SQLiteDatabase database
// object
dbDataBaseHelper.openDataBase();
// get our cursor. A cursor is a pointer to a dataset, in this case
// a set of results from a database query
ourCursor = dbDataBaseHelper.getCursor();
// tell android to start managing the cursor
// we do this just incase our activity is interrupted or ends, we
// want the activity
// to close and deactivate the cursor, as needed
startManagingCursor(ourCursor);
// create our adapter
adapter = new DataBaseAdapter(ourCursor);
// set the adapter!!!
myListView.setAdapter(adapter);
// this is how we know what to do when a list item is clicked
myListView.setOnItemClickListener(onListClick);
} catch (Exception e) {
// this is the line of code that sends a real message to the Log
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
// this is the line that prints out the location
// the code where the error occurred.
e.printStackTrace();
}
}
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Create our intent, as per usual
Intent i = new Intent(ListViewOne.this, ListViewTwo.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("ListViewTwo", i);
}
};
class DataBaseAdapter extends CursorAdapter {
DataBaseAdapter(Cursor c) {
super(ListViewOne.this, c);
}
@Override
// this is a CursorAdapter
// instead of Using a getView and if(row=null)
// we use a bindView and newView calls
// we can get away with this because CursorAdapters have
// a default implementation of getView that calls bindView and newView
// as needed. This makes our code a bit cleaner, and is the better way
// to
// do this
public void bindView(View row, Context ctxt, Cursor c) {
DataBaseHolder holder = (DataBaseHolder) row.getTag();
holder.populateFrom(c, dbDataBaseHelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.main, parent, false);
DataBaseHolder holder = new DataBaseHolder(row);
row.setTag(holder);
return (row);
}
}
static class DataBaseHolder {
private TextView name = null;
DataBaseHolder(View row) {
name = (TextView) row.findViewById(R.id.row);
}
void populateFrom(Cursor c, DatabaseHelper r) {
name.setText(r.getName(c));
}
}
}
Admob 正如预期的那样位于顶部,但也出现在我列表的每一行之间。我已经查看了很多关于此的其他问题,但无法纠正它。任何帮助表示赞赏。