- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
实际上,我正在尝试制作一个具有免费和付费风格的应用程序。当我使用简单的 findViewById 方法进行绑定(bind)时,它工作正常。但是当我尝试添加 butterknife 时,我拼命坚持使用 butterknife 。
我正在尝试通过 MainActivity.java 文件中的 butterknife 绑定(bind)来绑定(bind)内容(例如 AdMob 或按钮)。但它首先显示空指针异常错误,它显示在 AdMob 对象空指针异常上。我运行干净的项目,然后它显示在 button.onClickListener Nullpointer 异常上。
请帮我...
这是错误:显示 AdMob nullpointerException:
09-08 19:04:30.860 19466-19466/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.santossingh.jokeapp.free, PID: 19466
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.santossingh.jokeapp.free/com.santossingh.jokeapp.free.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
at com.santossingh.jokeapp.free.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-08 19:04:35.652 837-2192/system_process E/Surface: getSlotFromBufferLocked: unknown buffer: 0xf2cf6c90
09-08 19:04:56.030 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:06:20.986 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:07:41.011 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:09:01.042 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:10:20.982 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:11:46.019 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:13:06.051 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:14:41.018 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
09-08 19:16:06.047 837-853/system_process E/BluetoothAdapter: Bluetooth binder is null
public class MainActivity extends AppCompatActivity implements AsyncResponse{
@BindView(R.id.avi) AVLoadingIndicatorView avLoadingIndicatorView;
@BindView(R.id.button_jokeTeller) Button button_JokeTeller;
@BindView(R.id.instruction_TextView)
TextView instruction;
@BindView(R.id.container) RelativeLayout relativeLayout;
@BindView(R.id.adView) AdView adView;
@BindView(R.id.progressBar) LinearLayout linearLayout;
private InterstitialAd mInterstitialAd;
EndpointsAsyncTask endpointsAsyncTask;
private static final String JOKE_TAG="joke";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
endpointsAsyncTask = new EndpointsAsyncTask(this);
AdRequest adRequestBanner = new AdRequest.Builder().build();
adView.loadAd(adRequestBanner);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
AdRequest adRequestInterstitial = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestInterstitial);
button_JokeTeller.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInterstitial();
}
});
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
showProgressbar(true);
showJoke();
}
@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLeftApplication() {
showProgressbar(true);
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdOpened() {
Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show();
}
});
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
public void showJoke(){
endpointsAsyncTask.execute(getString(R.string.keyword));
}
@Override
public void processFinish(String result) {
Intent intent = new Intent(this, JokeActivity
.class)
.putExtra(JOKE_TAG, result);
showProgressbar(false);
startActivity(intent);
}
public void showProgressbar(boolean a){
if(a==true){
relativeLayout.setVisibility(View.GONE);
linearLayout.setVisibility(View.VISIBLE);
}else{
linearLayout.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0e73a6"
android:padding="10dp"
tools:context="com.santossingh.jokeapp.free.MainActivity"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<com.wang.avi.AVLoadingIndicatorView
android:id="@+id/avi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AVLoadingIndicatorView"
android:visibility="visible"
app:indicatorName="BallPulseIndicator"
/>
</LinearLayout>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/instructions"
android:textColor="#fff"
android:id="@+id/instruction_TextView"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_jokeTeller"
android:layout_below="@+id/instruction_TextView"
android:text="@string/button_text"
/>
<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"/>
</RelativeLayout>
</RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application>
<activity android:name="com.santossingh.jokeapp.free.MainActivity"
>
<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent"/>
</manifest>
最佳答案
确保仅在 setContentView 命令 之后添加三个 adMob 行
否则 - 此命令将返回 空 值(value) :
mAdView = **findViewById(R.id.adView);**
setContentView(R.layout.activity_item_list); // <== make asure this comes **BEFORE** the Admob mAdView commands
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
关于android - 如何使用 Butterknife 解决 NullPointerException void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39401662/
我正在尝试将 Google Admob 合并到我的 android 应用程序中。我已按照 https://developers.google.com/admob/android/quick-start
我有一个画廊应用程序,有一些类别:动物,花卉......我创建了一个 Activity 的每个画廊,所有画廊中的这个 Activity 具有相同的内容: onCreate: tSpeak = new
我正在尝试在我的 Activity 中通过 admob 制作广告,它工作正常,但在移动一些东西后,我出现了这个错误。 代码如下:第一次遇到这种情况,重启eclipse也没用。 java.lang.Cl
我正在开发一个 Android 应用程序,但在我添加 AdView 后它显示错误 Error:(23, 31) No resource found that matches the given nam
我在我的应用程序中使用 adview 来测试某些健康状况,但显示的广告与状况或健康问题完全无关。由于制药和健康行业的广告很多,我确信应该有“匹配”的广告——我的应用程序似乎没有调用它们! 有没有办法“
我有一个带有 View 和导航栏的导航 Controller 。 我想在导航栏和 View 之间插入一个 AdView(我正在使用 AdWhirl)。 AdView 应该向下滑动并将 View 向下推
我试图在执行 Activity 时在 AdsView 下方放置一个按钮 - download_btn。
我正在尝试将 AdView 放在我的应用程序的底部。但是,当我测试我的应用时,我没有看到广告,也没有看到 AdView。 在我的 MainActivity 中,我用 2 个 fragment 替换 2
我有: @Override public void onActivityCreated( Bundle bundle ) { super.onActivityC
我想在我的应用程序中包含一个 admob 横幅,但我遇到了这个奇怪的错误。非常感谢任何帮助。 这是xml代码 在 Activity 中,我写了如下代码 adview = (AdView)findVi
我有一个 Surfaceview 和一个 AdView,我是这样画 Adview 的: g=new GameView(this ); setContentView(R.layout.a
我的应用程序有一些“GameOver” Activity ,游戏结束时会调用该 Activity 。此 Activity 包含它自己的 AdView。现在我的问题是,每次玩家输掉游戏并且调用此 Act
我想在这个适配器的第二个位置添加一个广告 View 。我必须为此改变什么?在过去的两个小时里,我尝试了很多。我可以在加载列表后插入吗? 这是我的适配器代码: public class FeedsAda
在 XML 布局中,一切看起来都很完美。当我使用模拟器或真实设备时,adView不可见。我尝试为 adView 设置高度,例如 200 dp。然后 adView 就可见了。但是,当我为 adView
我在我的应用程序中使用了一个广告 View ,想知道何时有人点击了该广告 View !我该怎么做?onclicklistener 似乎不起作用。 最佳答案 你可以这样设置监听器: adView.set
我在 Layout 的 XML 中定义了一个 AdView,如果用户购买了应用程序的无广告版本,我想让它消失。 我在做的是 if(userhaspurchased){ if (mAdView
我正在使用 AdView 和 LeakCanary。承载 adView 的 Fragment 在 onDestroy 中调用 adView.destroy(),但 LeakCanary 显示承载该 F
我是 Android 开发的初学者。我刚刚发布了一个应用程序,但发现了一些问题,因此我想通过创建一个新的 Android 项目来纠正这些问题,该项目的包名称与已发布的应用程序的包名称相同。但是,一旦我
我想在我的应用程序中添加 AdMob,但在我的应用程序中找不到 AdView 类 build.gradle ... dependencies { api ('com.google.android
我最近在我的应用程序中添加了一个广告 View 。但dint对admob一无所知。我收到错误,因为我没有将其与 admobId 链接。所以我从布局中删除了广告 View 。但同样的错误又来了。我认为
我是一名优秀的程序员,十分优秀!