How to use onClick for multiple ImageViews for starting other activities









up vote
0
down vote

favorite
1












For the JAVA file:



public class UserDataInputActivity extends AppCompatActivity

@Override
protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data_input);


public void onGenericMenuClick(View view)

ImageView pastTests = (ImageView) findViewById(R.id.pasttests);
final Intent intent1 = new Intent(this, PastDiagnosticResult.class);
ImageView userData = (ImageView) findViewById(R.id.myinfo);
final Intent intent2 = new Intent(this, UserDataInputActivity.class);
ImageView currentTests = (ImageView) (findViewById(R.id.currenttest));
final Intent intent3 = new Intent(this, CurrentDiagnosticResultActivity.class);

pastTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent1);
);
userData.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent2);
);
currentTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent3);
);




I essentially have 3 icons (imageViews) on the bottom of my screen. My objective: to navigate to different screens as I click the different images on the menu. However, my java code does not work correctly. Although it runs, it crashes after I try to use Intents 2 and 3 consecutively. Is there a better way to do this?



Corresponding LOGCAT Error Output:



2018-11-11 02:22:02.224 7638-7638/com.example.owner.introductoryapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.owner.introductoryapplication, PID: 7638
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:24802)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6521)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.owner.introductoryapplication.CurrentDiagnosticResultActivity.onGenericMenuClick(CurrentDiagnosticResultActivity.java:28)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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)


Edit #2 - XML File:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2E2A27"
tools:context=".UserDataInputActivity">

<ImageView
android:id="@+id/header"
android:src="@drawable/companyicon"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#211F1E"
tools:layout_editor_absoluteY="4dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="@string/PersonalInfo"
android:textSize="30sp"
style="@style/TextHeaderAttr"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:background="#211F1E">

<ImageView
android:src="@drawable/diagnosisicon"
android:id="@+id/pasttests"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/profileicon"
android:id="@+id/myinfo"
style="@style/IconStyle"
android:tint="#A9A9A9"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/resultsicon"
android:id="@+id/currenttest"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
</LinearLayout>

<!-- CONTENT -->

</RelativeLayout>


 










share|improve this question























  • what is generated Exception and logcat result?
    – Farrokh
    Nov 11 at 9:14










  • @Farrokh Please see edit! Thank you.
    – Shounak Ray
    Nov 11 at 9:24










  • you have NullPointerException that says clicked imageview has not been initialized correctly, then for a better answer, can you put your xml layout file here?
    – Farrokh
    Nov 11 at 9:28











  • @Farrokh Please see the second edit! Thanks again.
    – Shounak Ray
    Nov 11 at 9:31











  • i hope this was last edit for your code, where do you call onGenericMenuClick method, you should to call it at onCreate or onViewCreated
    – Farrokh
    Nov 11 at 10:02














up vote
0
down vote

favorite
1












For the JAVA file:



public class UserDataInputActivity extends AppCompatActivity

@Override
protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data_input);


public void onGenericMenuClick(View view)

ImageView pastTests = (ImageView) findViewById(R.id.pasttests);
final Intent intent1 = new Intent(this, PastDiagnosticResult.class);
ImageView userData = (ImageView) findViewById(R.id.myinfo);
final Intent intent2 = new Intent(this, UserDataInputActivity.class);
ImageView currentTests = (ImageView) (findViewById(R.id.currenttest));
final Intent intent3 = new Intent(this, CurrentDiagnosticResultActivity.class);

pastTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent1);
);
userData.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent2);
);
currentTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent3);
);




I essentially have 3 icons (imageViews) on the bottom of my screen. My objective: to navigate to different screens as I click the different images on the menu. However, my java code does not work correctly. Although it runs, it crashes after I try to use Intents 2 and 3 consecutively. Is there a better way to do this?



Corresponding LOGCAT Error Output:



2018-11-11 02:22:02.224 7638-7638/com.example.owner.introductoryapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.owner.introductoryapplication, PID: 7638
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:24802)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6521)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.owner.introductoryapplication.CurrentDiagnosticResultActivity.onGenericMenuClick(CurrentDiagnosticResultActivity.java:28)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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)


Edit #2 - XML File:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2E2A27"
tools:context=".UserDataInputActivity">

<ImageView
android:id="@+id/header"
android:src="@drawable/companyicon"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#211F1E"
tools:layout_editor_absoluteY="4dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="@string/PersonalInfo"
android:textSize="30sp"
style="@style/TextHeaderAttr"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:background="#211F1E">

