gpt4 book ai didi

java - 用按钮错误链接两个 Java Activity ! "Android Studio"

转载 作者:行者123 更新时间:2023-12-01 11:50:12 25 4
gpt4 key购买 nike

嗨,我对 Java 很陌生,您能告诉我哪里出了问题吗?我将不胜感激,非常感谢!我想在按下按钮时链接两个 java Activity

<小时/>
package com.example.matt.androidui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;


public class MainActivity extends Activity {

在 Activity 顶部调用。

我遇到问题的代码似乎出了问题!

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener) {

public void onClick (View v){

Intent intent = new Intent (v.getContext(), MainActivity2Activity.class);
startActivityForResult(kntent, 0);
}
}
}

按钮代码到此结束!

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

// noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
<小时/>

最佳答案

如果您想通过按下按钮开始第二个 Activity ,则只需执行此操作即可,

Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);

关于java - 用按钮错误链接两个 Java Activity ! "Android Studio",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28858907/

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