Why DrawerLayout and ActionBar not working on Tablets? - java

I'm develop some app with sliding menu.
when i try to run this on my galaxy 4s(api 18+ 4.3.3) its work fine!
but when i run this on xperia arc (api 8 ver 2.3.4) the app crash.
i know its dependent on Support library and i use :
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
and i need to use :
import android.support.v7.app.ActionBar;
so i added to my project the support library and connect the support library to him.
but i dont know how to handle the support and what i need to chage on my code:
my java code (v4){
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
public class MainActivity extends Activity implements OnClickListener, OnLongClickListener , OnTouchListener , OnItemClickListener{
/////////////////
ArrayList<ItemInSetting > listSetting = new ArrayList<ItemInSetting>();
CustomAdapterSetting adapterSetting;
DrawerLayout drawer;
ActionBarDrawerToggle toggle;
//////////////////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingItems();
}
public void settingItems(){
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawer = (DrawerLayout) findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this, drawer, R.drawable.ic_drawer, 0, 0){
#Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("menu");
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle("app title");
invalidateOptionsMenu();
}
};drawer.setDrawerListener(toggle);
listSetting.add(new ItemInSetting("ist1", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist2", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist3", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist4", R.drawable.calende_bigg));
lvSetting = (ListView) findViewById(R.id.listSetting);
lvSetting.setOnItemClickListener(this);
adapterSetting = new CustomAdapterSetting(listSetting, this);
lvSetting.setAdapter(adapterSetting);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
date = new Date();
switch (position) {
case 0:
Intent intent = new Intent (MainActivity.this,ListOfHours.class);
startActivity(intent);
break;
case 1:
getSetting();
if(sickIsOn){
cancelingFreeday(false);
}else{
addingManualMenu();
}
break;
case 2:
changingSetting(" ");
break;
case 3:
sendContact();
break;
default:
break;
}
drawer.closeDrawers();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (toggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
toggle.onConfigurationChanged(newConfig);
}
my 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="#drawable/new_back"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listSetting"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#android:color/darker_gray"
android:choiceMode="singleChoice"
android:listSelector="#drawable/list_selector" >
</ListView>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bib.workclock"
android:versionCode="52"
android:versionName="3.0.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/icon_new3"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity
android:name="com.bibas.workclock.MainActivity"
android:label="#string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ListOfHours" ></activity>
<activity android:name="HelpPage"></activity>
<activity android:name="info" android:screenOrientation="portrait"></activity>
</application>
</manifest>
this the locat error when i try to run on xperia :
05-04 01:59:05.058: E/AndroidRuntime(15484): FATAL EXCEPTION: main
05-04 01:59:05.058: E/AndroidRuntime(15484): java.lang.NoSuchMethodError: com.bba.workclock.MainActivity.getActionBar
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.bba.workclock.MainActivity.settingItems(MainActivity.java:1657)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.bba.workclock.MainActivity.onCreate(MainActivity.java:124)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.os.Looper.loop(Looper.java:130)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.main(ActivityThread.java:3701)
05-04 01:59:05.058: E/AndroidRuntime(15484): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 01:59:05.058: E/AndroidRuntime(15484): at java.lang.reflect.Method.invoke(Method.java:507)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
05-04 01:59:05.058: E/AndroidRuntime(15484): at dalvik.system.NativeStart.main(Native Method)
here is project like mine:
http://speedy.sh/UKsrV/SlidingMenu.zip
if any one can fix that code and give it to me back i appreciate alot
How can i fix this?!!
please help me :(((
I FIX THAT PROBLEM BUT KNOW ON MY TABLET IT'S CRASH .
here the locat error:
05-11 23:10:22.127: E/AndroidRuntime(31723): FATAL EXCEPTION: main
05-11 23:10:22.127: E/AndroidRuntime(31723): java.lang.IllegalStateException: Child android.widget.FrameLayout#41f91e40 at index 0 does not have a valid layout_gravity - must be Gravity.LEFT, Gravity.RIGHT or Gravity.NO_GRAVITY
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:721)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer.doFrame(Choreographer.java:525)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Handler.handleCallback(Handler.java:615)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Looper.loop(Looper.java:137)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.app.ActivityThread.main(ActivityThread.java:4895)
05-11 23:10:22.127: E/AndroidRuntime(31723): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 23:10:22.127: E/AndroidRuntime(31723): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-11 23:10:22.127: E/AndroidRuntime(31723): at dalvik.system.NativeStart.main(Native Method)

ActionBar is available from api level 11. To support below api level 11 you need to use AppCompat from the support library. Reference AppCompat in your android project.
You need to extend ActionBarActivity. Use theme derived from Theme.AppCompat in manifest and use getSupportActionBar().
Complete example #
Navigation Drawer with backword compatibility android
Check Adding libraries with resources
https://developer.android.com/tools/support-library/setup.html
And read
http://developer.android.com/guide/topics/ui/actionbar.html

Related

Error in Fragment Activity in Android

I am trying a fragment app as per a tutorial and getting following error:
06-28 04:21:47.118: D/AndroidRuntime(1491): Shutting down VM
06-28 04:21:47.128: W/dalvikvm(1491): threadid=1: thread exiting with uncaught exception (group=0xb4af7b90)
06-28 04:21:47.158: E/AndroidRuntime(1491): FATAL EXCEPTION: main
06-28 04:21:47.158: E/AndroidRuntime(1491): Process: com.example.fragmentapp, PID: 1491
06-28 04:21:47.158: E/AndroidRuntime(1491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentapp/com.example.fragmentapp.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Looper.loop(Looper.java:137)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.NativeStart.main(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.setContentView(Activity.java:1928)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.example.fragmentapp.MainActivity.onCreate(MainActivity.java:13)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.performCreate(Activity.java:5243)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 11 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.pavan.fragmentdemo.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:597)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:561)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.onCreateView(Activity.java:4777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 21 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pavan.fragmentdemo.MyListFragment" on path: DexPathList[[zip file "/data/app/com.example.fragmentapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragmentapp-2, /system/lib]]
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:583)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 24 more
The code is as follows:
Main Activity.java
package com.example.fragmentapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements
ListFragment.Communicator {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void Message(String OS_Name) {
DetailFragment detailfragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail_Fragment);
if (detailfragment != null && detailfragment.isInLayout()) {
detailfragment.setText(OS_Name);
}
else {
Intent intent = new Intent(getApplicationContext(),
DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(DetailActivity.os_name, OS_Name);
intent.putExtras(extras);
startActivity(intent);
}
}
}
Code of DetailActivity.java
package com.example.fragmentapp;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class DetailActivity extends Activity {
public static String os_name = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.detail_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString(os_name);
DetailFragment detailFragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
detailFragment.setText(name);
}
}
}
Code of DetailFragment.java
package com.example.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
return view;
}
// we call this method when button from listfragment is clicked
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.display_tv);
view.setText(item);
}
}
Code of ListFragment.java
package com.example.fragmentapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class ListFragment extends Fragment implements OnClickListener {
private Communicator communicator;
Button android_btn, ios_btn, windows_btn;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof Communicator) {
communicator = (Communicator) activity;
}
else {
throw new
ClassCastException(activity.toString()
+ " must implemenet MyListFragment.Communicator");
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view =
inflater.inflate(R.layout.list_fragment, container, false);
// Initialize Views android_btn = (Button)
view.findViewById(R.id.android_btn_id);
ios_btn = (Button)
view.findViewById(R.id.ios_btn_id);
windows_btn = (Button)
view.findViewById(R.id.windows_btn_id);
// set on click Listeners for buttons
android_btn.setOnClickListener(this);
ios_btn.setOnClickListener(this);
windows_btn.setOnClickListener(this);
return view;
}
//Create Interface
public interface Communicator {
public void Message(String OS_Name);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.android_btn_id:
updateFragment("Android");
break;
case R.id.ios_btn_id:
updateFragment("IOS");
break;
case R.id.windows_btn_id:
updateFragment("Windows");
break;
}
}
private void updateFragment(String OS_Name) {
communicator.Message(OS_Name);
}
}
In Layout Folder
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
<fragment
android:id="#+id/detail_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="2"
class="com.pavan.fragmentdemo.DetailFragment" >
</fragment>
</LinearLayout>
Code of detail_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF99"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="#+id/display_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="40sp" />
</LinearLayout>
code of list_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFF99"
android:orientation="vertical"
android:padding="5dp" >
<Button
android:id="#+id/android_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<Button
android:id="#+id/ios_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IOS" />
<Button
android:id="#+id/windows_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Windows" />
</LinearLayout>
In layout-port
code activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
</LinearLayout>
Code of detail_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.pavan.fragmentdemo.DetailFragment" />
</LinearLayout>
You have different names of fragments:
in xml com.pavan.fragmentdemo.MyListFragment
in java com.example.fragmentapp.MyListFragment
fix package names

