我的应用有问题。当启动“FusRoDAHnomusic”活动时,它会崩溃。我刚刚接近一支部队?
package com.frede.fusrodah;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class FusRoDAHActivity extends Activity {
private AdView adView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), FusRoDAH.class);
startActivityForResult(myIntent, 0);
}
});
Button next2 = (Button) findViewById(R.id.button4);
next2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), FusRoDAHnomusic.class);
startActivityForResult(myIntent, 0);
}
});
// Create an ad.
adView2 = new AdView(this, AdSize.BANNER, "XXXXXXXXXXXX");
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main1);
layout.addView(adView2);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest();
// Start loading the ad in the background.
adView2.loadAd(adRequest);
}
/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView2 != null) {
adView2.destroy();
}
super.onDestroy();
}
}
package com.frede.fusrodah;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class FusRoDAHnomusic extends Activity {
private AdView adView3;
@Override
public void onBackPressed() {
return;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fusrodahnomusic);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final MediaPlayer mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.fusrodahnomusic);
mediaPlayer.start(); // no need to call prepare(); create() does that for you
Button next = (Button) findViewById(R.id.button3);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mediaPlayer.stop();
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
// Create an ad.
adView3 = new AdView(this, AdSize.BANNER, "XXXXXXXXXX");
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main1);
layout.addView(adView3);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest();
// Start loading the ad in the background.
adView3.loadAd(adRequest);
}
/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView3 != null) {
adView3.destroy();
}
super.onDestroy();
}
}
}
package com.frede.fusrodah;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class FusRoDAH extends Activity {
private AdView adView;
@Override
public void onBackPressed() {
return;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fusrodah);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final MediaPlayer mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.fusrodah);
mediaPlayer.start(); // no need to call prepare(); create() does that for you
Button next = (Button) findViewById(R.id.button2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mediaPlayer.stop();
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
// Create an ad.
adView = new AdView(this, AdSize.BANNER, "XXXXXXXXXXXX");
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.fusrodah);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
};
}
logcat的:
03-31 22:06:05.485: D/webviewglue(797): nativeDestroy view: 0x3ab4f0
03-31 22:06:05.494: D/webviewglue(797): nativeDestroy view: 0x3ab3b8
03-31 22:06:05.505: D/dalvikvm(797): GC freed 393 objects / 17312 bytes in 267ms
03-31 22:06:05.514: E/webcoreglue(797): The real object has been deleted
03-31 22:06:05.544: E/webcoreglue(797): The real object has been deleted
03-31 22:06:07.394: W/MediaPlayer(797): info/warning (1, 44)
03-31 22:06:08.234: D/AndroidRuntime(797): Shutting down VM
03-31 22:06:08.254: W/dalvikvm(797): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-31 22:06:08.274: E/AndroidRuntime(797): Uncaught handler: thread main exiting due to uncaught exception
03-31 22:06:08.345: E/AndroidRuntime(797): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.frede.fusrodah/com.frede.fusrodah.FusRoDAHnomusic}: java.lang.NullPointerException
03-31 22:06:08.345: E/AndroidRuntime(797): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.os.Looper.loop(Looper.java:123)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-31 22:06:08.345: E/AndroidRuntime(797): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 22:06:08.345: E/AndroidRuntime(797): at java.lang.reflect.Method.invoke(Method.java:521)
03-31 22:06:08.345: E/AndroidRuntime(797): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-31 22:06:08.345: E/AndroidRuntime(797): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-31 22:06:08.345: E/AndroidRuntime(797): at dalvik.system.NativeStart.main(Native Method)
03-31 22:06:08.345: E/AndroidRuntime(797): Caused by: java.lang.NullPointerException
03-31 22:06:08.345: E/AndroidRuntime(797): at com.frede.fusrodah.FusRoDAHnomusic.onCreate(FusRoDAHnomusic.java:66)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-31 22:06:08.345: E/AndroidRuntime(797): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-31 22:06:08.345: E/AndroidRuntime(797): ... 11 more
03-31 22:06:08.475: I/dalvikvm(797): threadid=7: reacting to signal 3
03-31 22:06:08.704: I/dalvikvm(797): Wrote stack trace to '/data/anr/traces.txt'
03-31 22:06:54.401: I/Process(797): Sending signal. PID: 797 SIG: 9
答案 0 :(得分:2)
只需阅读您的堆栈跟踪。它在第66行说NullPointerException
。65-66就是这样:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main1);
layout.addView(adView3);
这意味着layout
为null
,这意味着可能main1
不在该布局中。