I’m working on an Android project using API 7 and I’m trying to start a ViewPager, but the app keeps crashing whenever I try to create the activity that uses the ViewPager from the main activity. I’ve checked all my resources and they’re in their proper directories, but I’m stumped.
Here’s the activity that attempts to start the ViewPager:
public class World extends Activity{
MediaPlayer muzak;
Boolean mSwitch;
final Preferencer pp = (Preferencer)getApplicationContext();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
if(pp.getMuzak()){
muzak = MediaPlayer.create(World.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
Button zoom = (Button) findViewById(R.id.subbutton);
zoom.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Subworld.class);
startActivityForResult(myIntent, 0);
}
});
}
@Override
protected void onPause(){
super.onPause();
if(pp.getMuzak()){
muzak.release();
}
}
}
And the main activity that starts this activity is as follows:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), World.class);
startActivityForResult(myIntent, 0);
}
});
final ImageButton tog = (ImageButton) findViewById(R.id.button2);
tog.setTag(2);
tog.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final int status = (Integer) view.getTag();
if (status == 1){
tog.setImageResource(R.drawable.ic_lock_silent_mode_off);
view.setTag(2);
}
else if (status == 2){
tog.setImageResource(R.drawable.ic_lock_silent_mode);
view.setTag(1);
}
}
});
}
}
When I tried pasting the same exact code to initialize the ViewPager into the main activity, it works just fine, but I would much prefer to start the ViewPager in a separate activity so that when the user hits the hard back button, it will go back to the main activity. But I suppose putting that snippet of code back into the main activity would be okay, it’s just my only problem is that the ViewPager contains layouts that are supposed to have buttons that also start new activites in them. So if there’s a work around that too, that would be good to know.
I also know that the custom class “preferencer” that I call in both activities works, so I doubt that’s the problem.
I’ve searched the internet for a few days now and I am really clueless at this point. Sorry about the massive amounts of text and terrible formatting, I’m a newb here, so pardon my newbness. Any insight on this would be very greatly appreciated. Thanks!
Edit:
Here is the logcat stack trace:
01-30 12:23:00.958: D/dalvikvm(227): GC freed 711 objects / 55944 bytes in 158ms
01-30 12:23:12.808: D/AndroidRuntime(227): Shutting down VM
01-30 12:23:12.818: W/dalvikvm(227): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-30 12:23:12.818: E/AndroidRuntime(227): Uncaught handler: thread main exiting due to uncaught exception
01-30 12:23:12.848: E/AndroidRuntime(227): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.Hey.namespace/com.Hey.namespace.World}: java.lang.NullPointerException
01-30 12:23:12.848: E/AndroidRuntime(227): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.os.Looper.loop(Looper.java:123)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-30 12:23:12.848: E/AndroidRuntime(227): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 12:23:12.848: E/AndroidRuntime(227): at java.lang.reflect.Method.invoke(Method.java:521)
01-30 12:23:12.848: E/AndroidRuntime(227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-30 12:23:12.848: E/AndroidRuntime(227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-30 12:23:12.848: E/AndroidRuntime(227): at dalvik.system.NativeStart.main(Native Method)
01-30 12:23:12.848: E/AndroidRuntime(227): Caused by: java.lang.NullPointerException
01-30 12:23:12.848: E/AndroidRuntime(227): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
01-30 12:23:12.848: E/AndroidRuntime(227): at com.Hey.namespace.World.<init>(World.java:16)
01-30 12:23:12.848: E/AndroidRuntime(227): at java.lang.Class.newInstanceImpl(Native Method)
01-30 12:23:12.848: E/AndroidRuntime(227): at java.lang.Class.newInstance(Class.java:1479)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-30 12:23:12.848: E/AndroidRuntime(227): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
01-30 12:23:12.848: E/AndroidRuntime(227): ... 11 more
01-30 12:23:12.888: I/dalvikvm(227): threadid=7: reacting to signal 3
01-30 12:23:12.929: I/dalvikvm(227): Wrote stack trace to '/data/anr/traces.txt'
You are using
findViewByIdinsideonCreateof yourWorldActivity, but you did not callsetContentViewbefore it.