2

I need to call a method listview.setOnItemClickListener () and when I found this site on the solution, then I try realize it , but get an error with it at any mActivePosition, I can not understand what the problem, is here logcat:

12-28 05:33:39.324  13066-13066/com.example.SmsService E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.SmsService, PID: 13066
java.lang.NullPointerException
        at com.example.SmsService.VideoView$3.getView(VideoView.java:140)
        at com.example.SmsService.VideoView.onCardboardTrigger(VideoView.java:217)
        at com.google.vrtoolkit.cardboard.CardboardActivity$SensorListener.onCardboardTrigger(CardboardActivity.java:84)
        at com.google.vrtoolkit.cardboard.sensors.MagnetSensor$TriggerDetector$1.run(MagnetSensor.java:141)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        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:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
        at dalvik.system.NativeStart.main(Native Method)

there is problem string :

listview.getAdapter().getView(1, null, null),

And there is all my class, gist. Сan anyone tell me why this any typing errors and how it can fix that would work?

Community
  • 1
  • 1
  • please i do not really understand you can you come again, and its ok to pass null, but for some reasons its bad so just put your listiview in as a parent.. the listview is the parent not the listview's parent.. get me? and also i didnt understand what you said about the performclick and performitemclick – Elltz Dec 28 '14 at 13:00
  • on a second thought, use null..as the parent ok..its ok.. – Elltz Dec 28 '14 at 13:05
  • @Elltz, `performclick and performitemclick`- as I said I need to perform a method setOnItemClickListener that would go to the next item in the listView, but when I perform `performclick and performitemclick` then get(go) in method getView. – Александр Рихтер Dec 28 '14 at 13:09
  • @Elltz, from the response that gave the user **HBizhi** I have not worked `performclick and performitemclick` – Александр Рихтер Dec 28 '14 at 13:13
  • nevermind, you see the line 140 in your code in the getview?? do this rather replace this with that code okay `View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_leyout, parent, false)` and clean, rebuild and run again, the error should be gone – Elltz Dec 28 '14 at 13:28
  • @Elltz, yes it works , but still in the performance I get in `getView` method but `listview.setOnItemClickListener` not be carrying out. I need to perform `listview.setOnItemClickListener` that would go to the next item in the list `listView` – Александр Рихтер Dec 28 '14 at 13:52
  • can you please explain what you want to do in detail, as in what you want the listivew to do, becuase i see two listivew attached to the same adapter right? so please explain with detail what you want to do – Elltz Dec 28 '14 at 22:27
  • also i want to explain something to you `listview.performItemClick(listview.getAdapter().getView(1, null, null),1,listview.getAdapter().getItemId(1)` the parameters where you placed **1** must be the position of the click event so you must put `mSelectedItem+=1;` there, or? – Elltz Dec 28 '14 at 22:31
  • @Elltz, I think it is clear that I am only a novice programmer , but `mSelectedItem+=1` I understand , and the transition to the element is not carried out – Александр Рихтер Dec 28 '14 at 22:37
  • @Elltz, Here is another topic that I raised , because there is already a whole day can not solve your problem, you mey see it http://stackoverflow.com/questions/27681266/move-to-the-next-item-android-horisontallistview – Александр Рихтер Dec 28 '14 at 22:39
  • @Elltz, I also tried to somehow directly call the method `OnItemClickListener ()` so trying to make something of this `listview.getOnItemClickListener().onItemClick((AdapterView>) listview.getParent(),(ViewGroup)listview.getParent(),2, listview.getAdapter().getItemId(2));` – Александр Рихтер Dec 28 '14 at 22:41
  • last question before i try to answer, does the background color change? after you perform click? – Elltz Dec 28 '14 at 23:33
  • @Elltz, `background color change` - no, why should he change? – Александр Рихтер Dec 28 '14 at 23:44
  • because yo have this `retval.setBackgroundColor(Color.WHITE` so i was wondering if it changes, so i can know if really your getview method is called fully or not..okay.. – Elltz Dec 28 '14 at 23:47
  • @Elltz, I do not understand correctly , but the background color changes back , that picture is what it looks [like](http://pixs.ru/showimage/xdzqUAkngj_2470358_15358904.jpg) – Александр Рихтер Dec 28 '14 at 23:59
  • @Elltz, everything works well , but like I said I just want to figure out how to move to the next item in the list listView – Александр Рихтер Dec 29 '14 at 00:00
  • oh ok, good, if that's the case then use `listview.setSelection(number);` that is if you know the next item position.. but if you do not know the next item position `listview.setSelection(getPositionForView(getSelectedView())+1);` or simpily this `listview.setSelection(getSelectedItemPosition()+1)` or you could scroll to the next item by `smoothScrollToPosition(int position);` – Elltz Dec 29 '14 at 00:11
  • @Elltz, I take you it's but you don't see http://stackoverflow.com/questions/27681266/move-to-the-next-item-android-horisontallistview – Александр Рихтер Dec 29 '14 at 00:18

1 Answers1

0

Where are you trying to use performClick()?

If calling performItemClick on the onCreate() method, there would be NullPinterException,

Because the screen is not fully visible to the user.

Try calling this method in onWindowFocusChanged() like below:

listview.getAdapter().getView(1, null, null).performClick();

Instead of that you can also use same delay after:

Handler().postDelayed(new Runnable() 
{ 
    @Override public void run() {
                  listView.performItemClick(listView.getAdapter()
                      .getView(1, null, null), 0, listView.getAdapter().getItemId(1)); 
               } 
}, 2000);

Important note: This will work for single item that is item in position 1.

Also, If your question is all about using onWindowFocusChanged try checking this:

public void onWindowFocusChanged(boolean hasFocus) {

            super.onWindowFocusChanged(hasFocus);

          if(hasFocus) 
             Toast.makeText(context, text, duration).show();
    }
Hana Bzh
  • 2,212
  • 3
  • 18
  • 38