gpt4 book ai didi

java - 如何将 onClick 用于多个 ImageView 以启动其他 Activity

转载 作者:行者123 更新时间:2023-11-29 04:14:28 25 4
gpt4 key购买 nike

对于 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>

最佳答案

您没有按应有的方式使用 onGenericMenuClick()
这是所有 3 个 View 的通用 onClick(),因此您无需在其中定义新的监听器。
更改为:

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);
}

关于java - 如何将 onClick 用于多个 ImageView 以启动其他 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53247290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com