Android Application crahes when passing strings between classes

I'm trying to pass a string between 2 classes in Android . The first class is "MainActivity" and the second one is "abc" .
Here's my MainActivity code :
package com.example.passstrings;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
String pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass = "abcabc";
Intent abc = new Intent(this , abc.class);
abc.putExtra("key", pass);
startActivity(abc);
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.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.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
and here's my abc class code :
package com.example.passstrings;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}
}
Here's my fragment_main.xml code :
<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="com.example.passstrings.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginLeft="69dp"
android:layout_marginTop="72dp"
android:layout_toRightOf="#+id/textView2"
android:text="TextView" />
</RelativeLayout>
and here's my LogCat :
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x008b
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x0099
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.675: D/AndroidRuntime(734): Shutting down VM
04-04 05:34:38.675: W/dalvikvm(734): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:34:38.685: E/AndroidRuntime(734): FATAL EXCEPTION: main
04-04 05:34:38.685: E/AndroidRuntime(734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Looper.loop(Looper.java:123)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:34:38.685: E/AndroidRuntime(734): at dalvik.system.NativeStart.main(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivity(Activity.java:2923)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.example.passstrings.MainActivity.onCreate(MainActivity.java:25)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:34:38.685: E/AndroidRuntime(734): ... 11 more
and here's my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
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.passstrings.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>
</application>
</manifest>
If I remove "startActivity(abc);" the application doesn't crash, also if use try&catch the application doesn't crash but the same time it doesn't perform the action . So what could be the reason ? and thanks in advance
My LogCat after adding the activity to manifest :
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.555: W/dalvikvm(883): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.555: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x008b
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.565: W/dalvikvm(883): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.565: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x0099
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.775: D/AndroidRuntime(883): Shutting down VM
04-04 05:47:16.775: W/dalvikvm(883): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:47:16.786: E/AndroidRuntime(883): FATAL EXCEPTION: main
04-04 05:47:16.786: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.abc}: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:123)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:47:16.786: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): Caused by: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at com.example.passstrings.abc.onCreate(abc.java:20)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:47:16.786: E/AndroidRuntime(883): ... 11 more
and my Manifest after the update :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
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.passstrings.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.passstrings.abc" />
</application>
</manifest>
You need to add your second Activity to your Androidmanifest.xml file.
<activity android:name="com.example.passstrings.abc" />
have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
it clearly say's that you have not declared your abc activity in manifest file. have you declared this activity in your AndroidManifest.xml?
so just decalre your abc activity in manifest file.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.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=".abc"
</activity>
</application>
add this line
<activity
android:name=".abc"
</activity>
and also change this in your abc.java
setContentView(R.layout.activity_main);
you have reference of activity_main only you have to set abc.XML or whatever your XML filename for abc. So change it with
setContentView(R.layout.abc);
Edit
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}
It is simply ActivityNotFoundException which mean you forgot to declare abc activity in your AndroidManifest.xml file.
Just add following line in your manifest.xml in application tag.
<activity android:name="com.example.passstrings.abc" />
For your new NullPointerException here is the solution
You have defined following .xml in your Activity file activity_main while you have declare TextViews in fragment_main.xml, that's why it is giving NullPointerException.
I suggest you to change the xml from following line
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main.xml);

