实际上,我正在努力制作一款免费且付费的应用程序。当我使用简单的findViewById方法绑定时,它工作正常。但是当我尝试添加butterKnife时,我拼命地坚持使用butterKnife。
我正在尝试通过MainActivity.java文件中的butterknife绑定绑定内容(例如AdMob OR Button)。但它首先显示Nullpointer异常错误,它显示在AdMob对象nullpointer异常上。我运行干净的项目然后它显示在button.onClickListener Nullpointer异常。
请帮帮我......
以下是错误:显示AdMob nullpointerException:
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);
}
}
}
2-MainActivity.java
<?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>
3- activity_main.xml文件
<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>
4-清单文件
function map(n,i,o,r,t){return i>o?i>n?(n-i)*(t-r)/(o-i)+r:r:o>i?o>n?(n-i)*(t-r)/(o-i)+r:t:void 0}
答案 0 :(得分:1)
你错过了
adView = (AdView)this.findViewById(R.id.adView);
更改以下代码
AdRequest adRequestBanner = new AdRequest.Builder().build();
**adView = (AdView)this.findViewById(R.id.adView);**
adView.loadAd(adRequestBanner);
更新
的依赖关系dependencies { compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' }
答案 1 :(得分:0)
正确答案是我使用了gradle 2.2.0或更新版本。所以感谢JakeWarton github链接[https://github.com/JakeWharton/butterknife]which清楚地描述了Butterknife与新版gradle的集成。 因此,在较新版本的gradle中逐步实施butterKnife如下:
1-首先,添加classpath&#39; com.neenbedankt.gradle.plugins:android-apt:1.8&#39;依赖关系:
dependencies {
....
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
2-其次,添加build.gradle应用程序模块butterKnife
dependencies {
....
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
总而言之,当我们使用gradle 2.2.0或更新版本时,我们需要添加上述更改。这就是它,很容易...... :)
答案 2 :(得分:0)
我遇到了类似的问题。当我在资源文件中仔细检查时,我的活动有两个资源文件:
\res\layout\activity_main_lauch.xml
\res\layout-v21\activity_main_lauch.xml
我正在修改单个文件,因此它抛出了错误。当我在两个文件中应用更改时,它开始工作。
答案 3 :(得分:0)
AdView adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
在几行代码后设置此代码,有时加载广告和Adview会花费一些时间。 因此,在几行代码之后添加此代码,以获取结果并尝试再次运行。
答案 4 :(得分:0)
确保仅在setContentView命令之后添加三个adMob行
否则-此命令将返回 null 值:
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);