在listitem点击中未显示StartApp广告

时间:2015-09-01 11:09:57

标签: android fragment ads startapp

我想在android中新建一个活动时,在列表项的点击监听器上显示 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$1.onItemClick(GrammarTopics.java:129)
09-01 15:33:23.345: E/AndroidRuntime(25452):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)

完整的代码说明:

MainActivity: -

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);
}
}

包含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文档。

1 个答案:

答案 0 :(得分:1)

getActivity之前的

onCreateView会导致NullpointerException

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

如果您在getActivity()之前使用onCreateView()方法,则 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 } 方法将返回null。

像这样改变

unprocessedItems