<ImageView
android:src="@drawable/diagnosisicon"
android:id="@+id/pasttests"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/profileicon"
android:id="@+id/myinfo"
style="@style/IconStyle"
android:tint="#A9A9A9"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/resultsicon"
android:id="@+id/currenttest"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
</LinearLayout>

<!-- CONTENT -->

</RelativeLayout>


 










share|improve this question























  • what is generated Exception and logcat result?
    – Farrokh
    Nov 11 at 9:14










  • @Farrokh Please see edit! Thank you.
    – Shounak Ray
    Nov 11 at 9:24










  • you have NullPointerException that says clicked imageview has not been initialized correctly, then for a better answer, can you put your xml layout file here?
    – Farrokh
    Nov 11 at 9:28











  • @Farrokh Please see the second edit! Thanks again.
    – Shounak Ray
    Nov 11 at 9:31











  • i hope this was last edit for your code, where do you call onGenericMenuClick method, you should to call it at onCreate or onViewCreated
    – Farrokh
    Nov 11 at 10:02












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





For the JAVA file:



public class UserDataInputActivity extends AppCompatActivity

@Override
protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data_input);


public void onGenericMenuClick(View view)

ImageView pastTests = (ImageView) findViewById(R.id.pasttests);
final Intent intent1 = new Intent(this, PastDiagnosticResult.class);
ImageView userData = (ImageView) findViewById(R.id.myinfo);
final Intent intent2 = new Intent(this, UserDataInputActivity.class);
ImageView currentTests = (ImageView) (findViewById(R.id.currenttest));
final Intent intent3 = new Intent(this, CurrentDiagnosticResultActivity.class);

pastTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent1);
);
userData.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent2);
);
currentTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent3);
);




I essentially have 3 icons (imageViews) on the bottom of my screen. My objective: to navigate to different screens as I click the different images on the menu. However, my java code does not work correctly. Although it runs, it crashes after I try to use Intents 2 and 3 consecutively. Is there a better way to do this?



Corresponding LOGCAT Error Output:



2018-11-11 02:22:02.224 7638-7638/com.example.owner.introductoryapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.owner.introductoryapplication, PID: 7638
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:24802)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6521)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.owner.introductoryapplication.CurrentDiagnosticResultActivity.onGenericMenuClick(CurrentDiagnosticResultActivity.java:28)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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)


Edit #2 - XML File:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2E2A27"
tools:context=".UserDataInputActivity">

<ImageView
android:id="@+id/header"
android:src="@drawable/companyicon"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#211F1E"
tools:layout_editor_absoluteY="4dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="@string/PersonalInfo"
android:textSize="30sp"
style="@style/TextHeaderAttr"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:background="#211F1E">

<ImageView
android:src="@drawable/diagnosisicon"
android:id="@+id/pasttests"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/profileicon"
android:id="@+id/myinfo"
style="@style/IconStyle"
android:tint="#A9A9A9"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/resultsicon"
android:id="@+id/currenttest"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
</LinearLayout>

<!-- CONTENT -->

</RelativeLayout>


 










share|improve this question















For the JAVA file:



public class UserDataInputActivity extends AppCompatActivity

@Override
protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data_input);


public void onGenericMenuClick(View view)

ImageView pastTests = (ImageView) findViewById(R.id.pasttests);
final Intent intent1 = new Intent(this, PastDiagnosticResult.class);
ImageView userData = (ImageView) findViewById(R.id.myinfo);
final Intent intent2 = new Intent(this, UserDataInputActivity.class);
ImageView currentTests = (ImageView) (findViewById(R.id.currenttest));
final Intent intent3 = new Intent(this, CurrentDiagnosticResultActivity.class);

pastTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent1);
);
userData.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent2);
);
currentTests.setOnClickListener(new OnClickListener()
public void onClick(View view)

startActivity(intent3);
);




I essentially have 3 icons (imageViews) on the bottom of my screen. My objective: to navigate to different screens as I click the different images on the menu. However, my java code does not work correctly. Although it runs, it crashes after I try to use Intents 2 and 3 consecutively. Is there a better way to do this?



Corresponding LOGCAT Error Output:



2018-11-11 02:22:02.224 7638-7638/com.example.owner.introductoryapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.owner.introductoryapplication, PID: 7638
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:24802)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6521)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.owner.introductoryapplication.CurrentDiagnosticResultActivity.onGenericMenuClick(CurrentDiagnosticResultActivity.java:28)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6312) 
at android.view.View$PerformClick.run(View.java:24802) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
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)


Edit #2 - XML File:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2E2A27"
tools:context=".UserDataInputActivity">

