Для файла JAVA:
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);
}});
}
}
По сути, у меня 3 иконки (imageViews) внизу экрана. Моя цель: переходить на разные экраны, когда я нажимаю на разные изображения в меню. Тем не менее, мой код Java не работает правильно. Хотя он работает, он падает после того, как я пытаюсь использовать Intents 2 и 3 подряд. Есть ли лучший способ сделать это?
Соответствующий вывод ошибки LOGCAT:
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)
Редактировать # 2 - XML-файл:
<?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>
1012 *