StartApp 广告未在列表项点击时显示

StartApp Ads not showing on listitem click

当 android 中的新 activity 意图时,我想在列表项的点击侦听器上显示 StartApp Ads。但是按照下面的方式对我不起作用。

在第一个方法代码中可以,但横幅不显示并且在 second method 中显示横幅正在抛出 NullPointerException

// 第一种方法

startAppAd.showAd();
startAppAd.loadAd();

//第二种方法

        startAppAd.loadAd(new AdEventListener() {
            @Override
            public void onReceiveAd(Ad ad) {
                System.out.println("Ad received");
                Log.d("MISSION", "on load To Receive Ad");
                startAppAd.showAd();
            }
            @Override
            public void onFailedToReceiveAd(Ad arg0) {

                 Log.d("MISSION", "on Failed To Receive Ad");
            }
        });

异常:

09-01 15:33:23.345: E/AndroidRuntime(25452): java.lang.NullPointerException
09-01 15:33:23.345: E/AndroidRuntime(25452):    at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
09-01 15:33:23.345: E/AndroidRuntime(25452):    at com.startapp.android.publish.StartAppAd.load(StartAppSDK:133)
09-01 15:33:23.345: E/AndroidRuntime(25452):    at com.startapp.android.publish.StartAppAd.loadAd(StartAppSDK:316)
09-01 15:33:23.345: E/AndroidRuntime(25452):    at com.startapp.android.publish.StartAppAd.loadAd(StartAppSDK:289)
09-01 15:33:23.345: E/AndroidRuntime(25452):    at   com.rptl.customgrammartopic.list.GrammarTopics.onItemClick(GrammarTopics.java:129)
09-01 15:33:23.345: E/AndroidRuntime(25452):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)

完整代码说明:

主要活动:-

public class MainActivity extends Activity {

private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggles;
private String[] menuTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    StartAppSDK.init(this, "*********", true);

    setContentView(R.layout.activity_main);
}
}

Class 其中包含 listView

public class GrammarTopics extends Fragment {

  private StartAppAd startAppAd = new StartAppAd(getActivity());


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.grammartopics, null);
    ListView listview = (ListView)view.findViewById(R.id.lv_grammartopic);
    listview.setAdapter(new CustomAdapter(getActivity(),
        grammartopics_array, grammardes_array,icon_array));

    listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        if (position == 0) {

            // First method to be show Ads.
            Intent i = new Intent(getActivity(), Articles.class);
            startActivity(i);
            startAppAd.showAd();
            startAppAd.loadAd();

        } else if (position == 1) {

            // Second method to be show Ads.
            Intent i = new Intent(getActivity(), Adjectives.class);
            startActivity(i);

            startAppAd.loadAd(new AdEventListener() {
                @Override
                public void onReceiveAd(Ad ad) {
                    Log.d("MISSION", "on load To Receive Ad");
                    startAppAd.showAd();
                }

                @Override
                public void onFailedToReceiveAd(Ad arg0) {
                      Log.d("MISSION", "on Failed To Receive Ad");
                }
             });
        }

   @Override
   public void onResume() {
       super.onResume();
       startAppAd.onResume();
   }

   @Override
   public void onPause() {
        super.onPause();
        startAppAd.onPause();
   }

   @Override
   public void onBackPressed() {
        startAppAd.onBackPressed();
        super.onBackPressed();
   }

清单文件:-

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

     <activity
        android:name="com.startapp.android.publish.list3d.List3DActivity"
        android:theme="@android:style/Theme" />
    <activity
        android:name="com.startapp.android.publish.OverlayActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@android:style/Theme.Translucent" />
    <activity
        android:name="com.startapp.android.publish.FullScreenActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:theme="@android:style/Theme" />

我关注 here 中的 StartApp 文档。

getActivityonCreateView 之前会导致 NullpointerException.

您正在变量声明部分初始化startAppAd

如果您在 onCreateView().

之前使用 getActivity() 方法,该方法将 return 无效

改成这样

   public class GrammarTopics extends Fragment {

  private StartAppAd startAppAd;


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.grammartopics, null);

    startAppAd = new StartAppAd(getActivity());

   //..remaining code
}