You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Toolbar中ImageView点击跳转Activity时应用崩溃求助

问题:点击Toolbar中ImageView跳转Activity时应用崩溃

我给Toolbar里的ImageView设置了setOnClickListener,原本要打开user_login Activity,但点击图标时应用直接崩溃,求解决。相关跳转逻辑代码如下:

public class MainActivity extends AppCompatActivity {

    ImageView lang, share, profileButton;
    TextView title;
    private EditText depart, arriver;
    private Button verifyButton;
    BottomNavigationView bottomNavigationView;


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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        title = findViewById(R.id.title1);
        title.setText("EasyBooking");
        lang = findViewById(R.id.lang);
        share = findViewById(R.id.shareButton);

        this.profileButton = (ImageView) findViewById(R.id.profileButton);

        profileButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), user_login.class);
                startActivity(intent);
                finish();
            }
        });


        depart = findViewById(R.id.departurepoint);
        arriver = findViewById(R.id.arrivingpoint);
        verifyButton = findViewById(R.id.search);

        verifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String departure = depart.getText().toString();
                String arrival = arriver.getText().toString();

                // 打开对应页面并传入起止点参数
                openActivity(departure, arrival);
            }
        });

        // 起点输入框自动补全
        String[] Towns = getResources().getStringArray(R.array.TownDeparture);
        AutoCompleteTextView editText = findViewById(R.id.departurepoint);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Towns);
        editText.setAdapter(adapter);

        // 终点输入框自动补全
        String[] Town = getResources().getStringArray(R.array.ArrivingTowns);
        AutoCompleteTextView editTexts = findViewById(R.id.arrivingpoint);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,Towns);
        editTexts.setAdapter(adapter1);

        bottomNavigationView = findViewById(R.id.botton_nav_bar1);
        bottomNavigationView.setSelectedItemId(R.id.Idhome);
        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();

                if (itemId == R.id.Idhome){
                    return true;
                } else if (itemId == R.id.Idagence) {
                    startActivity(new Intent(getApplicationContext(), Agence.class));
                    overridePendingTransition(0, 0);
                    return true;
                } else if (itemId == R.id.Idticket) {
                    startActivity(new Intent(getApplicationContext(), Ticket.class));
                    overridePendingTransition(0, 0);
                    return true;
                }else if (itemId == R.id.Idsetting) {
                    startActivity(new Intent(getApplicationContext(), Setting.class));
                    overridePendingTransition(0, 0);
                    return true;
                }

                return false;
            }
        });

        // 分享按钮点击事件
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "应用分享链接");
                startActivity(Intent.createChooser(intent, "分享至"));
            }
        });


       /* // 语言切换按钮点击事件
        lang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Locale locale = new Locale("en");
                Configuration configuration = new Configuration(getResources().getConfiguration());
                configuration.setLocale(locale);
                getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
                recreate();
            }
        });*/

    }

    private void openActivity(String departure, String arrival) {
        Intent intent = new Intent(this, Douala__Bafoussam.class);
        intent.putExtra("departure", departure);
        intent.putExtra("arrival", arrival);
        startActivity(intent);
    }
}

排查与解决方案

  • 检查控件是否为空:确认activity_main.xml中存在ID为profileButton的ImageView,且该控件属于当前布局的Toolbar。如果ID拼写错误或控件不存在,findViewById会返回null,点击时触发空指针崩溃。
  • 确认Activity已注册:检查AndroidManifest.xml中是否声明了user_login Activity,添加如下代码(如果未添加):
    <activity android:name=".user_login" />
    
  • 替换Context对象:将getApplicationContext()换成MainActivity.this传入Intent,避免Context生命周期不匹配问题:
    Intent intent = new Intent(MainActivity.this, user_login.class);
    
  • 查看崩溃日志:打开Android Studio的Logcat,过滤Error级别日志,找到具体崩溃异常(如NullPointerException、ActivityNotFoundException),根据堆栈信息精准定位问题。

内容的提问来源于stack exchange,提问作者Kenne Dany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 15:43:19