startActivity在android中不起作用

时间:2017-07-28 12:22:10

标签: java android start-activity

我无法找到有效的问题解决方案,所以这里是:

当我播放此代码时,调试器显示已达到startActivity语句,但执行只是通过它。

Java代码:

public class grassesDoFamily extends AppCompatActivity {
    protected String TAG = "grassesDoFamily >>";
    private String  familyName;
    private String  familyText;
    private int     familyNumber;
    private String  familyTable;
    private int     genusNumber;
    private int     speciesNumber;

    Context dContext = grassesDoFamily.this;
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.grasses_layout_start_family);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        Intent myIntent = getIntent();
        familyNumber = myIntent.getIntExtra("familyNumber", 0);
        genusNumber = myIntent.getIntExtra("genusNumber", 0);
        speciesNumber = myIntent.getIntExtra("speciesNumber", 0);
        /**
         *      Get Family data
         */
        GetFamily(familyNumber);
        /**
         *  Special case fam=5 : generic, fam=4 Plantaginaceae
         */
       // Intent intentgrassesShowSpeciesMain = new Intent(dContext, grassesShowSpeciesMain.class);

        Intent intentgrassesShowSpeciesMain = new Intent(dContext, grassesShowSpeciesMain.class);
        /**
         *  Get genus# and species# if not Plantaginaceae
         */
       if (familyNumber==5){

            intentgrassesShowSpeciesMain.putExtra("genusNumber", genusNumber);
            intentgrassesShowSpeciesMain.putExtra("speciesNumber", speciesNumber);

        }
        if ((familyNumber==5) || (familyNumber==4)){


            intentgrassesShowSpeciesMain.putExtra("familyNumber", familyNumber);
            startActivity(intentgrassesShowSpeciesMain);
        }

清单:

<activity
    android:name="com.learningideas.grasses.grassesShowSpeciesMain"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/CustomActionBarTheme"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/doShowSpecies"
    android:screenOrientation="portrait"
    android:parentActivityName="com.learningideas.grasses.grassesActivityMain">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.learningideas.grasses.grassesActivityMain" />
</activity>

非常感谢。

2 个答案:

答案 0 :(得分:0)

我认为问题可能是你正在使用的奇怪的Context成员变量。尝试简化为:

Intent intentgrassesShowSpeciesMain = new Intent(this,grassesShowSpeciesMain.class);

答案 1 :(得分:-1)

您应该将其更改为:

if (familyNumber == 5) {
    intentgrassesShowSpeciesMain.putExtra("genusNumber", genusNumber);
    intentgrassesShowSpeciesMain.putExtra("speciesNumber", speciesNumber);
}
else if (familyNumber == 4) {
    intentgrassesShowSpeciesMain.putExtra("familyNumber", familyNumber);
}
if (familyNumber == 5 || familyNumber == 4) {
    startActivity(intentgrassesShowSpeciesMain);
}