i'm working on my project and i created an interface of my app.
now i want that when ever i click in image view of Automation i go to my next layout. So i tried to write this code
the layout in which i want to move after clicking Automation is this :
But when i'm running app it's saying
Application error
Unfortunately,animation(name of my project) has stopped.
Logcat:
Caused by: java.lang.NullPointerException
at com.example.animation.dashboard.onCreate(dashboard.java:25)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5336)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
What's mistake i'm making here ? i'm new to android so kindly help me out here.
Your view variable is not defined and you are trying to use it as it is null. But on the another hand, I dont think you need this, just go with findViewById without this view
It looks like view_rooms is not an activity - the second argument in your intent needs to be the context of the destination.
So if you want to go to new activity change your intent like this:
startActivity(new Intent(dashboard.this, SecondActivity.class));
You are using this in the dashboard activity line number 25
imgauto = view.findViewById(R.id.autoimg)
while your variable view is empty you have not assigned it something and also you are in activity so use it directly like this
imgauto = findViewById(R.id.autoimg)
try this and i am sure it will work perfectly.
Related
I use Navigation Component from Jetpack in my app. I have 2 fragments, e.g. FirstFragment and SecondFragment and I have navigation in navigation graph from the first to second. Everything works correctly this way. I want to add child fragment to FirstFragment. So in onCreateView method of FirstFragment class I added the line
getChildFragmentManager().beginTransaction().add(R.id.fragment_container, new SimpleFragment()).commit();
This way I have an inner fragment in FirstFragment. Navigation to SecondFragment from FirstFragment still works correctly, but when I press the back button in SecondFragment I get this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abc.def, PID: 28856
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
at android.view.ViewGroup.addView(ViewGroup.java:4768)
at android.view.ViewGroup.addView(ViewGroup.java:4708)
at android.view.ViewGroup.addView(ViewGroup.java:4681)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1353)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1642)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1736)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1800)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3096)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3050)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2688)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1369)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2633)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2377)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2333)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2230)
at androidx.fragment.app.FragmentManager$3.run(FragmentManager.java:414)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I get this error if I either press the back button or call navController.navigateUp() or navController.popBackStack(). If I remove the line
getChildFragmentManager().beginTransaction().add(R.id.fragment_container, new SimpleFragment()).commit();
from FirstFragment I have no error, so this means that there is a problem with child fragment manager, right? What is wrong here?
If I understand this correctly without knowing what you have without looking at your layout of "fragment_container" tells me that its type of ViewGroup which can have only one child at this point if you don't provide more information to it.
What that means is fragment_container already has a child and which you need to remove before you add your SimpleFragment.
Lets assume you have your first layout of fragment something like this
<someView>
<fragment_container/> // hear you are loading your first fragment and trying load the SimpleFragment as well which is causing problem.
</someView>
what you need is
<someView>
<fragment_container/> only load First Fragment "first_fragment_layout.xml"
</someView>
And in layout of First fragment
first_fragment_layout.xml
<someView>
<viewBlasBla/>
<viewBlasBla/>
<fragment_container/> load SimpleFragment here
</someView>
as you can see, you are indeed trying to do nested fragment which is not a Good Practice. try to avoid this if possible :).
you can reach me here
The problem was in SimpleFragment. I was keeping reference to the root view of that fragment (had View mRootView field), and didn't recreate it in case it wasn't null.
I have an issue I cannot seem to find the answer for.
I've been browsing the web for hours, I found people with similar issues and errors, but those problems have been solved with proper code which I already did implement ( error is not due to my code being broken. Up to date tutorials backs me up..(?) ), in my case my app worked fine during the majority of it's developing stage. But as I implemented an adView, my app suddenly started getting this error. I removed the Ad implementation but the error remains..
I have been looking at: Android Fragment no view found for ID?
But had no luck there.
I've Rebuilt, cleaned, restarted both android studio and my computer.
I peaked into R to see if fragment_layout had the assigned Id '0x7f09006d'.
I've tried changing name from fragment_layout to anything else, but no luck, also made a new content_main.xml to try it.
I'm using 27.1.1 implementations. Compile version 27, target 27, min 17.
I'm having a activity_main.xml with a drawer layout, that (app_bar_main.xml).
app_bar_main.xml does a (content_main.xml).
Content_main.xml's rootView has the ID of fragment_layout.
(This is standard template when creating a drawer layout Activity)
This is how I change to the fragments, using fragment_layout
compare_loan compareLoan = new compare_loan();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, compareLoan, compareLoan.getTag()).commit();
(I get the following error:
java.lang.IllegalArgumentException: No view found for id 0x7f09006d (com.something.somethingelse.appname:id/fragment_layout) for fragment compare_loan{aee705c #0 id=0x7f09006d}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1422)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
12-13 17:57:48.000 25339-29001/system_process E/memtrack: Couldn't load memtrack module
Anyone has any clue? As said it has been working fine for days. No big changes could have affected this. Only thing I can point out is adView implementation. Which is now removed from 'implementation' now.
I hope this makes sense for you guys. This is my first post.
I hope to provide a solution if I find one.
I have added a menu in a simple app of hello world! But my app keeps crashing.
I am not adding my xml code. My xml file name is menu inside the menu folder in res.
Here is my java file.
MyActivity.java
public boolean onCreateOptionsMenu(Menu menu1){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu1); //line 19
return true;
}
Logcat
06-03 18:03:57.682 13542-13542/com.example.kaushalraj.a424 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kaushalraj.a424, PID: 13542
android.view.InflateException: Couldn't resolve menu item onClick handler action in class com.example.kaushalraj.a424.MainActivity
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:253)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NoSuchMethodException: action [interface android.view.MenuItem]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:249)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I don't know if this your case (and i don't have the reputation to comment) but: What app theme are you using? If you are using a theme that does not have a actionbar at the top then you will get this error when you try to attach your menu to it, as the actionbar does not exist.
You have two solutions:
Check your theme in res/values/styles.xml if you are using something like Theme.AppCompat.Light.NoActionBar you must change it to something like Theme.AppCompat.Light.DarkActionBar. This will add the actionbar to your activity.
Using the support toolbar library, add the toolbar to your activity xml layout then from your activity class get the view in OnCreate() and use setSupportActionBar(myToolbar). This will set your toolbar view as the actionbar where your menu will be attached to.
This question already has answers here:
Your content must have a ListView whose id attribute is 'android.R.id.list'
(7 answers)
Closed 6 years ago.
I've got a problem when trying to run my app in Android Studio. I get a
FATAL EXCEPTION: main
more precisely there seems to be a problem at:
dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
which is:
setContentView(R.layout.activity_chords_list);
here is the ChordsListActivity class:
import android.app.ListActivity;
import android.os.Bundle;
public class ChordsListActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chords_list);
String[] accordi = new String[] {"Do maggiore", "Re maggiore", "Si maggiore"};
String[] note = new String[] {"note: Do, Mi, Sol", "note: La, Re, Fa#", "note: Fa#, Si, Re#"};
int[] immagini = new int[] {R.drawable.do_maggiore, R.drawable.re_maggiore, R.drawable.si_maggiore};
CustomListAdapter adapter = new CustomListAdapter(getApplicationContext(), accordi, note, immagini);
setListAdapter(adapter);
}
}
I was kinda able to solve it by extending Activity instead of ListActivity but then I cannot use the setListAdapter() method.
I'd really appreciate if someone could help me fix the code!
please let me know if you need me to post the custom Adapter or any other file.
Edit: added the whole stack trace
FATAL EXCEPTION: main
Process: dancam.com.chords, PID: 3080
java.lang.RuntimeException: Unable to start activity ComponentInfo{dancam.com.chords/dancam.com.chords.ChordsListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:398)
at android.app.Activity.setContentView(Activity.java:2166)
at dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
This is problem with ListView.
Go to R.layout.activity_chords_list and add <ListView> and set id android:id="#android:id/list"
Like Exception says: 'Your content must have a ListView whose id attribute is 'android.R.id.list'
It should works if rest of the code is ok.
When you extend from ListActivity, it already has ListView, but Activity doesn't. You have to create list inbActivity's layout- better use RecyclerView instead of List view. In your activity create RecyclerView reference, initialize it in onCreate. Create adapter for your data in activity (onCreate method). Finnaly, set adapter to listview reference. In order to modify data in a list, set new data in adapter and call .notifyDatasetChange () on it. Notice, for RecyclerView you will also need LayoutManager (see docs)
I have the following problem:
My app consists of several fragments that are dynammicly added. There is one fragment with a push button and an textedit (called 'fragA').
If I click the push button I want to show an different fragment with some text (called 'fragB'). I do this with the following code (in fragA class):
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Fragment howFragment = new HowFragment();
FragmentTransaction transaction = null;
transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.flQuestion, howFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
Now the problem is this:
When i push the button on fragA the fragment (fragB) is created and show on the screen but when I push on the location where the pushbutton was on fragA it makes an new fragment (fragB). Also if i push where the textedit on fragA was located it opens an keyboard on fragB..
It looks like FragB is just overlaying fragA without replacing it.
I also want to achieve that when i swype to the next fragment that fragB is removed and fragA is just showed normally (state when not pressed button)
Update #
When trying to add and remove this is the following logcat output:
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: No view found for id 0x7f090015 (com.example.eindwerkappv1:id/flQuestion) for fragment HowFragment{419c93c8 #3 id=0x7f090015}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
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:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
I would guess your fragA fragment is defined in your XML layout?
According to the documentation, this is what happens when you use the <fragment> tag:
the system inserts the View returned by the fragment directly in place
of the element.
This is why you cannot remove the previous fragment, as it does not exist.
If you want to changes fragment from code, you have to add the first fragment from code too.
You need to use a container like a FrameLayout, and add the first fragment to this container in the onCreate() of your activity, using FragmentTransaction.add().
Then FragmentTransaction.replace() method should work.
So why don't you just call the .remove() on the EditText Fragment and .add() the new TextView Framgent instead on osing the .replace()
do some thing like this:
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(addCommentFragment)
.add(R.id.containerForFragments, commentFragment, "comment"+
String.valueOf(numOfComments)).commit();
this worked for me.
I found the solution now.
I still use the replace method but I had to catch the ontouch event in the new fragment:
Fragment over another fragment issue
See previous link for the answer