如何对多个ImageView使用onClick来启动其他活动

时间:2018-11-11 09:11:46

标签: java android xml android-intent imageview

对于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代码无法正常工作。尽管它可以运行,但是在我尝试连续使用Intent 2和Intent 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>

2 个答案:

答案 0 :(得分:2)

您没有像应该那样使用onGenericMenuClick()
这是所有3个视图的通用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);
}

答案 1 :(得分:1)

在这种情况下,应使用“片段”代替“活动”。

1。用片段替换三个活动。

2。使用FragmentManager在片段之间切换。

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

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