我正在使用电报的源代码创建我的应用程序,所以现在我正在尝试将google admob集成到应用程序中但是每当我这样做时,应用程序成功编译并安装但在启动时它会告诉我发生错误并且它关闭,在下面是我的logcat
01-21 20:29:05.413 21932-21932/? W/dalvikvm? threadid=1: calling UncaughtExceptionHandler
01-21 20:29:05.415 21932-21932/? E/AndroidRuntime? FATAL EXCEPTION: main
Process: org.telegram.messenger, PID: 21932
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.telegram.messenger/org.telegram.ui.LaunchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at org.telegram.ui.LaunchActivity.onCreate(LaunchActivity.java:131)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
以下是我的启动活动
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class LaunchActivity extends Activity implements ActionBarLayout.ActionBarLayoutDelegate, NotificationCenter.NotificationCenterDelegate, DialogsActivity.MessagesActivityDelegate {
private boolean finished;
private String videoPath;
private String sendingText;
private ArrayList<Uri> photoPathsArray;
private ArrayList<String> documentsPathsArray;
private ArrayList<Uri> documentsUrisArray;
private String documentsMimeType;
private ArrayList<String> documentsOriginalPathsArray;
private ArrayList<TLRPC.User> contactsToSend;
private int currentConnectionState;
private static ArrayList<BaseFragment> mainFragmentsStack = new ArrayList<>();
private static ArrayList<BaseFragment> layerFragmentsStack = new ArrayList<>();
private static ArrayList<BaseFragment> rightFragmentsStack = new ArrayList<>();
private ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener;
private ActionBarLayout actionBarLayout;
private ActionBarLayout layersActionBarLayout;
private ActionBarLayout rightActionBarLayout;
private FrameLayout shadowTablet;
private FrameLayout shadowTabletSide;
private ImageView backgroundTablet;
protected DrawerLayoutContainer drawerLayoutContainer;
private DrawerLayoutAdapter drawerLayoutAdapter;
private PasscodeView passcodeView;
private AlertDialog visibleDialog;
private Intent passcodeSaveIntent;
private boolean passcodeSaveIntentIsNew;
private boolean passcodeSaveIntentIsRestore;
private boolean tabletFullSize;
private Runnable lockRunnable;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
@Override
protected void onCreate(Bundle savedInstanceState) {
ApplicationLoader.postInitApplication();
NativeCrashManager.handleDumpFiles(this);
Pushbots.sharedInstance().init(this);
Pushbots.sharedInstance().debug(true);
if (!UserConfig.isClientActivated()) {
Intent intent = getIntent();
if (intent != null && intent.getAction() != null && (Intent.ACTION_SEND.equals(intent.getAction()) || intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE))) {
super.onCreate(savedInstanceState);
finish();
return;
}
if (intent != null && !intent.getBooleanExtra("fromIntro", false)) {
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("logininfo", MODE_PRIVATE);
Map<String, ?> state = preferences.getAll();
if (state.isEmpty()) {
Intent intent2 = new Intent(this, IntroActivity.class);
startActivity(intent2);
super.onCreate(savedInstanceState);
finish();
return;
}
}
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
setTheme(R.style.Theme_TMessages);
getWindow().setBackgroundDrawableResource(R.drawable.transparent);
super.onCreate(savedInstanceState);
if (UserConfig.passcodeHash.length() != 0 && UserConfig.appLocked) {
UserConfig.lastPauseTime = ConnectionsManager.getInstance().getCurrentTime();
}
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
AndroidUtilities.statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
actionBarLayout = new ActionBarLayout(this);
drawerLayoutContainer = new DrawerLayoutContainer(this);
setContentView(drawerLayoutContainer, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
AdView mAdView = new AdView(this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
drawerLayoutContainer.addView(mAdView);
mAdView.loadAd(adRequest);
应用程序不再崩溃,但广告也没有显示。这是我的logcat
01-22 20:02:04.530 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.545 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.545 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.550 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.551 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.566 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.567 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.571 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.571 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.594 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.594 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.607 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.607 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.621 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.621 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.628 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.628 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.645 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.645 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.648 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.649 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.665 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.665 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.670 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.670 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.685 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.685 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.690 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.690 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.706 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.706 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.710 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.710 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.726 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.726 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.731 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.732 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.747 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.749 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.753 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.754 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.769 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.769 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
01-22 20:02:04.789 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw
01-22 20:02:04.789 12233-12233/org.telegram.messenger W/AwContents﹕ onDraw mClearViewActive = false
01-22 20:02:04.802 12233-12233/org.telegram.messenger D/AwContents﹕ setContentsSize 400, 50
01-22 20:02:04.803 12233-12233/org.telegram.messenger D/AwContents﹕ setPageScaleFactor 1.0
答案 0 :(得分:0)
您试图在不设置布局
的情况下获取AdView
AdView mAdView = (AdView) findViewById(R.id.adView);
在onCreate
LaunchActivity
方法中移动以下代码
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdView.loadAd(adRequest);
在
drawerLayoutContainer = new DrawerLayoutContainer(this);
setContentView(drawerLayoutContainer, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
<强>更新强>
根据您的评论,您在# 171 行中收到错误。
原因:您没有从xml加载布局而不是在运行时创建。当您尝试获取AdView
时,它将返回null,因为它在活动的当前布局中不可用。
这样做(保持以前的更改完整,这是额外的)
AdView mAdView = new AdView(this);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
drawerLayoutContainer.addView(mAdView);
mAdView.loadAd(adRequest);