Using simple fragments in android

I am trying to use Simple Fragments
I am getting the log errors in the screen
Any ideas to resolve them
LM_Fragement.java
public class LM_Fragement extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
return inflater.inflate(
R.layout.lm_fragement, container, false);
}
}
PM_Fragement.java
public class PM_Fragement extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
return inflater.inflate(
R.layout.pm_fragment, container, false);
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
/**
* Check the device orientation and act accordingly
*/
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
/**
* Landscape mode of the device
*/
LM_Fragement ls_fragment = new LM_Fragement();
fragmentTransaction.replace(android.R.id.content, ls_fragment);
}else{
/**
* Portrait mode of the device
*/
PM_Fragement pm_fragment = new PM_Fragement();
fragmentTransaction.replace(android.R.id.content, pm_fragment);
}
fragmentTransaction.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="com.example.fragments"
android:id="#+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragments"
android:id="#+id/pm_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Log::
10-13 19:36:11.917: I/dalvikvm(652): Could not find method com.example.fragmentstutorialpoint.MainActivity.getFragmentManager, referenced from method com.example.fragmentstutorialpoint.MainActivity.onCreate
10-13 19:36:11.966: E/AndroidRuntime(652): FATAL EXCEPTION: main
10-13 19:36:11.966: E/AndroidRuntime(652): java.lang.NoSuchMethodError: com.example.fragmentstutorialpoint.MainActivity.getFragmentManager
10-13 19:36:11.966: E/AndroidRuntime(652): at com.example.fragmentstutorialpoint.MainActivity.onCreate(MainActivity.java:22)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.os.Looper.loop(Looper.java:123)
10-13 19:36:11.966: E/AndroidRuntime(652): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 19:36:11.966: E/AndroidRuntime(652): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 19:36:11.966: E/AndroidRuntime(652): at java.lang.reflect.Method.invoke(Method.java:507)
10-13 19:36:11.966: E/AndroidRuntime(652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 19:36:11.966: E/AndroidRuntime(652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 19:36:11.966: E/AndroidRuntime(652): at dalvik.system.NativeStart.main(Native Method)
How can i resolve these errors ....
Any Ideas
Thanks
Are you using the supportlibrary in a pre 3.0 project?
If so use getSupportFragmentManager instead

The application has stopped unexpectedly . Error-caused by: Java.lang.NullPointerException

main.java
package com.learnactivities;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
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: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=".Main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
</RelativeLayout>
Second.java
package com.learnactivities;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
second.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=".Second" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="#string/nd" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnactivities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.learnactivities.Main"
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=".second" />
</application>
</manifest>
I m getting following errors. I can see that error caused by java.lang.NullPointerException. I did my best searching here and there but could not get to the solution.Plz, anyone help me sort this problem out?
08-25 19:08:44.442: D/AndroidRuntime(276): Shutting down VM
08-25 19:08:44.442: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-25 19:08:44.472: E/AndroidRuntime(276): FATAL EXCEPTION: main
08-25 19:08:44.472: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learnactivities/com.learnactivities.Main}: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-25 19:08:44.472: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at com.learnactivities.Main.onCreate(Main.java:19)
08-25 19:08:44.472: E/AndroidRuntime(276): at `android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)`
`08-25 19:08:44.472: E/AndroidRuntime(276): at` `android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)`
08-25 19:08:44.472: E/AndroidRuntime(276): ... 11 more
R.id.button1 is declared inside second.xml, but you are looking for it inside activity.xml. So in Main, when you do
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
findViewById is returning null
Try this in your main class..
TextView b = (TextView ) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
and also in manifest change like below line
<activity android:name=".Second" />

startActivity() forces closes my application

I cannot start a new activity no matter what I do.
I re-downloaded all Android SDKs again.
I re-downloaded Eclipse and installed ADT plugin again
Error Log
09-16 17:53:51.537: W/IInputConnectionWrapper(4661): getCursorCapsMode on inactive InputConnection
09-16 17:53:51.998: D/AndroidRuntime(4661): Shutting down VM
09-16 17:53:51.998: W/dalvikvm(4661): threadid=1: thread exiting with uncaught exception (group=0x41987300)
09-16 17:53:52.022: E/AndroidRuntime(4661): FATAL EXCEPTION: main
09-16 17:53:52.022: E/AndroidRuntime(4661): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lays.decisong/com.lays.decisong.activities.GameActivity}: java.lang.NullPointerException
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.os.Looper.loop(Looper.java:137)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.reflect.Method.invoke(Method.java:511)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-16 17:53:52.022: E/AndroidRuntime(4661): at dalvik.system.NativeStart.main(Native Method)
09-16 17:53:52.022: E/AndroidRuntime(4661): Caused by: java.lang.NullPointerException
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.AlertDialog.<init>(AlertDialog.java:98)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ProgressDialog.show(ProgressDialog.java:110)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ProgressDialog.show(ProgressDialog.java:99)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity$3.<init>(GameActivity.java:401)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity.<init>(GameActivity.java:398)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.Class.newInstanceImpl(Native Method)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.Class.newInstance(Class.java:1319)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
Public key in DecisongApplication.java
public static final String PLAYERS_KEY = "com.lays.decisong.activities.Players";
Button Handler in InputActivity.java
App forces closes when startActivity() is called
public void startGame(View v) {
// check if there's more than one player
if (mPlayers.size() < 2) {
Log.i(TAG, "Only 1 player");
Toast.makeText(mContext, "At least 2 players needed to start game",
Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(this.getApplicationContext(), GameActivity.class);
intent.putStringArrayListExtra(DecisongApplication.PLAYERS_KEY, mPlayers);
startActivity(intent);
overridePendingTransition(R.anim.slide_up_incoming, R.anim.slide_up_outgoing);
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lays.decisong"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<supports-screens
android:anyDensity="true"
android:largeScreens="false"
android:normalScreens="true"
android:smallScreens="false"
android:xlargeScreens="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="com.lays.decisong.DecisongApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
<activity android:name=".activities.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.InstructionsActivity"
android:noHistory="true" />
<activity
android:name=".activities.InputActivity"
android:noHistory="true" />
<activity
android:name=".activities.SettingsActivity"
android:noHistory="true" />
<activity
android:name=".activities.GameActivity"
android:noHistory="true" />
</application>
</manifest>
onCreate of GameActivity.java in com.lays.decisong.activities which never got called
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
// init rdio variables
mTrackQueue = new LinkedList<Track>();
if (mRdio == null) {
mRdio = new Rdio(DecisongApplication.RDIO_API_KEY,
DecisongApplication.RDIO_SECRET_KEY, null, null, this, this);
}
// init quiz variables
mListView = getListView();
mAllAlbums = new HashMap<String, Album>();
mChosenTracks = new ArrayList<Track>();
mAdapter = new TracksAdapter(this, mChosenTracks);
setListAdapter(mAdapter);
// init player variables
mCurrentRoundView = (TextView) findViewById(R.id.current_round);
mCurrentRound = INITIAL_ROUND;
mCurrentPlayerView = (TextView) findViewById(R.id.current_player);
mCurrentPlayer = INITIAL_PLAYER;
mPlayers = new ArrayList<Player>();
if (getIntent().hasExtra(DecisongApplication.PLAYERS_KEY)) {
ArrayList<String> players = getIntent().getStringArrayListExtra(DecisongApplication.PLAYERS_KEY);
for (String p : players) {
mPlayers.add(Player.create(p));
}
}
// setup game conditions
Collections.shuffle(mPlayers);
mCurrentPlayerView.setText(mPlayers.get(mCurrentPlayer).name);
}
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity$3.<init>(GameActivity.java:401)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity.<init>(GameActivity.java:398)
Looks like you're trying to do some setup in your GameActivity's constructor, or initialise some fields before the activity's Context is fully initialised. You should really do all setup in onCreate(); don't use a constructor, and don't rely on the Context being usable until onCreate() is entered.

Categories