I want to change my project from Activity to FragmentActivity. The first step was to create the fragment:
public class Frag1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.detail_fragment, container, false);
return view;
}
}
the detail_fragment is a simple LinearLayout with a TextView
The second Step was to (after my SplashScreen) make the parent class to FragmentActivity:
public class TestFrag extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The R.layout.frag_main:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frag"
android:name=".fragments.Frag1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
On my SplashScreen is say know:
Intent myInt = new Intent(this, TestFrag.class);
startActivity(myInt);
But alwas i get these errors:
05-18 23:09:51.543: W/dalvikvm(28644): Unable to resolve superclass of Lws/stefma/projectname/TestFrag; (20)
05-18 23:09:51.543: W/dalvikvm(28644): Link of class 'Lws/stefma/projectname/TestFrag;' failed
05-18 23:09:51.543: E/dalvikvm(28644): Could not find class 'ws.stefma.projectname.TestFrag', referenced from method ws.stefma.projectname.Activity_Login_Registration.onCreate
05-18 23:09:51.543: W/dalvikvm(28644): VFY: unable to resolve const-class 94 (Lws/stefma/projectname/TestFrag;) in Lws/stefma/projectname/Activity_Login_Registration;
05-18 23:09:51.543: D/dalvikvm(28644): VFY: replacing opcode 0x1c at 0x000b
05-18 23:09:51.583: D/AndroidRuntime(28644): Shutting down VM
05-18 23:09:51.583: W/dalvikvm(28644): threadid=1: thread exiting with uncaught exception (group=0x41850930)
05-18 23:09:51.593: E/AndroidRuntime(28644): FATAL EXCEPTION: main
05-18 23:09:51.593: E/AndroidRuntime(28644): java.lang.NoClassDefFoundError: ws.stefma.projectname.TestFrag
05-18 23:09:51.593: E/AndroidRuntime(28644): at ws.stefma.projectname.Activity_Login_Registration.onCreate(Activity_Login_Registration.java:30)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.Activity.performCreate(Activity.java:5104)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.os.Looper.loop(Looper.java:137)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-18 23:09:51.593: E/AndroidRuntime(28644): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 23:09:51.593: E/AndroidRuntime(28644): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 23:09:51.593: E/AndroidRuntime(28644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-18 23:09:51.593: E/AndroidRuntime(28644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-18 23:09:51.593: E/AndroidRuntime(28644): at dalvik.system.NativeStart.main(Native Method)
I have no idea why...
The confused about that:
If I go back from FragmentActivity to Activity all runs fine without errors!!!
Also i have these in my AndroidManifest:
<activity android:name="ws.stefma.projectname.TestFrag" />
Update:
I have import the support-v4 libary.
My AndroidManifest was declared to minSdk="14" and targetSdk="17".
Because i used the support-v4 I have imported on the Fragment-Class
import android.app.support.v4.Fragment
Now i have changed that to
import android.app.Fragment
and it seems to work...
Also i must change the FragmentActivity to Activity. Because the FragmentActivity is only an Class for the support v4 Package!
Update 2 - Solution found:
Here is the solution
Change R.layout.frag_main to something like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/fragment1"
android:name="ws.stefma.projectname.Frag1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I think you don't need use support-v4 library. Please tell me why do you use that library? I see min sdk is 14, in this case using of sopport-v4 is useless.
Please checkout ws.stefma.projectname package, because logcat says that ws.stefma.projectname.TestFrag doesn't exist.
Okay guys, here is the Solution.
I donn't know exactly what the folling stands for and what it makes, but it runs and i have now errors after these workaround:
Close eclipse -> remove support-V4 -> open eclipse -> right click on your project -> AndroidTools -> Add Support Libary -> right click on your project -> Properties -> Choose java build path -> Choose 'Order and Export' -> Select All -> Click 'ok' -> Project -> Clean -> Run and stay happy!
Related
I try to get the phone number of my device, get the following error:
FATAL EXCEPTION:
main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.myapplication/com.example.dell.myapplication.MainActivity}: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
I am a new at android development. I have looked at the errors that have been generated by LogCat but do not know the problem and where to find it based on the errors I received.
Thanks for your help and sorry for the duration!
here is my code:
MainActivity.java
package com.example.dell.myapplication;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends ActionBarActivity {
public MainActivity() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
String phone = tm.getLine1Number();
Toast.makeText(MainActivity.this, "my IMEI: "+imei, Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,"my phone: "+phone,Toast.LENGTH_LONG).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="121dp" />
</RelativeLayout>
LogCat
12-23 12:50:35.013 8789-8789/? D/dalvikvm﹕ Late-enabling CheckJNI
12-23 12:50:35.303 8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
12-23 12:50:35.303 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 13332: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
12-23 12:50:35.303 8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
12-23 12:50:35.443 8789-8789/com.example.dell.myapplication D/AndroidRuntime﹕ Shutting down VM
12-23 12:50:35.443 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416f02a0)
12-23 12:50:35.468 8789-8789/com.example.dell.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.myapplication/com.example.dell.myapplication.MainActivity}: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getDeviceId(IPhoneSubInfo.java:222)
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:249)
at com.example.dell.myapplication.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
12-23 12:50:35.513 8789-8793/com.example.dell.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 234K, 14% free 9604K/11143K, paused 3ms+3ms, total 49ms
12-23 12:50:48.168 8789-8789/com.example.dell.myapplication I/Process﹕ Sending signal. PID: 8789 SIG: 9
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell.myapplication" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Add READ_PHONE_STATE permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
in your manifest file
READ_PHONE_STATE
Allows read only access to phone state.
Note: If both your minSdkVersion and targetSdkVersion values are set
to 3 or lower, the system implicitly grants your app this permission.
If you don't need this permission, be sure your targetSdkVersion is 4
or higher.
Protection level: dangerous
Constant Value:
"android.permission.READ_PHONE_STATE"
in your manifest, you need to add followong permission in order to read phone number:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
In your logCat, system tells the you the reason of the error with this line:
Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
Add Permission READ_PHONE_STATE
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
I have this problem and figure out that you need to add READ_PHONE_NUMBERS if your targetSdkVersion is 31
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
You need to request that permission explicitly also
when i am using devsmartliberary from github in my project my app is going to crash i am unable to find out this error .i am new to android plz help me
this is my main activity
package com.example.horizontal;
import com.devsmart.android.ui.HorizontalListView;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] itemName = { "OverView", "History", "Budget", "Distribution",
"Statistics" };
Integer[] imageId = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.ic_launcher, R.drawable.image5 };
ListView lvList;
GridView gvList;
CustomerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.horizontal);
adapter = new CustomerAdapter(this, itemName, imageId);
// gvList = (GridView) findViewById(R.id.gvList);
// gvList.setAdapter(adapter);
HorizontalListView hlv = (HorizontalListView) findViewById(R.id.listview);
//lvList = (ListView) findViewById(R.id.lvList);
hlv.setAdapter(adapter);
}
#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;
}
}
This is my horizontal.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:background="#fff"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.devsmart.android.ui.HorizontialListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd" />
</LinearLayout>
Exception are getting here
03-24 15:19:36.692: E/AndroidRuntime(881): FATAL EXCEPTION: main
03-24 15:19:36.692: E/AndroidRuntime(881): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.horizontal/com.example.horizontal.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.devsmart.android.ui.HorizontialListView
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.os.Looper.loop(Looper.java:137)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-24 15:19:36.692: E/AndroidRuntime(881): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 15:19:36.692: E/AndroidRuntime(881): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 15:19:36.692: E/AndroidRuntime(881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-24 15:19:36.692: E/AndroidRuntime(881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-24 15:19:36.692: E/AndroidRuntime(881): at dalvik.system.NativeStart.main(Native Method)
03-24 15:19:36.692: E/AndroidRuntime(881): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.devsmart.android.ui.HorizontialListView
03-24 15:19:36.692: E/AndroidRuntime(881): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-24 15:19:36.692: E/AndroidRuntime(881): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.Activity.setContentView(Activity.java:1835)
03-24 15:19:36.692: E/AndroidRuntime(881): at com.example.horizontal.MainActivity.onCreate(MainActivity.java:26)
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.Activity.performCreate(Activity.java:4465)
Analysis:
com.devsmart.android.ui.HorizontialListView is the class path from
your library project which you have used in xml
If properly not refered, it cause Binary Xml Error
Your Log says:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.horizontal/com.example.horizontal.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.devsmart.android.ui.HorizontialListView
03-24 15:19:36.692: E/AndroidRuntime(881): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
It means: The library project is not properly referred in your project
Solution:
Right click on your project -> Android -> in the right bottom -> click add and point to the library project of horizontal library, so that it is refered properly in your project
Edit
horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.devsmart.android.ui.HorizontialListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd" />
</LinearLayout>
I had the same issue and then i downloaded it's jar file and added that in my project..
You better use Recycler View for horizontal scrolling that is much much better than Horizontal listView and is one of the latest approaches.
Secondly if you still want to use HSV then download this https://www.dropbox.com/s/ihfmr149z6liabb/devsmartlib.jar?dl=0 library and add it in your libraries folder and then right click and click Add as library and use HSV
You are getting inflate error that means android is not able to find your HorizontalListView class, here is check list for getting out of this error:
As said by #Devrath you have to add library project to your project, check ReferencingLibraryProject.
If you still get this error then right click on project-> Properties -> select java builder path-> select Order and Export-> Make sure that "Android private libraries" must be checked.
If still your problem not solved then check and make sure you have given correct class reference in you layout file. For custom view you need to give full path to your class by including package name <packagename>.HorizontalListView.
I had this same issue I've spent many infuriating hours figuring it out. The only solution I could come up with was to change the devsmart package name then re-add it to my project then added it to the libraries. I believe this started because I changed my project package name so don't change your package name when working with this library I guess.
Ok first you need to add an 's' to the project library folder 'devsmartlibs'
NOTE: That you only have to do this if it won't let you add the project because it's already been added as a project.
Then you import it as a project so: File -> Import -> Existing android code into workspace. Then select the project location in browse and select finish.
After that right click on Your Project -> Properties -> Android -> Add (Bottom Right) -> now add the new library you've added to you project (devsmartlibs).
If this causes issues make sure that devsmartlibs has the same 'android-support-v4.jar' as your project because this can cause issues.
This worked for me I hope it helps.
I got an error reported from a user the other day, but how it happened is boggling my mind.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.inthub.greenfly/com.inthub.greenfly.view.fragment.MyProfileFragment}: java.lang.ClassCastException: com.inthub.greenfly.view.fragment.MyProfileFragment cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.ClassCastException: com.inthub.greenfly.view.fragment.MyProfileFragment cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
I know your first thought is "you must be starting up the fragment with startActivity or something". I start up this fragment in one single place:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment, new MyProfileFragment());
ft.commit();
I have been unable to figure out how this was started as an activity and also have not been able to reproduce it. Any ideas where to continue looking?
My class extends Fragment and not FragmentActivity.
I have an onCreate, I've removed a little of the code to keep things short:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getActivity().getActionBar() != null) {
getActivity().getActionBar().setTitle(R.string.myprofile);
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_profile, container, false);
.... tons of lines setting up my view ....
}
I am declaring my fragments in the manifest file as activities. I am reading that it may be incorrect to do this. If that is the source of the problem why am I unable to reproduce the error across multiple devices?
<activity
android:name="com.inthub.greenfly.view.fragment.MyProfileFragment"
android:screenOrientation="portrait" >
</activity>
Update, just to be clear. I have had the incorrect activity lines in my manifest for quite some time and have never encountered this error. It was reported through Crashlytics from another user. I have spent hundreds of hours developing and testing across multiple devices and not encountered it. If it is as simple as removing the lines, why am I unable to reproduce it?
This question already has answers here:
Libraries do not get added to APK anymore after upgrade to ADT 22
(7 answers)
Closed 9 years ago.
I'm trying to use fragments to create a simple, persistent navigation bar. The problem is I'm getting the following Error output.
05-23 14:58:02.861: E/AndroidRuntime(882): FATAL EXCEPTION: main
05-23 14:58:02.861: E/AndroidRuntime(882): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {org.childrensmuseum.visittcmindy/org.childrensmuseum.visittcmindy.MainActivity}: java.lang.ClassNotFoundException: org.childrensmuseum.visittcmindy.MainActivity
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.access$600(ActivityThread.java:122)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.os.Looper.loop(Looper.java:137)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 14:58:02.861: E/AndroidRuntime(882): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-23 14:58:02.861: E/AndroidRuntime(882): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-23 14:58:02.861: E/AndroidRuntime(882): at dalvik.system.NativeStart.main(Native Method)
05-23 14:58:02.861: E/AndroidRuntime(882): Caused by: java.lang.ClassNotFoundException: org.childrensmuseum.visittcmindy.MainActivity
05-23 14:58:02.861: E/AndroidRuntime(882): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-23 14:58:02.861: E/AndroidRuntime(882): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-23 14:58:02.861: E/AndroidRuntime(882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
05-23 14:58:02.861: E/AndroidRuntime(882): ... 11 more
My main activity looks like this:
package org.childrensmuseum.visittcmindy;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.ImageButton;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFormat(PixelFormat.RGBA_8888);
findViewById(R.id.NavigationBar).getBackground().setDither(true);
ImageButton homeButton = (ImageButton) findViewById(R.id.home_button);
homeButton.setColorFilter(Color.argb(255, 90, 179, 0));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
My Fragment looks like this:
package org.childrensmuseum.visittcmindy;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainNavigation extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.navigation_main, container, false);
}
}
Lastly my main activity xml file looks like this:
<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" >
<ImageView
android:id="#+id/HomeBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/homescreen_desc"
android:src="#drawable/homescreen"
android:scaleType="centerCrop"
/>
<fragment android:name="org.childrensmuseum.visittcmindy.MainNavigation"
android:id="#+id/NavigationBar"
android:layout_gravity="bottom" />
</RelativeLayout>
What am I missing?
EDITED to try to make it a general answer to this issue (in this case, the correct answer is provided by #Raghunandan):
Humm... so your MainActivity is "lost" (ClassNotFoundException: org.childrensmuseum.visittcmindy.MainActivity):
1.- May be an Eclipse (or whatever IDE you are using) building issue. Clean your Project and rebuild it and/or restart Eclipse.
2.- It can be a problem with your Manifest file. Add your Activity in your 'AndroidManifest.xml', making sure your Package and Activity names are correctly written (check caps). An example could be:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.childrensmuseum.visittcmindy">
<application>
<activity android:name=".MainActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
3.- Doesn't seem to be your case, but sometimes external libraries are not correctly included in the Build Path. You have to put your third party libraries in a "libs" folder and re-reference them (Right-click, properties, Java Build Path, Libraries, Add Jar...).
Shared libraries like the Maps library, which is not a part of the standard Android library, must be declared in the Android Manifest. Open the AndroidManifest.xml file and add the following as a child of the element:
<uses-library android:name="com.google.android.maps"/>
4.- #Raghunandan gives a solution here in case you've updated to Android SDK Tools Rev 22.
Right click on your project goto properties. Java Build Path. Choose Order export tab. Make sure that Android Private Libraries is selected. If you have referenced library project. do the same for the library project also. Clean and Build.
I've been debugging for hours now and still no luck. I made a RSS Reader using Android fragments. It works amazingly well on devices running Android 4.0.x to 4.1.x but crashes on start on Android 4.2.x devices.
I'd really appreciate any help I can get.
Logcat:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.impsycho.androidpakistan/com.impsycho.androidpakistan.ItemListActivity}:
android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.impsycho.androidpakistan.ItemListActivity.onCreate(ItemListActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
Caused by: java.lang.NullPointerException
at com.impsycho.androidpakistan.ItemListFragment$GetAllThePosts.onPreExecute(ItemListFragment.java:157)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.impsycho.androidpakistan.ItemListFragment.onCreate(ItemListFragment.java:54)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:835)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
... 20 more
Sending signal. PID: 30286 SIG: 9
Main Activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class ItemListActivity extends FragmentActivity implements ItemListFragment.Callbacks {
private boolean mTwoPane;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
getActionBar().setDisplayShowTitleEnabled(false);
if (findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
((ItemListFragment) getSupportFragmentManager()
.findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
}
}
...
activity_item_list.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_list"
android:name="com.impsycho.androidpakistan.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemListActivity" />
activity_item_twopane.xml
<LinearLayout 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:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ItemListActivity" >
<fragment
android:id="#+id/item_list"
android:name="com.impsycho.androidpakistan.ItemListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5" />
<FrameLayout
android:id="#+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="5" />
</LinearLayout>
Okay, so I figured out what was wrong.
I was focusing on the wrong error. The error was actually in my method GetAllThePosts() which is called when the app is started, because of which the layout could not be inflated. And it's not even an error because it works perfectly well in all other Android versions.
For some reason, you can't change the view of menu items while the app is starting in Android 4.2.x
The code responsible was:
private class GetAllThePosts extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
RefreshMenuButton.setActionView(R.layout.action_progress);
RefreshMenuButton.expandActionView();
...
I just added a conditional statement to check if we were on Android 4.2 and higher. If we are it won't run the first time.