<ImageView
android:id="@+id/header"
android:src="@drawable/companyicon"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#211F1E"
tools:layout_editor_absoluteY="4dp" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="82dp"
android:text="@string/PersonalInfo"
android:textSize="30sp"
style="@style/TextHeaderAttr"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:background="#211F1E">

<ImageView
android:src="@drawable/diagnosisicon"
android:id="@+id/pasttests"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/profileicon"
android:id="@+id/myinfo"
style="@style/IconStyle"
android:tint="#A9A9A9"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
<ImageView
android:src="@drawable/resultsicon"
android:id="@+id/currenttest"
style="@style/IconStyle"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:onClick="onGenericMenuClick"/>
</LinearLayout>

<!-- CONTENT -->

</RelativeLayout>


 







java android xml android-intent imageview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:31

























asked Nov 11 at 9:11









Shounak Ray

185




185











  • what is generated Exception and logcat result?
    – Farrokh
    Nov 11 at 9:14










  • @Farrokh Please see edit! Thank you.
    – Shounak Ray
    Nov 11 at 9:24










  • you have NullPointerException that says clicked imageview has not been initialized correctly, then for a better answer, can you put your xml layout file here?
    – Farrokh
    Nov 11 at 9:28











  • @Farrokh Please see the second edit! Thanks again.
    – Shounak Ray
    Nov 11 at 9:31











  • i hope this was last edit for your code, where do you call onGenericMenuClick method, you should to call it at onCreate or onViewCreated
    – Farrokh
    Nov 11 at 10:02
















  • what is generated Exception and logcat result?
    – Farrokh
    Nov 11 at 9:14










  • @Farrokh Please see edit! Thank you.
    – Shounak Ray
    Nov 11 at 9:24










  • you have NullPointerException that says clicked imageview has not been initialized correctly, then for a better answer, can you put your xml layout file here?
    – Farrokh
    Nov 11 at 9:28











  • @Farrokh Please see the second edit! Thanks again.
    – Shounak Ray
    Nov 11 at 9:31











  • i hope this was last edit for your code, where do you call onGenericMenuClick method, you should to call it at onCreate or onViewCreated
    – Farrokh
    Nov 11 at 10:02















what is generated Exception and logcat result?
– Farrokh
Nov 11 at 9:14




what is generated Exception and logcat result?
– Farrokh
Nov 11 at 9:14












@Farrokh Please see edit! Thank you.
– Shounak Ray
Nov 11 at 9:24




@Farrokh Please see edit! Thank you.
– Shounak Ray
Nov 11 at 9:24












you have NullPointerException that says clicked imageview has not been initialized correctly, then for a better answer, can you put your xml layout file here?
– Farrokh
Nov 11 at 9:28





you have NullPointerException that says clicked imageview has not been initialized correctly, then for a better answer, can you put your xml layout file here?
– Farrokh
Nov 11 at 9:28













@Farrokh Please see the second edit! Thanks again.
– Shounak Ray
Nov 11 at 9:31





@Farrokh Please see the second edit! Thanks again.
– Shounak Ray
Nov 11 at 9:31













i hope this was last edit for your code, where do you call onGenericMenuClick method, you should to call it at onCreate or onViewCreated
– Farrokh
Nov 11 at 10:02




i hope this was last edit for your code, where do you call onGenericMenuClick method, you should to call it at onCreate or onViewCreated
– Farrokh
Nov 11 at 10:02












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You are not using onGenericMenuClick() as you should.

This is the common onClick() for all 3 views so you don't define inside it new listeners.

Change to this:



public void onGenericMenuClick(View view) 
Intent intent;

if(v.getId() == R.id.pasttests)
intent = new Intent(this, PastDiagnosticResult.class);
else if (v.getId() == R.id.myinfo)
intent = new Intent(this, UserDataInputActivity.class);
else if (v.getId() == R.id.currenttest)
intent = new Intent(this, CurrentDiagnosticResultActivity.class);


startActivity(intent);






share|improve this answer




















  • Thank you @forpas. Do you know how to eliminate the fading between the activities as I click the button? I having some trouble...
    – Shounak Ray
    Nov 11 at 17:20










  • This would happen for various reasons: the theme of your app, the drawables you load or any other heavy load, sync tasks that are executed, even the device you run the app and more.
    – forpas
    Nov 11 at 17:23










  • Is there any way to eliminate the fading?
    – Shounak Ray
    Nov 11 at 20:37










  • I really can't tell, but the first thing that comes to mind is a very complex or loaded layout with large drawables maybe. So try first by creating lighter drawables or no drawables at all just for testing.
    – forpas
    Nov 11 at 20:42

















