Android Studio 在Onclick开始活动后..出现白色屏幕或应用程序转到开始活动而不是下一个或其他崩溃

kr98yfug  于 2023-06-24  发布在  Android
关注(0)|答案(1)|浏览(118)

这个主活动FindDoctorDetails cardview应该将我带到下一个活动DoctorDetailsActivity

package com.example.healthcare;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class FindDoctorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_doctor);

      CardView exit = findViewById(R.id.cardFDBack);
      exit.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startActivity(new Intent(FindDoctorActivity.this,HomeActivity.class));
          }
      });

      CardView familyphysician = findViewById(R.id.cardFDFamilyPhysician);
      familyphysician.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent it = new Intent(FindDoctorActivity.this,DoctorDetailsActivity.class);
              it.putExtra("title", "Family Physicians");
              startActivity(it);
          }
      });

        CardView dietician = findViewById(R.id.cardFDDietician);
        dietician.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(FindDoctorActivity.this,DoctorDetailsActivity.class);
                it.putExtra("title", "Dietician");
                startActivity(it);
            }
        });

        CardView surgeon = findViewById(R.id.cardFDSurgeon);
        familyphysician.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(FindDoctorActivity.this,DoctorDetailsActivity.class);
                it.putExtra("title", "Surgeon");
                startActivity(it);
            }
        });

        CardView cardiologist = findViewById(R.id.cardFDCardiologist);
        cardiologist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(FindDoctorActivity.this,DoctorDetailsActivity.class);
                it.putExtra("title", "Cardiologist");
                startActivity(it);
            }
        });

        CardView dentist = findViewById(R.id.cardFDDentist);
        dentist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(FindDoctorActivity.this,DoctorDetailsActivity.class);
                it.putExtra("title", "Denstist");
                startActivity(it);
            }
        });


    }
}

点击FindDoctorActivity后,我应该转到此DoctorDetailsActivity,但相反,我重新加载回FindDoctorActivity或只是得到一个白色屏幕和应用程序崩溃,我使用的是API 33和Android Studio最新的火烈鸟

