onOptionsItemSelected(MenuItem item)打开错误的活动

时间:2016-05-10 12:03:03

标签: java android

我在操作栏上有一个菜单,应该在点击时打开一个特定的活动,我调用我要打开的活动(MaininsertActivity.java),但是我可以看到它的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

5 个答案:

答案 0 :(得分:1)

因为在 MaininsertActivity 的onCreate()中,您正在开始 Main2Activity 。这就是您看到 Main2Activity 布局

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

因为此代码您获得 Main2Activity 屏幕

答案 1 :(得分:0)

那是因为您在Main2Activity的onCreate中调用了MaininsertActivity

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

因此,当您点击菜单项时,它会首先启动MaininsertActivity,但在OnCreate中,还有另一个Intent,因此它正在转发到MainA2ctivity

答案 2 :(得分:0)

因为你在MaininsertActivity中调用onCreate到Main2Activity类

答案 3 :(得分:0)

你正在传递onCreate方法的意图,这就是为什么,在insertActivity中删除你的意图。并尝试按下按钮

答案 4 :(得分:0)

在' MaininsertActivity'的onCreate方法中,你正在开始' Main2Activity'用这些线:

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

因此,MainActivity2的布局在屏幕上可见 - R.layout.activity_main2