gpt4 book ai didi

java - onOptionsItemSelected(MenuItem item) 打开错误的 Activity

转载 作者:行者123 更新时间:2023-12-01 09:56:57 29 4
gpt4 key购买 nike

我在操作栏上有一个菜单,应该在单击时打开一个特定的 Activity ,我正在调用我想要打开的 Activity (MaininsertActivity.java),但我可以在 View 中看到它的activity_main2.xml

MainActivity.java

    package com.example.bugdroid.trabalhoapp1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.bugdroid.trabalhoapp1.modelo.Series;
import com.example.bugdroid.trabalhoapp1.modelo.Serie;

public class MainActivity extends AppCompatActivity {

private ListView lv;

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

lv = (ListView) findViewById(R.id.lista_series);

Series Series = new Series();

Serie exemplo = new Serie ("Thrones", "açao", 50, 10, 1900 );
Series.getLista().add(exemplo);

exemplo = new Serie("CSI", "Policial", 30, 20, 4);
Series.getLista().add(exemplo);

exemplo = new Serie("Flash", "aventura", 30, 20, 4);
Series.getLista().add(exemplo);

exemplo = new Serie("Vikings", "Historia", 3, 2, 4);
Series.getLista().add(exemplo);

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, Series.getLista());

lv.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if(id == R.id.action_add) {
Intent intent = new Intent(this, MaininsertActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}

MaininsertActivity.java

    package com.example.bugdroid.trabalhoapp1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class MaininsertActivity extends AppCompatActivity {

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

EditText et = (EditText) findViewById(R.id.nomeSerie);
String nomedaserie = et.getText().toString();

Intent i = new Intent(this, Main2Activity.class);
i.putExtra("nomeSerie", nomedaserie);
startActivity(i);

}
}

Main2Activity.java

    package com.example.bugdroid.trabalhoapp1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.example.bugdroid.trabalhoapp1.modelo.Serie;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

String uriString;

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

Intent i = getIntent();
uriString = i.getStringExtra("nomeSerie");
}
}

这是我的项目树:

https://gyazo.com/e3cae13f1b62c2cb1442f377405ca55b

最佳答案

因为在 MaininsertActivity 的 onCreate() 中,您正在启动 Main2Activity。这就是为什么您会看到 Main2Activity 布局

Intent i = new Intent(this, Main2Activity.class);
i.putExtra("nomeSerie", nomedaserie);
startActivity(i);

由于此代码,您将获得Main2Activity屏幕

关于java - onOptionsItemSelected(MenuItem item) 打开错误的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37137990/

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