package com.example.healthcare;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class DoctorDetailsActivity extends AppCompatActivity {
    private String[][] doctor_details1 =
            {
                    {"Doctor Name : Ajit Saste", "Hospital Address : pimpri","Mobile No. : 1234567890","500"},
                    {"Doctor Name : Prasad Pawar", "Hospital Address : Nigdi","Mobile No. : 1234567809","600"},
                    {"Doctor Name : Swapnil Kale", "Hospital Address : Pune","Mobile No. : 1234567089","700"},
                    {"Doctor Name : Deepak Deshmukh", "Hospital Address : Chinchwad","Mobile No. : 1234560789","800"},
                    {"Doctor Name : Ashok Panda", "Hospital Address : Katraj","Mobile No. : 1234567890","900"}

            };
    private String[][] doctor_details2 =
            {
                    {"Doctor Name : Ankur Kar", "Hospital Address : pimpri","Mobile No. : 1234567890","500"},
                    {"Doctor Name : Anusha Gokhale", "Hospital Address : Nigdi","Mobile No. : 1234567809","600"},
                    {"Doctor Name : Dipa Rout", "Hospital Address : Pune","Mobile No. : 1234567089","700"},
                    {"Doctor Name : Aalok Krish", "Hospital Address : Chinchwad","Mobile No. : 1234560789","800"},
                    {"Doctor Name : Abhiram Kothari", "Hospital Address : Katraj","Mobile No. : 1234567890","900"}

            };
    private String[][] doctor_details3 =
            {
                    {"Doctor Name : Madhur Bhargav", "Hospital Address : pimpri","Mobile No. : 1234567890","500"},
                    {"Doctor Name : Advaith Kapur", "Hospital Address : Nigdi","Mobile No. : 1234567809","600"},
                    {"Doctor Name : Shaurya Nzareth", "Hospital Address : Pune","Mobile No. : 1234567089","700"},
                    {"Doctor Name : Sudarshan Gopal", "Hospital Address : Chinchwad","Mobile No. : 1234560789","800"},
                    {"Doctor Name : Aruna Upadhyay", "Hospital Address : Katraj","Mobile No. : 1234567890","900"}

            };
    private String[][] doctor_details4 =
            {
                    {"Doctor Name : Rajesh Kade", "Hospital Address : pimpri","Mobile No. : 1234567890","500"},
                    {"Doctor Name : Varsha Chaudhari", "Hospital Address : Nigdi","Mobile No. : 1234567809","600"},
                    {"Doctor Name : Anah Dalal", "Hospital Address : Pune","Mobile No. : 1234567089","700"},
                    {"Doctor Name : Gauri Lad", "Hospital Address : Chinchwad","Mobile No. : 1234560789","800"},
                    {"Doctor Name : Nima Jayram", "Hospital Address : Katraj","Mobile No. : 1234567890","900"}

            };
    private String[][] doctor_details5 =
            {
                    {"Doctor Name : Manas Mannan", "Hospital Address : pimpri","Mobile No. : 1234567890","500"},
                    {"Doctor Name : Kavika Ramesh", "Hospital Address : Nigdi","Mobile No. : 1234567809","600"},
                    {"Doctor Name : Minali Saraff", "Hospital Address : Pune","Mobile No. : 1234567089","700"},
                    {"Doctor Name : Rashmi Chander", "Hospital Address : Chinchwad","Mobile No. : 1234560789","800"},
                    {"Doctor Name : Neel Salvi", "Hospital Address : Katraj","Mobile No. : 1234567890","900"}

            };

    TextView tv;
    Button btn;
    String[][] doctor_details = {};
    HashMap<String, String> item;
    ArrayList list;
    SimpleAdapter sa;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_details);

        tv =  findViewById(R.id.textViewDDTitle);
        btn = findViewById(R.id.buttonDDBack);

        Intent it = getIntent();
        String title = it.getStringExtra("title");
        tv.setText(title);

        if(title.compareTo("Family Physicians")==0)
            doctor_details = doctor_details1;
        else
        if(title.compareTo("Dietician")==0)
            doctor_details = doctor_details2;
        else
            if(title.compareTo("Surgeon")==0)
            doctor_details = doctor_details3;
        else
            if(title.compareTo("Dentist")==0)
            doctor_details = doctor_details4;
        else
            doctor_details = doctor_details5;


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(DoctorDetailsActivity.this,FindDoctorActivity.class));
            }
        });

        list = new ArrayList();
        for(int i=0;i<doctor_details.length;i++){
            item = new HashMap<String,String>();
            item.put("line1", doctor_details[i][0]);
            item.put("line2", doctor_details[i][1]);
            item.put("line3", doctor_details[i][2]);
            item.put("line4", doctor_details[i][3]);
            item.put("line5","Cons Fees :"+doctor_details[i][4]+"/-");
            list.add( item );
        }
         sa = new SimpleAdapter(this,list,
                 R.layout.multi_lines,
                 new String[]{"line1","line2","line3","line4","line5"},
                 new int []{R.id.line_a,R.id.line_b,R.id.line_c,R.id.line_d,R.id.line_e});
         ListView lst = findViewById(R.id.listViewDD);
         lst.setAdapter(sa);



    }
}
b4wnujal

b4wnujal1#

请确保DoctorDetailsActivity已添加到您的Manifest.xml文件中。它应该出现在应用部分:

<application
    <activity
        android:name=".DoctorDetailsActivity"
        android:exported="false"
    </activity>
</application>

您的DoctorDetailsActivity活动显然位于项目的主分支中,因此上面的声明是可以的。但是如果您想添加一些子目录中的活动(即在dir1/dir2/DoctorDetailsActivity中),只需如下添加:

<application
    <activity
        android:name=".dir1.dir2.DoctorDetailsActivity"
        android:exported="false"
    </activity>
</application>

我将exported设置为false,因为您显然不希望从应用外部启动此Activity-您假设Bundle是非空的,并且存在一些具有非空值的Title键-您不考虑空字符串。
另一件事是,您错过了为FindDoctorActivity中的surgery变量设置OnClickListener。相反,您覆盖了familyphysician的OnClickListener(可能是复制+粘贴错误)。这样,点击外科医生将没有任何效果,因为没有定义OnClickListener-您将停留在FindDoctorActivity上。如果您尚未将DoctorDetailsActivity添加到您的清单中,尝试单击所有其他医生将使您的应用程序崩溃。

相关问题