I'm developing an android app with a splash screen. I want another activity which is a tab swiper to be started soon after the splash screen and next go to the main activity.
When I run the following code, my splash screen works fine but then the app stops without showing the tabswiper or the main activity. How should I change my code to get it working? Any suggestions would be highly valued.
My main activity: MainActivity.java
package lk.sidadiya.sidadiya;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(MainActivity.this, WelcomeScreen.class);
startActivity(i);
// close this activity
finish();
}
My tabswiper: WelcomeScreen.java
package lk.sidadiya.sidadiya;
import lk.sidadiya.tabswipe.adapter.TabsPagerAdapter;
import lk.sidadiya.sidadiya.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class WelcomeScreen extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
My splash screen: SplashScreen.java
package lk.sidadiya.sidadiya;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lk.sidadiya.sidadiya"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="lk.sidadiya.sidadiya.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Welcome screen -->
<activity
android:name="lk.sidadiya.sidadiya.WelcomeScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black" >
<intent-filter>
<action android:name="lk.sidadiya.sidadiya.WelcomeScreen" />
<!-- <category android:name="android.intent.category.DEFAULT" /> -->
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="lk.sidadiya.sidadiya.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LAUNCHER" />
</intent-filter>
</activity>
-->
</application>
</manifest>
logcat:
04-20 12:09:21.327: I/Process(1684): Sending signal. PID: 1684 SIG: 9
04-20 12:27:56.267: D/dalvikvm(1755): GC_FOR_ALLOC freed 54K, 5% free 2808K/2948K, paused 300ms, total 303ms
04-20 12:27:57.737: D/gralloc_goldfish(1755): Emulator without GPU emulation detected.
04-20 12:28:01.567: D/dalvikvm(1755): GC_FOR_ALLOC freed 9K, 3% free 3311K/3404K, paused 230ms, total 231ms
04-20 12:28:02.997: I/Choreographer(1755): Skipped 38 frames! The application may be doing too much work on its main thread.
04-20 12:28:04.037: D/AndroidRuntime(1755): Shutting down VM
04-20 12:28:04.037: W/dalvikvm(1755): threadid=1: thread exiting with uncaught exception (group=0xb2ad3ba8)
04-20 12:28:04.187: E/AndroidRuntime(1755): FATAL EXCEPTION: main
04-20 12:28:04.187: E/AndroidRuntime(1755): Process: lk.sidadiya.sidadiya, PID: 1755
04-20 12:28:04.187: E/AndroidRuntime(1755): java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.sidadiya.sidadiya/lk.sidadiya.sidadiya.WelcomeScreen}: java.lang.NullPointerException
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.os.Looper.loop(Looper.java:136)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 12:28:04.187: E/AndroidRuntime(1755): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:28:04.187: E/AndroidRuntime(1755): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 12:28:04.187: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 12:28:04.187: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 12:28:04.187: E/AndroidRuntime(1755): at dalvik.system.NativeStart.main(Native Method)
04-20 12:28:04.187: E/AndroidRuntime(1755): Caused by: java.lang.NullPointerException
04-20 12:28:04.187: E/AndroidRuntime(1755): at lk.sidadiya.sidadiya.WelcomeScreen.onCreate(WelcomeScreen.java:34)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.Activity.performCreate(Activity.java:5231)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-20 12:28:04.187: E/AndroidRuntime(1755): ... 11 more
activity_main.xml:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Related
I am trying to make a navigation drawer but every time it's executed the application crashes. I am a beginner. Please, let me know if you need any more info and thanks
MainActivity.java
package com.pixalstudio.navigationdrawer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends Activity implements OnItemClickListener {
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] mTitles;
DrawerLayout mDrawerLayout;
ListView mDrawerList;
mTitles = getResources().getStringArray(R.array.drawerItems);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_listview);
mDrawerList.setAdapter((ListAdapter) new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mTitles));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.closeDrawers();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void selectItem(int position) {
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, LocationSelection.class);
startActivity(i);
break;
default:
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, LocationSelection.class);
startActivity(i);
break;
default:
}
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pixalstudio.navigationdrawer.MainActivity" >
<RelativeLayout
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#757575" >
</RelativeLayout>
<ListView
android:id="#+id/drawer_listview"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FF9800"
android:choiceMode="singleChoice" >
</ListView>
</android.support.v4.widget.DrawerLayout>
LogCat
07-07 16:53:20.301: D/OpenGLRenderer(1775): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-07 16:53:20.305: D/(1775): HostConnection::get() New Host Connection established 0xb42d7750, tid 1775
07-07 16:53:20.322: D/Atlas(1775): Validating map...
07-07 16:53:20.398: D/libEGL(1775): loaded /system/lib/egl/libEGL_emulation.so
07-07 16:53:20.399: D/libEGL(1775): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-07 16:53:20.414: D/libEGL(1775): loaded /system/lib/egl/libGLESv2_emulation.so
07-07 16:53:20.427: D/(1775): HostConnection::get() New Host Connection established 0xaf039480, tid 1793
07-07 16:53:20.638: I/OpenGLRenderer(1775): Initialized EGL, version 1.4
07-07 16:53:20.728: D/OpenGLRenderer(1775): Enabling debug mode 0
07-07 16:53:20.900: W/EGL_emulation(1775): eglSurfaceAttrib not implemented
07-07 16:53:20.900: W/OpenGLRenderer(1775): Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035860, error=EGL_SUCCESS
07-07 16:53:20.940: D/AndroidRuntime(1775): Shutting down VM
07-07 16:53:20.940: D/AndroidRuntime(1775): --------- beginning of crash
07-07 16:53:20.941: E/AndroidRuntime(1775): FATAL EXCEPTION: main
07-07 16:53:20.941: E/AndroidRuntime(1775): Process: com.pixalstudio.navigationdrawer, PID: 1775
07-07 16:53:20.941: E/AndroidRuntime(1775): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.pixalstudio.navigationdrawer/com.pixalstudio.navigationdrawer.LocationSelection}: java.lang.ClassCastException: com.pixalstudio.navigationdrawer.LocationSelection cannot be cast to android.app.Activity
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.os.Looper.loop(Looper.java:135)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.main(ActivityThread.java:5254)
07-07 16:53:20.941: E/AndroidRuntime(1775): at java.lang.reflect.Method.invoke(Native Method)
07-07 16:53:20.941: E/AndroidRuntime(1775): at java.lang.reflect.Method.invoke(Method.java:372)
07-07 16:53:20.941: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-07 16:53:20.941: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-07 16:53:20.941: E/AndroidRuntime(1775): Caused by: java.lang.ClassCastException: com.pixalstudio.navigationdrawer.LocationSelection cannot be cast to android.app.Activity
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
07-07 16:53:20.941: E/AndroidRuntime(1775): ... 10 more
07-07 16:53:23.310: I/Process(1775): Sending signal. PID: 1775 SIG: 9
Menifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pixalstudio.navigationdrawer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LocationSelection"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LOCATIONSELECTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I have following Android Unity 3D Project file that compiles but fail in runtime with java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.nullpointerexception.
Here is the UnityPlayerNavtiveActivity.java
package com.sniper.game;
import com.unity3d.player.*;
import android.app.NativeActivity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class UnityPlayerNativeActivity extends NativeActivity
{
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
#Override protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
// Quit Unity
#Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// Pause Unity
#Override protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
// Resume Unity
#Override protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
// This ensures the layout will be correct.
#Override public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
#Override public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
#Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
#Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
#Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
#Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
}
Here is UnityPlayerActivity.java:
package com.sniper.game;
import com.unity3d.player.*;
/**
* #deprecated Use UnityPlayerNativeActivity instead.
*/
public class UnityPlayerActivity extends UnityPlayerNativeActivity { }
Here is UnityPlayerProxyActivity.java:
package com.sniper.game;
import com.unity3d.player.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
/**
* #deprecated Use UnityPlayerNativeActivity instead.
*/
public class UnityPlayerProxyActivity extends Activity
{
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, com.sniper.game.UnityPlayerNativeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Bundle extras = getIntent().getExtras();
if (extras != null)
intent.putExtras(extras);
startActivity(intent);
}
}
Here is the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="5" android:versionName="2.0" package="com.sniper.game" android:installLocation="preferExternal">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" />
<!-- Google Mobile Ads Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:icon="#drawable/app_icon" android:label="#string/app_name" android:debuggable="false">
<!-- meta-data tag for Google Play services -->
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<activity android:label="#string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.sniper.game.UnityPlayerProxyActivity">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="#string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.sniper.game.UnityPlayerActivity">
</activity>
<activity android:label="#string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.sniper.game.UnityPlayerNativeActivity">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<activity android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="#string/app_name" android:name="com.unity3d.player.VideoPlayer">
</activity>
<!-- Google Mobile Ads Activity -->
<activity android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="#string/app_name" android:name="com.google.android.gms.ads.AdActivity">
</activity>
</application>
<uses-feature android:glEsVersion="0x00020000" />
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
Here is the complete log trace ran few hours later from the screenshot above:
05-15 14:14:03.840: D/AndroidRuntime(1583): Shutting down VM
05-15 14:14:03.840: W/dalvikvm(1583): threadid=1: thread exiting with uncaught exception (group=0xb1ac2b90)
05-15 14:14:03.850: E/AndroidRuntime(1583): FATAL EXCEPTION: main
05-15 14:14:03.850: E/AndroidRuntime(1583): Process: com.sniper.game, PID: 1583
05-15 14:14:03.850: E/AndroidRuntime(1583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sniper.game/com.sniper.game.UnityPlayerNativeActivity}: java.lang.NullPointerException
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Looper.loop(Looper.java:137)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-15 14:14:03.850: E/AndroidRuntime(1583): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 14:14:03.850: E/AndroidRuntime(1583): at java.lang.reflect.Method.invoke(Method.java:515)
05-15 14:14:03.850: E/AndroidRuntime(1583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-15 14:14:03.850: E/AndroidRuntime(1583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-15 14:14:03.850: E/AndroidRuntime(1583): at dalvik.system.NativeStart.main(Native Method)
05-15 14:14:03.850: E/AndroidRuntime(1583): Caused by: java.lang.NullPointerException
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Parcel.readException(Parcel.java:1467)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.Parcel.readException(Parcel.java:1415)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.os.storage.IMountService$Stub$Proxy.mkdirs(IMountService.java:750)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ContextImpl.ensureDirsExistOrFilter(ContextImpl.java:2160)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ContextImpl.getObbDirs(ContextImpl.java:874)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ContextImpl.getObbDir(ContextImpl.java:863)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.content.ContextWrapper.getObbDir(ContextWrapper.java:220)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.NativeActivity.onCreate(NativeActivity.java:177)
05-15 14:14:03.850: E/AndroidRuntime(1583): at com.sniper.game.UnityPlayerNativeActivity.onCreate(UnityPlayerNativeActivity.java:22)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.Activity.performCreate(Activity.java:5243)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 14:14:03.850: E/AndroidRuntime(1583): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-15 14:14:03.850: E/AndroidRuntime(1583): ... 11 more
I need help in getting this Unity3D Android game running on simulator and then download and run on actual device.
I'm trying to develop a simple 2D game using libgdx in Android Studio (0.8.14), but at this point (just with a splash and an empty menu) I'm getting an error, with this LogCat output, when I launch the app (I'm testing on device, Sony Xperia Z1):
12-02 18:01:52.146 24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ Late-enabling CheckJNI
12-02 18:01:52.196 24248-24248/com.ak.thesoccerball.android W/ActivityThread﹕ Application com.ak.thesoccerball.android can be debugged on port 8100...
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.ak.thesoccerball.android-1/libgdx.so 0x447c06f0
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ Added shared lib /data/app-lib/com.ak.thesoccerball.android-1/libgdx.so 0x447c06f0
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.ak.thesoccerball.android-1/libgdx.so 0x447c06f0, skipping init
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android W/dalvikvm﹕ Exception Ljava/lang/NullPointerException; thrown while initializing Lcom/ak/thesoccerball/AKGame;
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android D/AndroidRuntime﹕ Shutting down VM
12-02 18:01:52.246 24248-24248/com.ak.thesoccerball.android W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41618d88)
12-02 18:01:52.256 24248-24248/com.ak.thesoccerball.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ak.thesoccerball.android, PID: 24248
java.lang.ExceptionInInitializerError
at com.ak.thesoccerball.android.AndroidLauncher.onCreate(AndroidLauncher.java:17)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
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:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ak.thesoccerball.AKGame.<clinit>(AKGame.java:9)
at com.ak.thesoccerball.android.AndroidLauncher.onCreate(AndroidLauncher.java:17)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
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:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
The classes involved are as follows:
- AndroidLauncher.java
package com.ak.thesoccerball.android;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.ak.thesoccerball.AKGame;
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useAccelerometer = false;
config.useCompass = false;
initialize(new AKGame(), config);
}
}
AKGame.java
package com.ak.thesoccerball;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class AKGame extends Game {
public static final int WIDTH = Gdx.graphics.getWidth();
public static final int HEIGHT = Gdx.graphics.getHeight();
public SpriteBatch batch;
#Override
public void create() {
batch = new SpriteBatch();
setScreen(new SplashScreen(this));
}
public void render() {
super.render(); //important!
}
public void dispose() {
batch.dispose();
}
}
And here's the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ak.thesoccerball.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="com.ak.thesoccerball.android.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
What am I missing so hard?
Move this code:
public static final int WIDTH = Gdx.graphics.getWidth();
public static final int HEIGHT = Gdx.graphics.getHeight();
into the create() method, for example:
#Override
public void create() {
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
//..
}
The thing is that before the create method gets called the Gdx is still null and cannot be used yet.
I'm a self learner for android app developer, then this problem came up and I'm not sure if people have this problem before but it says my app has stopped in the emulator.
So these are the codes I have :
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imageButton13 = (Button) findViewById(R.id.imageButton13);
imageButton13.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent = new Intent(v.getContext(), EXOActivity.class);
v.getContext().startActivity(Intent);
}
});
}
}
EXOActivity.java:
package com.mhyuktown.exowallpaper;
import android.app.Activity; import android.os.Bundle;
public class EXOActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exo);
}
}
Lastly here is my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mhyuktown.exowallpaper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EXOActivity" >
</activity>
</application>
</manifest>
logcat:
09-09 00:40:02.006: D/dalvikvm(1564): GC_FOR_ALLOC freed 49K, 4% free 3304K/3436K, paused 79ms, total 86ms
09-09 00:40:02.256: D/dalvikvm(1564): GC_FOR_ALLOC freed 7K, 3% free 3770K/3876K, paused 35ms, total 37ms
09-09 00:40:02.326: D/AndroidRuntime(1564): Shutting down VM
09-09 00:40:02.326: W/dalvikvm(1564): threadid=1: thread exiting with uncaught exception (group=0xb1aa7ba8)
09-09 00:40:02.376: E/AndroidRuntime(1564): FATAL EXCEPTION: main
09-09 00:40:02.376: E/AndroidRuntime(1564): Process: com.mhyuktown.exowallpaper, PID: 1564
09-09 00:40:02.376: E/AndroidRuntime(1564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mhyuktown.exowallpaper/com.mhyuktown.exowallpaper.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.os.Handler.dispatchMessage(Handler.java:102)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.os.Looper.loop(Looper.java:136)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-09 00:40:02.376: E/AndroidRuntime(1564): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 00:40:02.376: E/AndroidRuntime(1564): at java.lang.reflect.Method.invoke(Method.java:515)
09-09 00:40:02.376: E/AndroidRuntime(1564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-09 00:40:02.376: E/AndroidRuntime(1564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-09 00:40:02.376: E/AndroidRuntime(1564): at dalvik.system.NativeStart.main(Native Method)
09-09 00:40:02.376: E/AndroidRuntime(1564): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
09-09 00:40:02.376: E/AndroidRuntime(1564): at com.mhyuktown.exowallpaper.MainActivity.onCreate(MainActivity.java:18)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.Activity.performCreate(Activity.java:5231)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-09 00:40:02.376: E/AndroidRuntime(1564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-09 00:40:02.376: E/AndroidRuntime(1564): ... 11 more
Change it to this:
Intent intent = new Intent(MainActivity.this, EXOActivity.class);
startActivity(intent);
And
Button imageButton13 = (Button) findViewById(R.id.imageButton13);
to
ImageButton imageButton13 = (ImageButton) findViewById(R.id.imageButton13);
I coded it & it works fine. Import every thing needed.See the below code. Try pressing Ctrl+Shift+O, Eclipse will automatically add the import statement if missing.
Use ImageButton instead Button.
ImageButton button = (ImageButton)findViewById(R.id.imageButton13);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, EXOActivity.class);
startActivity(intent);
}
});
change
Intent Intent = new Intent(v.getContext(), EXOActivity.class);
v.getContext().startActivity(Intent);
To
Intent intent = new Intent(MainActivity.this, EXOActivity.class);
startActivity(intent);
Every activity you create must be included in the AndroidManifest.xml.
Find this:
</activity>
and after that just add this:
<activity android:name=".YourActivity"></activity>
when try to click on New Activity Button in my MainActivity , my app crash and closed . whit this error on screen " unfortunately[ App Name ] has stopped "
I google this problem and i can't find any Definitive solution.
here is logCat Log , First Java Source ( MainActivity.java ) and seccond java source ( Seccond.java ) .
Logcat Log :
05-03 00:05:17.750: E/AndroidRuntime(1091): FATAL EXCEPTION: main
05-03 00:05:17.750: E/AndroidRuntime(1091): Process: com.example.helloworld, PID: 1091
05-03 00:05:17.750: E/AndroidRuntime(1091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.Seccond}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.os.Looper.loop(Looper.java:136)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 00:05:17.750: E/AndroidRuntime(1091): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091): at java.lang.reflect.Method.invoke(Method.java:515)
05-03 00:05:17.750: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 00:05:17.750: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 00:05:17.750: E/AndroidRuntime(1091): at dalvik.system.NativeStart.main(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.Activity.performStart(Activity.java:5241)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
MainActivity.java
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start
Button btn_img = (Button) findViewById(R.id.btn_image);
Button btn = (Button) findViewById(R.id.btn_Click);
final TextView txt = (TextView) findViewById(R.id.txt_view);
Button btn_avtivity = ( Button) findViewById(R.id.btn_activity);
// For button click
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
txt.setText(" Amin Bassam ");
}
});
// Image show
btn_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
ImageView img=(ImageView)findViewById(R.id.img_view);
img.setImageResource(R.drawable.amin);
}
});
btn_avtivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),Seccond.class);
startActivity(intent);
}
});
// Open Activities
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new Fragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
}
Seccond.java
package com.example.helloworld;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class Seccond extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seccond);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.seccond, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_seccond,
container, false);
return rootView;
}
}
}
Activity_main.Xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.helloworld.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/txt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_Click"
android:layout_alignParentTop="true"
android:layout_marginTop="98dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btn_Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="66dp"
android:text="Click"
tools:ignore="HardcodedText" />
"res/layout/activity_main.xml"
<Button
android:id="#+id/btn_image"
style="#style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_Click"
android:layout_alignBottom="#+id/btn_Click"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/btn_Click"
android:text="Image"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_image"
android:layout_centerVertical="true"
android:scaleType="center"
android:src="#drawable/abc_ab_solid_light_holo"
tools:ignore="ContentDescription" />
<Button
android:id="#+id/btn_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_image"
android:text="New Activities"
tools:ignore="HardcodedText" />
</RelativeLayout>
Seccond_activity.xml
<RelativeLayout 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="#A8B007"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.helloworld.Seccond$PlaceholderFragment" >
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.helloworld.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.helloworld.Seccond"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment
The error is saying it cannot find the view with an id of container for the fragment placeholder to go into.
setContentView(R.layout.activity_seccond);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
This part of the code is the main problem. In the add() for the getSupportFragmentManager, it is saying to create the fragment and put it in a view with the id container. But the layout specified in setContentView does not have a view with an id of container. So there needs to be a layout file with a View (like a frame) with an id of container, so that when the fragment layout loads, it can go into that view.
Try adding a file to layouts called activity_frame_seccond.xml with:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Which is a frame with the id 'container'.
Change this line in Seccond.java:
setContentView(R.layout.activity_seccond);
for
setContentView(R.layout.activity_frame_seccond);
and see if that works.
With this code you are using the new file activity_frame_seccond.xml as the layout for the activity, and the first file activity_seccond.xml as the fragment that will put in the frame element (because it has the id container).
If this doesn't work let me know and I will have another look.
Check this line in your Fragment class :
View rootView = inflater.inflate(R.layout.activity_seccond,
container, false);
i think the Returned View is Null
Remove this line from your manifest
<activity
android:name="com.example.helloworld.Second"
android:label="#string/app_name" >
>
</activity>
and try
try in this way
#Override
public void onClick(View arg0) {
Intent intent = new Intent(this,Seccond.class);
startActivity(intent);
}