up vote
1
down vote













This is a case, where you should use Fragments instead Activities.



1.Replace three Activities with fragments.



2.Use FragmentManager to switch between fragments.



UserDataInputFragment userDataInputFragment = UserDataInputFragment.newInstance("","");

getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment, userDataInputFragment, "userDataInputFragment")
.commit();
//here fragment in R.id.fragment is the container of Fragments





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247290%2fhow-to-use-onclick-for-multiple-imageviews-for-starting-other-activities%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You are not using onGenericMenuClick() as you should.

    This is the common onClick() for all 3 views so you don't define inside it new listeners.

    Change to this:



    public void onGenericMenuClick(View view) 
    Intent intent;

    if(v.getId() == R.id.pasttests)
    intent = new Intent(this, PastDiagnosticResult.class);
    else if (v.getId() == R.id.myinfo)
    intent = new Intent(this, UserDataInputActivity.class);
    else if (v.getId() == R.id.currenttest)
    intent = new Intent(this, CurrentDiagnosticResultActivity.class);


    startActivity(intent);






    share|improve this answer




















    • Thank you @forpas. Do you know how to eliminate the fading between the activities as I click the button? I having some trouble...
      – Shounak Ray
      Nov 11 at 17:20










    • This would happen for various reasons: the theme of your app, the drawables you load or any other heavy load, sync tasks that are executed, even the device you run the app and more.
      – forpas
      Nov 11 at 17:23










    • Is there any way to eliminate the fading?
      – Shounak Ray
      Nov 11 at 20:37










    • I really can't tell, but the first thing that comes to mind is a very complex or loaded layout with large drawables maybe. So try first by creating lighter drawables or no drawables at all just for testing.
      – forpas
      Nov 11 at 20:42














    up vote
    2
    down vote



    accepted










    You are not using onGenericMenuClick() as you should.

    This is the common onClick() for all 3 views so you don't define inside it new listeners.

    Change to this:



    public void onGenericMenuClick(View view) 
    Intent intent;

    if(v.getId() == R.id.pasttests)
    intent = new Intent(this, PastDiagnosticResult.class);
    else if (v.getId() == R.id.myinfo)
    intent = new Intent(this, UserDataInputActivity.class);
    else if (v.getId() == R.id.currenttest)
    intent = new Intent(this, CurrentDiagnosticResultActivity.class);


    startActivity(intent);






    share|improve this answer




















    • Thank you @forpas. Do you know how to eliminate the fading between the activities as I click the button? I having some trouble...
      – Shounak Ray
      Nov 11 at 17:20










    • This would happen for various reasons: the theme of your app, the drawables you load or any other heavy load, sync tasks that are executed, even the device you run the app and more.
      – forpas
      Nov 11 at 17:23










    • Is there any way to eliminate the fading?
      – Shounak Ray
      Nov 11 at 20:37










    • I really can't tell, but the first thing that comes to mind is a very complex or loaded layout with large drawables maybe. So try first by creating lighter drawables or no drawables at all just for testing.
      – forpas
      Nov 11 at 20:42












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    You are not using onGenericMenuClick() as you should.

    This is the common onClick() for all 3 views so you don't define inside it new listeners.

    Change to this:



    public void onGenericMenuClick(View view) 
    Intent intent;

    if(v.getId() == R.id.pasttests)
    intent = new Intent(this, PastDiagnosticResult.class);
    else if (v.getId() == R.id.myinfo)
    intent = new Intent(this, UserDataInputActivity.class);
    else if (v.getId() == R.id.currenttest)
    intent = new Intent(this, CurrentDiagnosticResultActivity.class);


    startActivity(intent);






    share|improve this answer












    You are not using onGenericMenuClick() as you should.

    This is the common onClick() for all 3 views so you don't define inside it new listeners.

    Change to this:



    public void onGenericMenuClick(View view) 
    Intent intent;

    if(v.getId() == R.id.pasttests)
    intent = new Intent(this, PastDiagnosticResult.class);
    else if (v.getId() == R.id.myinfo)
    intent = new Intent(this, UserDataInputActivity.class);
    else if (v.getId() == R.id.currenttest)
    intent = new Intent(this, CurrentDiagnosticResultActivity.class);


    startActivity(intent);







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 10:23









    forpas

    4,9971217




    4,9971217











    • Thank you @forpas. Do you know how to eliminate the fading between the activities as I click the button? I having some trouble...
      – Shounak Ray
      Nov 11 at 17:20










    • This would happen for various reasons: the theme of your app, the drawables you load or any other heavy load, sync tasks that are executed, even the device you run the app and more.
      – forpas
      Nov 11 at 17:23










    • Is there any way to eliminate the fading?
      – Shounak Ray
      Nov 11 at 20:37










    • I really can't tell, but the first thing that comes to mind is a very complex or loaded layout with large drawables maybe. So try first by creating lighter drawables or no drawables at all just for testing.
      – forpas
      Nov 11 at 20:42
















    • Thank you @forpas. Do you know how to eliminate the fading between the activities as I click the button? I having some trouble...
      – Shounak Ray
      Nov 11 at 17:20










    • This would happen for various reasons: the theme of your app, the drawables you load or any other heavy load, sync tasks that are executed, even the device you run the app and more.
      – forpas
      Nov 11 at 17:23










    • Is there any way to eliminate the fading?
      – Shounak Ray
      Nov 11 at 20:37










    • I really can't tell, but the first thing that comes to mind is a very complex or loaded layout with large drawables maybe. So try first by creating lighter drawables or no drawables at all just for testing.
      – forpas
      Nov 11 at 20:42















    Thank you @forpas. Do you know how to eliminate the fading between the activities as I click the button? I having some trouble...
    – Shounak Ray
    Nov 11 at 17:20




    Thank you @forpas. Do you know how to eliminate the fading between the activities as I click the button? I having some trouble...
    – Shounak Ray
    Nov 11 at 17:20












    This would happen for various reasons: the theme of your app, the drawables you load or any other heavy load, sync tasks that are executed, even the device you run the app and more.
    – forpas
    Nov 11 at 17:23




    This would happen for various reasons: the theme of your app, the drawables you load or any other heavy load, sync tasks that are executed, even the device you run the app and more.
    – forpas
    Nov 11 at 17:23












    Is there any way to eliminate the fading?
    – Shounak Ray
    Nov 11 at 20:37




    Is there any way to eliminate the fading?
    – Shounak Ray
    Nov 11 at 20:37












    I really can't tell, but the first thing that comes to mind is a very complex or loaded layout with large drawables maybe. So try first by creating lighter drawables or no drawables at all just for testing.
    – forpas
    Nov 11 at 20:42




    I really can't tell, but the first thing that comes to mind is a very complex or loaded layout with large drawables maybe. So try first by creating lighter drawables or no drawables at all just for testing.
    – forpas
    Nov 11 at 20:42












    up vote
    1
    down vote













    This is a case, where you should use Fragments instead Activities.



    1.Replace three Activities with fragments.



    2.Use FragmentManager to switch between fragments.



    UserDataInputFragment userDataInputFragment = UserDataInputFragment.newInstance("","");

    getSupportFragmentManager().beginTransaction()
    .replace(R.id.fragment, userDataInputFragment, "userDataInputFragment")
    .commit();
    //here fragment in R.id.fragment is the container of Fragments





    share|improve this answer


























      up vote
      1
      down vote













      This is a case, where you should use Fragments instead Activities.



      1.Replace three Activities with fragments.



      2.Use FragmentManager to switch between fragments.



      UserDataInputFragment userDataInputFragment = UserDataInputFragment.newInstance("","");

      getSupportFragmentManager().beginTransaction()
      .replace(R.id.fragment, userDataInputFragment, "userDataInputFragment")
      .commit();
      //here fragment in R.id.fragment is the container of Fragments





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        This is a case, where you should use Fragments instead Activities.



        1.Replace three Activities with fragments.



        2.Use FragmentManager to switch between fragments.



        UserDataInputFragment userDataInputFragment = UserDataInputFragment.newInstance("","");

        getSupportFragmentManager().beginTransaction()
        .replace(R.id.fragment, userDataInputFragment, "userDataInputFragment")
        .commit();
        //here fragment in R.id.fragment is the container of Fragments





        share|improve this answer














        This is a case, where you should use Fragments instead Activities.



        1.Replace three Activities with fragments.



        2.Use FragmentManager to switch between fragments.



        UserDataInputFragment userDataInputFragment = UserDataInputFragment.newInstance("","");

        getSupportFragmentManager().beginTransaction()
        .replace(R.id.fragment, userDataInputFragment, "userDataInputFragment")
        .commit();
        //here fragment in R.id.fragment is the container of Fragments






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 10:43

























        answered Nov 11 at 9:59









        Touhidul Islam

        400111




        400111



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247290%2fhow-to-use-onclick-for-multiple-imageviews-for-starting-other-activities%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3