Passing ArrayList<String> from activity to fragment in android - java

I have been dealing with an issue for a while now that I cannot seem to figure out, I am trying to pass an ArrayList<String> from an activity to a fragment, however, I get the following stack trace when I run the code
03-30 19:26:38.465 28955-28955/org.tcnjteamandroid.cryptochat E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.tcnjteamandroid.cryptochat, PID: 28955
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.tcnjteamandroid.cryptochat/org.tcnjteamandroid.cryptochat.drawer}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5240)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at org.tcnjteamandroid.cryptochat.drawer.onCreate(drawer.java:53)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5240)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getStringArrayList(java.lang.String)' on a null object reference
at org.tcnjteamandroid.cryptochat.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:96)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at org.tcnjteamandroid.cryptochat.drawer.onCreate(drawer.java:53)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5240)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
the code I am using to pass this information and pick it up is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
ArrayList<String> menuItems = new ArrayList<String>();
menuItems.add("Test");
Bundle bundle = new Bundle();
bundle.putStringArrayList("edttext", menuItems);
/* set Fragmentclass Arguments */
NavigationDrawerFragment fragobj = new NavigationDrawerFragment();
fragobj.setArguments(bundle);
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
And the code I am using to pick up the new arguments is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<String> strtext = getArguments().getStringArrayList("edttext");
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
strtext));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
Any help I could get would be greatly appreciated!

you can use a class with static ArrayList property
public class Values
{
public static ArrayList<String> myArrayList;
}
in the activity:
ArrayList<String> menuItems = new ArrayList<String>();
Values.myArrayList = menuItems;
in the fragment
ArrayList<String> strtext = Values.myArrayAdapter;

Related

Android Application java lang exception

I have created an android application using Android Studio IDE while clicking the button in my app it does'nt navigate it to the another activity instead its throwing up the exception
Here is My Logcat:
02-10 11:33:56.030 7404-7404/com.example.mcs.oodoeg E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at org.apache.xmlrpc.common.XmlRpcController.<init>(XmlRpcController.java:31)
at org.apache.xmlrpc.client.XmlRpcClient.<init>(XmlRpcClient.java:51)
at com.example.mcs.oodoeg.MainActivity.getXmlRpcClient(MainActivity.java:138)
at com.example.mcs.oodoeg.MainActivity.registerUser(MainActivity.java:103)
at com.example.mcs.oodoeg.MainActivity$1.onClick(MainActivity.java:45)
at android.view.View.performClick(View.java:4275)
at android.view.View$PerformClick.run(View.java:17434)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:4947)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ExceptionInInitializerError
at org.apache.xmlrpc.common.TypeFactoryImpl.<clinit>(TypeFactoryImpl.java:88)
            at org.apache.xmlrpc.common.XmlRpcController.<init>(XmlRpcController.java:31)
            at org.apache.xmlrpc.client.XmlRpcClient.<init>(XmlRpcClient.java:51)
            at com.example.mcs.oodoeg.MainActivity.getXmlRpcClient(MainActivity.java:138)
            at com.example.mcs.oodoeg.MainActivity.registerUser(MainActivity.java:103)
            at com.example.mcs.oodoeg.MainActivity$1.onClick(MainActivity.java:45)
            at android.view.View.performClick(View.java:4275)
            at android.view.View$PerformClick.run(View.java:17434)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: org.apache.ws.commons.serialize.DOMSerializer
at org.apache.xmlrpc.serializer.NodeSerializer.<clinit>(NodeSerializer.java:30)
            at org.apache.xmlrpc.common.TypeFactoryImpl.<clinit>(TypeFactoryImpl.java:88)
            at org.apache.xmlrpc.common.XmlRpcController.<init>(XmlRpcController.java:31)
            at org.apache.xmlrpc.client.XmlRpcClient.<init>(XmlRpcClient.java:51)
            at com.example.mcs.oodoeg.MainActivity.getXmlRpcClient(MainActivity.java:138)
            at com.example.mcs.oodoeg.MainActivity.registerUser(MainActivity.java:103)
            at com.example.mcs.oodoeg.MainActivity$1.onClick(MainActivity.java:45)
            at android.view.View.performClick(View.java:4275)
            at android.view.View$PerformClick.run(View.java:17434)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:177)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
