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
Related
My app get stops while including particular java class in manifest.xml and the logcat shows error in fragment.If i remove those classes from manifest.xml the app is running successfully.On the other side remaining java classes working properly and there is no error in the fragment.I don't know why the app gets stopped for the particular java files
This is the error shown by logcat while including that particular java class
10-05 10:46:31.617 12820-12820/com.sentientit.theiWedplanner E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sentientit.theiWedplanner/com.sentientit.theiWedplanner.Frontpage}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:1930)
at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.sentientit.theiWedplanner.Fragadmob: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:592)
at android.app.Fragment.instantiate(Fragment.java:560)
at android.app.Activity.onCreateView(Activity.java:4864)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:1930)
at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sentientit.theiWedplanner.Fragadmob" on path: /system/framework/com.google.android.maps.jar:/data/app/com.sentientit.theiWedplanner-1.apk
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.Fragment.instantiate(Fragment.java:560)
at android.app.Activity.onCreateView(Activity.java:4864)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:1930)
at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
10-05 10:46:40.273 12820-12824/com.sentientit.theiWedplanner D/dalvikvm﹕ GC_CONCURRENT freed 413K, 28% free 6025K/8348K, paused 4ms+2ms, total 36ms
front.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<fragment
android:id="#+id/adview153613"
android:name="com.sentientit.theiWedplanner.Fragadmob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/fragadmob" />
<RelativeLayout
android:id="#+id/fron"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="50px"
android:background="#003399"
android:gravity="center"
android:text="iWedPlanner"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif" />
<ImageView
android:id="#+id/cl"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:src="#drawable/ilogo" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="23dp"
android:gravity="center"
android:text="Welcome to iWedPlanner"
android:textColor="#FF9933"
android:textSize="20sp"
android:textStyle="italic"
android:typeface="serif" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cl"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="Your Guide to fun and flawless wedding planning"
android:textColor="#FFFF0000"
android:textStyle="italic"
android:typeface="serif" />
<ImageButton
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cl"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#FFFFFF"
android:src="#drawable/ib" />
</RelativeLayout>
</LinearLayout>
fragadmob.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adview153613"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/aagr_background2" >
<ImageView
android:id="#+id/image123"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:src="#drawable/adview1536132" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="XXXXXXXXXXXXXXXXXXXXXXXXXXX"
/>
<ImageView
android:id="#+id/addviewimage1"
android:layout_width="31dp"
android:layout_height="31dp"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:src="#drawable/close" />
</RelativeLayout>
frontpage.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import com.bugsense.trace.BugSenseHandler;
public class Frontpage extends FragmentActivity {
/** Called when the activity is first created. */
SharedPreferences sharedPreferences;
#Override
public void onStart() {
super.onStart();
// The rest of your onStart() code.
// // EasyTracker.getInstance(this).activityStart(this); // Add this method.
}
#Override
public void onStop() {
super.onStop();
// The rest of your onStop() code.
// EasyTracker.getInstance(this).activityStop(this); // Add this method.
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); BugSenseHandler.initAndStartSession(this, "68640bea");
//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.front);
RelativeLayout clic =(RelativeLayout)findViewById(R.id.fron);
clic.setOnClickListener(new OnClickListener() {
//#Override
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent=new Intent(Frontpage.this,welcomeuser1.class);
//Intent intent=new Intent(Frontpage.this,ZXingJarDemoActivity.class);
startActivity(intent);
finish();
}
});
}
}
fragadmob.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class Fragadmob extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragadmob, container, false);
//Admob
RelativeLayout gonad=(RelativeLayout)rootView.findViewById(R.id.adview153613);
ImageView addviewimage=(ImageView)rootView.findViewById(R.id.image123);
ImageView inap=(ImageView)rootView.findViewById(R.id.addviewimage1);
// AdView adView = new AdView(getActivity());
AdView adView = (AdView)rootView.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
if(addviewimage.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
addviewimage.getLayoutParams().height = 30;
// code to do for Portrait Mode
}
return rootView;
}
}
You never need to add fragments to your manifest. your logcat showing ClassNotFoundException: Didn't find class "com.sentientit.theiWedplanner.Fragadmob" but Fragadmob extends Fragment so u trying to adding fragment in manifast so make sure once and enjoy coding.
I am trying to create a simple application that can sign in / sign up users . All the data are
stored in Mysql database .
My app was installed successfully but as I opened it, it gave an error saying "Unfortunately, 'myapp' has stop working".
I have read almost all related problems posted here but no luck!
here's from logcat .
07-25 01:22:31.452: E/AndroidRuntime(2101): FATAL EXCEPTION: main
07-25 01:22:31.452: E/AndroidRuntime(2101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysqltest/com.example.mobileapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Looper.loop(Looper.java:137)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invoke(Method.java:525)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 01:22:31.452: E/AndroidRuntime(2101): at dalvik.system.NativeStart.main(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.setContentView(Activity.java:1895)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.example.mobileapp.MainActivity.onCreate(MainActivity.java:45)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.performCreate(Activity.java:5133)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 11 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.constructNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 23 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.OutOfMemoryError
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.Resources.loadDrawable(Resources.java:2105)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3364)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3293)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.ViewGroup.<init>(ViewGroup.java:453)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" >
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/password"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:text="Password" />
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp" />
<TextView
android:id="#+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="38dp"
android:gravity="center"
android:text="PhilTaxWindow User Login"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/username"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp"
android:text="Username" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/register"
android:layout_alignBottom="#+id/register"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/register"
android:text="Don't Have an Account?"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/register"
android:layout_alignParentLeft="true"
android:layout_marginBottom="60dp"
android:text="Login" />
<EditText
android:id="#+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tv2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:ems="10"
android:inputType="textPassword"
android:singleLine="true" />
<Button
android:id="#+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv"
android:layout_alignParentRight="true"
android:layout_marginBottom="17dp"
android:layout_marginRight="29dp"
android:text="Register here" />
</RelativeLayout>
mainactivity.java
package com.example.mobileapp;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.mysqltest.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText Username, Password;
private Button Submit, Register;
private ProgressDialog Dialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.4/checkingPHP/CheckUser.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Username = (EditText)findViewById(R.id.username);
Password = (EditText)findViewById(R.id.password);
Submit = (Button)findViewById(R.id.Button01);
Register = (Button)findViewById(R.id.register);
Submit.setOnClickListener(this);
Register.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
boolean failure = false;
#Override
protected void onPreExecute() {
super.onPreExecute();
Dialog = new ProgressDialog(MainActivity.this);
Dialog.setMessage("Validating...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
#Override
protected String doInBackground(String... args) {
int success;
String username = Username.getText().toString();
String password = Password.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", username));
params.add(new BasicNameValuePair("Password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(MainActivity.this, WelcomPage.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String file_url) {
Dialog.dismiss();
if (file_url != null){
Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
The emulator ran into out of memory exception
Caused by: java.lang.OutOfMemoryError due to
android:background="#drawable/background"
The image that you are trying to load is causing this. Try reducing the image resolution.
I've been doing some research of this problem but no solution has yet to work for me. I've added empty constructors in the fragment classes and tried with different imports of fragments, but nothing seems to work. I hope you guys can help me!
My Activity:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
}
public void onRssItemSelected(String link) {
DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
}//end if
}//end method onRssItemSelected
}//end class
My fragment:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment {
public MyListFragment() {
}
private OnItemSelectedListener listener;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview, container, false);
Button button = (Button) view.findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateDetail();
}//end onClick
});
return view;
}//end onCreateView
public interface OnItemSelectedListener {
public void onRssItemSelected(String link);
}//end onItemSelectedListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ "Must implement MyListFragment.OnItemSelectedListener");
}//end else
}//end onAttach
//May also be triggered from the activity
public void updateDetail() {
//Create fake data
String newTime = String.valueOf(System.currentTimeMillis());
// Send data to Activity
listener.onRssItemSelected(newTime);
}//end method updateDetail
}//end class
My fragment XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.example.android.rssfeed.MyListFragment"
android:layout_marginTop="?android:attr/actionBarSize"></fragment>
<fragment
android:id="#+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.android.rssfeed.DetailFragment"></fragment>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.example.android.rssfeed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
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.com.example.android.rssfeed.RssfeedActivity"
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>
Button 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" >
<button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update">
</button>
</LinearLayout>
LogCat error:
05-17 10:35:01.864: E/AndroidRuntime(6384): FATAL EXCEPTION: main
05-17 10:35:01.864: E/AndroidRuntime(6384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.example.android.rssfeed/com.example.com.example.android.rssfeed.RssfeedActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.access$700(ActivityThread.java:150)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Looper.loop(Looper.java:137)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.main(ActivityThread.java:5279)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.NativeStart.main(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.setContentView(Activity.java:1928)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.example.com.example.android.rssfeed.RssfeedActivity.onCreate(RssfeedActivity.java:11)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.performCreate(Activity.java:5267)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 11 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.rssfeed.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:592)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:560)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.onCreateView(Activity.java:4862)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 21 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.rssfeed.MyListFragment" on path: /data/app/com.example.com.example.android.rssfeed-1.apk
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:582)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 24 more
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.android.rssfeed.MyListFragment" on path:
/data/app/com.example.com.example.android.rssfeed-1.apk
Change this
class="com.example.android.rssfeed.MyListFragment"
to
class="com.example.com.example.android.rssfeed.MyListFragment"
cause the packagename is package com.example.com.example.android.rssfeed.
Similarly
class="com.example.com.example.android.rssfeed.DetailFragment"
if the package name is the same
Change this
<button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update">
To
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update" />
Change
"class= com.example.android.rssfeed.MyListFragment"
to
"com.example.com.example.android.rssfeed.MyListFragment"
And your problem will be solved out
Add this line in fragment
class="com.google.android.gms.maps.SupportMapFragment"
I created an app by following the tutorial at http://www.techotopia.com/index.php/Using_Fragments_in_Android_-_A_Worked_Example,
but I have a error.
LogCat:
05-02 08:16:22.044: D/dalvikvm(1846): Late-enabling CheckJNI
05-02 08:16:22.080: D/AndroidRuntime(1846): Shutting down VM
05-02 08:16:22.080: W/dalvikvm(1846): threadid=1: thread exiting with uncaught exception (group=0xa4d81b20)
05-02 08:16:22.096: E/AndroidRuntime(1846): FATAL EXCEPTION: main
05-02 08:16:22.096: E/AndroidRuntime(1846): Process: com.example.myfragmentexample, PID: 1846
05-02 08:16:22.096: E/AndroidRuntime(1846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfragmentexample/com.example.myfragmentexample.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.os.Looper.loop(Looper.java:136)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.reflect.Method.invoke(Method.java:515)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 08:16:22.096: E/AndroidRuntime(1846): at dalvik.system.NativeStart.main(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.setContentView(Activity.java:1929)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.example.myfragmentexample.MainActivity.onCreate(MainActivity.java:12)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.performCreate(Activity.java:5231)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 11 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragmentexample.ToolbarFragment: make sure class name exists, is public, and has an empty constructor that is public
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:597)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:561)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.onCreateView(Activity.java:4778)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 21 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.fragmentexample.ToolbarFragment" on path: DexPathList[[zip file "/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1, /system/lib]]
05-02 08:16:22.096: E/AndroidRuntime(1846): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:583)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 24 more
MainActivity.java:
package com.example.myfragmentexample;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements
ToolbarFragment.ToolbarListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtononClick(int fontsize, String text) {
TextFragment textfragment = (TextFragment) getSupportFragmentManager()
.findFragmentById(R.id.text_fragment);
textfragment.zmienWlasciwosci(fontsize, text);
}
}
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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/toolbar_fragment"
android:name="com.example.fragmentexample.ToolbarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="#layout/toolbar_fragment" />
<fragment
android:id="#+id/text_fragment"
android:name="com.example.fragmentexample.TextFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
tools:layout="#layout/text_fragment" />
</RelativeLayout>
ToolbarFragment.java:
package com.example.myfragmentexample;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class ToolbarFragment extends Fragment implements
OnSeekBarChangeListener {
private static int seekvalue = 10;
private static EditText edittext;
ToolbarListener activityCallback;
public interface ToolbarListener {
public void onButtononClick(int position, String text);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCallback = (ToolbarListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ToolbarListener");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.toolbar_fragment, container,
false);
edittext = (EditText) view.findViewById(R.id.editText);
SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
public void buttonClicked(View view) {
activityCallback.onButtononClick(seekvalue, edittext.getText()
.toString());
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
seekvalue = arg1;
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
toolbar_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText"
android:layout_marginTop="14dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:layout_below="#+id/seekBar"
android:text="#string/button_text" />
</RelativeLayout>
TextFragment.java:
package com.example.myfragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TextFragment extends Fragment {
private static TextView textview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.text_fragment, container, false);
textview = (TextView) view.findViewById(R.id.textView1);
return view;
}
public void zmienWlasciwosci (int fontsize, String text){
textview.setTextSize(fontsize);
textview.setText(text);
}
}
text_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/text_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfragmentexample"
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.myfragmentexample.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>
I read a lot of posts about this error, but I couldn't find anything wrong in my code. I extended android.support.v4.app.FragmentActivity.
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.fragmentexample.ToolbarFragment" on path:
DexPathList[[zip file
"/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1,
/system/lib]]
This
android:name="com.example.fragmentexample.ToolbarFragment"
Must be
android:name="com.example.myfragmentexample.ToolbarFragment"
cause package name for ToolbarFragment.java is
package com.example.myfragmentexample;
Similarly for TextFragment
android:name="com.example.myfragmentexample.TextFragment"
Try adding correct layouts at correct place like ex. in my case I was putting colors.xml in values-w820p then it was giving runtime exception of classtype. I changed the folder from this to simple value and... taddda!! it worked! :)
I am also new to Android and encountered the same error today. I could not find a solution that worked for me but one thing was clear to me that this issue occurs when android is not able to resolve a mandatory attribute of one of the elements in layout xml.
So I checked my layout thoroughly and found that by mistake I created an ID
<item type="id" name="login"/>
in ids.xml. This Id was conflicting with attribute
android:imeActionId="#+id/login"
of EditText element.
Hope this helps someone.
In my case Google Maps were not correctly initialized. A message in log console printed about it. I had to add
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
to <application> inside AndroidManifest.
I am new to Android.
I've just started to build my first Android app and have been struggling with this errorfor the past 2 days.
Finding no solution I had to come here.
Error:
Unfortunately "app name" has stopped
This is main activity layout:
<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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
This is another layout:
<?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">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
logcat error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.os.Looper.loop(Looper.java:137)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:01:29.772: E/AndroidRuntime(1314): at dalvik.system.NativeStart.main(Native Method)
03-25 12:01:29.772: E/AndroidRuntime(1314): Caused by: java.lang.NullPointerException
03-25 12:01:29.772: E/AndroidRuntime(1314): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:01:29.772: E/AndroidRuntime(1314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:01:29.772: E/AndroidRuntime(1314): ... 11 more
03-25 12:01:35.622: I/Process(1314): Sending signal. PID: 1314 SIG: 9
03-25 12:11:56.522: D/AndroidRuntime(1378): Shutting down VM
03-25 12:11:56.522: W/dalvikvm(1378): threadid=1: thread exiting with uncaught exception (group=0xb3adbb90)
03-25 12:11:56.542: E/AndroidRuntime(1378): FATAL EXCEPTION: main
03-25 12:11:56.542: E/AndroidRuntime(1378): Process: com.example.listview, PID: 1378
03-25 12:11:56.542: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.MainActivity}: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Handler.dispatchMessage(Handler.java:102)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.os.Looper.loop(Looper.java:137)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): at java.lang.reflect.Method.invoke(Method.java:515)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-25 12:11:56.542: E/AndroidRuntime(1378): at dalvik.system.NativeStart.main(Native Method)
03-25 12:11:56.542: E/AndroidRuntime(1378): Caused by: java.lang.NullPointerException
03-25 12:11:56.542: E/AndroidRuntime(1378): at com.example.listview.MainActivity.onCreate(MainActivity.java:36)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Activity.performCreate(Activity.java:5243)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 12:11:56.542: E/AndroidRuntime(1378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-25 12:11:56.542: E/AndroidRuntime(1378): ... 11 more
main activity code
package com.example.listview;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lst = (ListView)findViewById(R.id.listView1);
//ListView mainListView = null;
String[] test = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(test) );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );
// Set the ArrayAdapter as the ListView's adapter.
lst.setAdapter( listAdapter );
}
#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;
}
}
Your listview is not contained in the activity's layout so when you call it it gives you a null pointer exception. you could put the listview inside the main activity's layout.
<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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="TextView" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
Line 36 is:
lst.setAdapter( listAdapter );
Means that lst is probably null.
ListView lst = (ListView)findViewById(R.id.listView1); //NULL
Failed to find the view, because you load main_activity layout, which does not have a ListView with id=listView1.
Probably you need to load another layout, which it contains.
ListView lst = (ListView)findViewById(R.id.listView1); return null because you don't use the right content in setContentView(R.layout.activity_main);. You have two options:
Use a include to put a layout inside another:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<TextView
... />
<include android:layout="#layout\my_layout_listview" />
</RelativeLayout>
Or set the other layout which contains your ListView in:
setContentView(R.layout.my_layout_listview);