addView nullpointerexception admob network

时间:2013-11-16 22:28:37

标签: java android android-layout android-listview admob

添加admob时会出错。如果我删除它工作正常。问题出在哪里,谢谢。

我的问题;

layout.addView(adView); at com.zilsesleri.SoundList.onCreate(SoundList.java:55)

SoundList类;

public class SoundList extends ListActivity {

int [] soundfile;
MediaPlayer mediaPlayer;
final Activity act = this;

public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);

    soundfile= new int[] {R.raw.sound1,R.raw.sound2};

    String[] newsounds = getResources().getStringArray(R.array.newsounds);        
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, newsounds));
    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {     

          Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
          intent.putExtra("position", position);
          startActivity(intent);      
      }
    });

    LinearLayout layout = (LinearLayout)findViewById(R.id.reklam);
    AdView adView = new AdView(this, AdSize.BANNER, "....");
    layout.addView(adView);
     AdRequest request = new AdRequest();
     //request.setTesting(true);
     adView.loadAd(request);

}

List_item.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView 
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="18dip"
android:textStyle="bold" >
</TextView>

<LinearLayout
    android:id="@+id/reklam"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

错误日志;

11-17 00:53:49.700: E/AndroidRuntime(30935): FATAL EXCEPTION: main
11-17 00:53:49.700: E/AndroidRuntime(30935): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zilsesleri/com.zilsesleri.SoundList}: java.lang.NullPointerException
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.os.Looper.loop(Looper.java:137)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.ActivityThread.main(ActivityThread.java:4441)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at java.lang.reflect.Method.invokeNative(Native Method)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at java.lang.reflect.Method.invoke(Method.java:511)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at dalvik.system.NativeStart.main(Native  Method)
11-17 00:53:49.700: E/AndroidRuntime(30935): Caused by: java.lang.NullPointerException
11-17 00:53:49.700: E/AndroidRuntime(30935):    at com.zilsesleri.SoundList.onCreate(SoundList.java:55)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.Activity.performCreate(Activity.java:4465)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-17 00:53:49.700: E/AndroidRuntime(30935):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
11-17 00:53:49.700: E/AndroidRuntime(30935):    ... 11 more

1 个答案:

答案 0 :(得分:0)

堆栈跟踪清楚地给出了答案

Caused by: java.lang.NullPointerException
11-17 00:53:49.700: E/AndroidRuntime(30935):    at com.zilsesleri.SoundList.onCreate(SoundList.java:55)
11-17 00:53:49.700: E/AndroidRuntime(30935

SoundList的第55行是问题所在。