This is my Main Activity class i have created two buttons here called register and signin:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intObj = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intObj);
}
});
Button btn2;
btn2=(Button) findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent= new Intent(MainActivity.this,Sigin.class);
startActivity(intent);
}
} );
}
When the register button is clicked this Main Activity2 should be called:
public class MainActivity2 extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
}
When sigin button is clicked this sigin class should be called:
public class Sigin extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sigin);
}
}
I think you are missing ws-commons-util-1.0.2.jar from $EXIST_HOME/lib/core.
You have to make sure you have included at least all of the libraries from $EXIST_HOME/lib/core.

Application unfortunately has been stopped while implementing simple fragment

My MainActivity contents is :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
my_frag myFrag = new my_frag();
fragmentTransaction.replace(android.R.id.content,myFrag);
fragmentTransaction.commit();
}
fragment class is :
public class my_frag extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_frag, container, false);
}
Main xml is :
<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">
<fragment
android:layout_width="2000dp"
android:layout_height="250dp"
android:name="com.example.gcoeara.mywork.my_fragment"
android:id="#+id/fragment"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
tools:layout="#layout/fragment_my_frag" />
</RelativeLayout>
Namespace is as follows :
activity name : MainActivity.java
main xml : activity_main.xml
fragment name : my_frag.java
fragment class : fragment_my_frag.xml
system : windows 7
framework : android studio
log cat is :
Process: com.example.gcoeara.mywork, PID: 22847
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gcoeara.mywork/com.example.gcoeara.mywork.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at com.example.gcoeara.mywork.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: Fragment com.example.gcoeara.mywork.my_fragment did not create a view.
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
at android.app.Activity.onCreateView(Activity.java:5282)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
            at android.app.Activity.setContentView(Activity.java:2144)
            at com.example.gcoeara.mywork.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Change
android:name="com.example.gcoeara.mywork.my_fragment"
in your fragment to
android:name="com.example.gcoeara.mywork.my_frag"
Hope you have the package path for my_frag as
com.example.gcoeara.mywork.my_frag.

Create Custom View and Add to XML

I have created a custom View which draws a line:
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawLine(0, 0, 100, 100, paint);
}
}
I have a MainActivity class and activity_main layout where my layout contains an image and several buttons. I want to put the custom view that I created to my XML file(activity_main) where a line would be drawn on top of the image.
I tried adding it:
<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 and buttons here
<com.myapp.alphasickbro.myapplication2.DrawView
android:id="#+id/paintView"
android:layout_width="100dp"
android:layout_height="100dp"
/>
</RelativeLayout>
Here is my MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
But when I run it, the app unfortunately stops. Did I miss something? What lines of code should I add?
Any answer would be appreciated
EDIT:
04-18 15:24:47.517 26732-26732/com.myapp.alphasickbro.myapplication2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.alphasickbro.myapplication2, PID: 26732
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.alphasickbro.myapplication2/com.myapp.alphasickbro.myapplication2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.myapp.alphasickbro.myapplication2.DrawView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.myapp.alphasickbro.myapplication2.DrawView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
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:290)
at android.app.Activity.setContentView(Activity.java:2002)
at com.myapp.alphasickbro.myapplication2.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5411)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            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:290)
            at android.app.Activity.setContentView(Activity.java:2002)
            at com.myapp.alphasickbro.myapplication2.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5411)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.myapp.alphasickbro.myapplication2.DrawView.<init>(DrawView.java:27)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            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:290)
            at android.app.Activity.setContentView(Activity.java:2002)
            at com.myapp.alphasickbro.myapplication2.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5411)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
I think you need to add the missing constructors. Clean your project.
public class DrawView extends View {
Paint paint;
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLACK);
}
}

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo, all activities are registered in AndroidManifest.xml, dont know whats wrong

I'm new to Android and i'm trying to run an app (simple test app), but get this error(. I have registered all my activities in AndroidManifest.xml and i'm using Android Studio 1.0.2
Here is the logcat of an app:
01-03 20:34:24.128 17300-17300/com.example.temur.sherlocked D/AndroidRuntime﹕ Shutting down VM
01-03 20:34:24.134 17300-17300/com.example.temur.sherlocked E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.temur.sherlocked, PID: 17300
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.temur.sherlocked/com.example.temur.sherlocked.MainActivity}: java.lang.InstantiationException: com.example.temur.sherlocked.MainActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.lang.InstantiationException: com.example.temur.sherlocked.MainActivity
at java.lang.Class.newInstance(Class.java:1561)
at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1559)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:1892)
at com.example.temur.sherlocked.MainActivity.<init>(MainActivity.java:43)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1559)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
Code of MainActivity
package com.example.temur.sherlocked;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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.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);
}
TextView infoTextView = (TextView)findViewById(R.id.textViewAnswer);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if (requestCode==CHOOSE_THIEF){
if (resultCode==RESULT_OK){
String thiefName = data.getStringExtra(SecondActivity.THIEF);
infoTextView.setText(thiefName);
}else {
infoTextView.setText("");
}
}
}
static final private int CHOOSE_THIEF = 0;
public void onClick(View view) {
Intent questionIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(questionIntent,CHOOSE_THIEF);
}
}
Help me please :)
If you read other Stack Overflow questions, you will notice that well-written questions include the source code that is crashing (in your case, MainActivity) in addition to the stack trace.
In this case, I can tell just from the stack trace that you are calling findViewById() on your MainActivity too soon. This cannot be done until after your call to setContentView().
In particular, you appear to be calling findViewById() in the initializer of data member. Do not call inherited methods on Activity until after super.onCreate(), unless specifically directed otherwise.

Force close after clear memory and resume app

When I test my app, after close app and clean memory(in my case, CleanMaster app) and then reopen it again, it force close. I try search for cause but can't find solvable solution.
It work all fine if not clean memory. I wonder if anything need to be saved and restore after these incident. I'm quite newby for Android.
here is log
06-29 11:59:19.479 22283-22283/com.thaifasttel D/WebView﹕ onTrimMemory: 20
06-29 11:59:33.985 22524-22524/com.thaifasttel D/jdwp﹕ sendBufferedRequest : len=0x39
06-29 11:59:33.993 22524-22524/com.thaifasttel W/asset﹕ AssetManager-->addDefaultAssets CIP path not exsit!
06-29 11:59:34.050 22524-22524/com.thaifasttel D/dalvikvm﹕ newInstance failed: no <init>()
06-29 11:59:34.052 22524-22524/com.thaifasttel D/AndroidRuntime﹕ Shutting down VM
06-29 11:59:34.052 22524-22524/com.thaifasttel W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d6f9a8)
06-29 11:59:34.060 22524-22524/com.thaifasttel E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thaifasttel/com.thaifasttel.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.thaifasttel.MainActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.thaifasttel.MainActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
at com.thaifasttel.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.thaifasttel.MainActivity$PlaceholderFragment; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
            at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
            at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
            at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
            at com.thaifasttel.MainActivity.onCreate(MainActivity.java:73)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
public class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("My App", "onCreateView");
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
WebView myWebView;
RelativeLayout splash_view;
TextView splash_text;
SharedPreferences Usettings;
String lang="th";
int isloadwa=0;
AssetManager assetPath = getAssets();
String waAssetPath ="web";
String wabase=getApplicationContext().getFilesDir().getPath();
String waIntPath = "web";
String waIntPath2 = wabase+"/"+waIntPath;
String baseupdatefolder="http://www.test.com/test/webapp";
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("My App", "onActivityCreated");
myWebView = (WebView) getView().findViewById(R.id.webview);
splash_view = (RelativeLayout) getView().findViewById(R.id.splashview);
splash_text = (TextView) getView().findViewById(R.id.splash_status);
inituserPref();
copydir();
}
#Override
public void onStart(){
super.onStart();
Log.e("My App", "onstart");
}
#Override
public void onResume(){
super.onResume();
Log.e("My App", "onResume");
if(isloadwa==1) {
if (isConnected()) {
Log.e("My App", "check update in app");
splash_text.setText(a_check_update);
checkupdate();
}
}
}
// do a lot of method after this

Categories