简体   繁体   English

第二项活动不会启动

[英]Second activity won't launch

I'm doing the Android Fundamentals 2.2 Coding Challenge and I'm unable to get the second activity to launch using the logic described in the preceding lessons. 我正在执行Android基础知识2.2编码挑战,并且无法使用上一课中描述的逻辑来启动第二个活动。

Here is the code for my first activity: 这是我第一次活动的代码:

package com.homing.a22codingchallenge;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    public static final String EXTRA_MESSAGE = "com.homing.mainactivity.extra.message";
    public static final int TEXT_REQUEST = 1;
    private TextView TV1, TV2, TV3, TV4, TV5, TV6, TV7, TV8, TV9, TV10;

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

        TV1 = findViewById(R.id.TV1);
        TV2 = findViewById(R.id.TV2);
        TV3 = findViewById(R.id.TV3);
        TV4 = findViewById(R.id.TV4);
        TV5 = findViewById(R.id.TV5);
        TV6 = findViewById(R.id.TV6);
        TV7 = findViewById(R.id.TV7);
        TV8 = findViewById(R.id.TV8);
        TV9 = findViewById(R.id.TV9);
        TV10 = findViewById(R.id.TV10);


    }

    public void addItems(View view) {
        Log.d(LOG_TAG, "Button clicked");
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra(EXTRA_MESSAGE, "What?");
        startActivityForResult(intent, TEXT_REQUEST);
        Log.d(LOG_TAG, "startActivityForResult()");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TEXT_REQUEST) {
            if (resultCode == RESULT_OK) {
                String reply = data.getStringExtra(SecondActivity.EXTRA_RETURN);
                fillList(reply);
            }
        }
    }

    public void fillList(String string) {
        String[] list = { TV1.toString(), TV2.toString(), TV3.toString(), TV4.toString(), TV5.toString(), TV6.toString(), TV7.toString(), TV8.toString(), TV9.toString(), TV10.toString() };
        for (int i = 0; i < list.length; i++) {
            list[i] = string;
        }
    }
}

Here is the code to my second activity: 这是我第二项活动的代码:

package com.homing.a22codingchallenge;

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

public class SecondActivity extends AppCompatActivity {

    private Button BTN1, BTN2, BTN3, BTN4, BTN5, BTN6, BTN7, BTN8, BTN9, BTN10;
    public static final String EXTRA_RETURN = "com.homing.22codingchallenge.secondactivity.extra.return";

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

        BTN1 = findViewById(R.id.BTN1);
        BTN2 = findViewById(R.id.BTN2);
        BTN3 = findViewById(R.id.BTN3);
        BTN4 = findViewById(R.id.BTN4);
        BTN5 = findViewById(R.id.BTN5);
        BTN6 = findViewById(R.id.BTN6);
        BTN7 = findViewById(R.id.BTN7);
        BTN8 = findViewById(R.id.BTN8);
        BTN9 = findViewById(R.id.BTN9);
        BTN10 = findViewById(R.id.BTN10);

        Intent returnIntent = new Intent();
        returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
        setResult(RESULT_OK, returnIntent);
        finish();

    }
}

I've tried comparing the code with the project I was walked through in the guide and everything is consistent so far as I can see. 我已经尝试将代码与本指南中介绍的项目进行比较,据我所知,所有内容都是一致的。 Posts with issues similar to mine made a few suggestions that didn't make sense since my first project worked fine. 自从我的第一个项目运行良好以来,与我的问题相似的帖子提出了一些毫无意义的建议。

I've let up debug logs and confirmed in Logcat that the button is registering the click and it's even running through the block through the startActivityForResult() method. 我放弃了调试日志,并在Logcat中确认该按钮正在注册该点击,并且甚至通过startActivityForResult()方法在该块中运行。

There was one Logcat entry that seemed relevant, but searched didn't really yield anything helpful to me: 有一个Logcat条目似乎很相关,但是搜索并没有产生任何对我有帮助的信息:

2018-10-18 07:01:37.386 1624-1677/system_process W/ActivityManager: Unable to start service Intent { act=com.google.android.gms.drive.ApiService.RESET_AFTER_BOOT flg=0x4 cmp=com.google.android.gms/.drive.api.ApiService (has extras) } U=0: not found 2018-10-18 07:01:37.386 1624-1677 / system_process W / ActivityManager:无法启动服务意图{act = com.google.android.gms.drive.ApiService.RESET_AFTER_BOOT flg = 0x4 cmp = com.google.android .gms / .drive.api.ApiService(有其他功能)} U = 0:找不到

I've since tried to reproduce this error a number of times, but have not been able to. 从那以后,我尝试过多次重现此错误,但一直未能做到。 The only entries I'm seeing across my attempts are along the lines of the following: 我在尝试中看到的唯一条目是以下内容:

2018-10-18 07:00:44.979 1369-1401/? 2018-10-18 07:00:44.979 1369-1401 /? W/audio_hw_generic: Not supplying enough data to HAL, expected position 754681 , only wrote 603360 W / audio_hw_generic:没有向HAL提供足够的数据,预期位置754681,只写了603360

But I'm not sure that this is really related to the issue of launching the second activity. 但是我不确定这是否与启动第二项活动有关。

Edit: 编辑:

In response to some comments here is my manifest. 在回应一些评论时,这是我的清单。

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="22CodingChallenge"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/SecondActivity_name"
            android:parentActivityName=".MainActivity">
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value="com.homing.a22codingchallenge.MainActivity" />
        </activity>
    </application>

If the second Activity is not added to the AndroidManifest.xml , IDE will complaining that it is not added. 如果第二个Activity没有添加到AndroidManifest.xml ,IDE将抱怨它没有添加。

However, if you get: Button clicked in the logs, so main Activity has no problem but, check the codes in the second Activity : 但是,如果您得到:在日志中Button clickedButton clicked ,那么主Activity没有问题,但是,请检查第二个Activity的代码:

Intent returnIntent = new Intent();
        returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
        setResult(RESULT_OK, returnIntent);
        finish();

As soon as it do the putExtra() , it actually finishes the Activity : finish(); 只要它做putExtra()它实际上完成的Activityfinish(); after. 后。

For what I see, I suppose you are just starting the SecondActivity in an onClick attribute in the activity_main layout, and, once in the SecondActivity, as soon as the onCreate happens, you are just calling finish() here: 对于我所看到的,我想您只是在activity_main布局的onClick属性中启动SecondActivity,并且一旦在SecondActivity中,一旦onCreate发生,您就在这里调用finish()

Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();

So as soon as the activity is created, it is finished... 因此,一旦创建了活动,便完成了...

Maybe what you are trying to do is to return the button clicked text in the SecondActivity? 也许您想要做的是在SecondActivity中返回按钮单击的文本?

Something like: 就像是:

    BTN1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent returnIntent = new Intent();
            returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
            setResult(RESULT_OK, returnIntent);
            finish();
        }
    });

Maybe you should add a onClick attribute to each button, and add a call to a method that will obtain the text of the button clicked like: 也许您应该为每个按钮添加onClick属性,并添加对方法的调用,该方法将获取被单击的按钮的文本,例如:

public void clickButton(View view) {
    Intent returnIntent = new Intent();
    returnIntent.putExtra(EXTRA_RETURN, ((Button) view).getText().toString());
    setResult(RESULT_OK, returnIntent);
    finish();

I haven't tried the code, but I hope this helps you! 我没有尝试过该代码,但希望对您有所帮助!

If your onActivityResult is reached that means the SecondActivity has been started. 如果达到了onActivityResult则意味着SecondActivity已启动。 You have just to check what to do inside your second activity before finishing it. 您只需在完成第二项活动之前检查该做什么。 For example with the code above, you are calling finishing the activity at its creation. 例如,使用上面的代码,您要在创建活动时调用完成活动。

Your code works, you have just to find the right place to place this code: 您的代码有效,您只需找到合适的位置放置此代码即可:

Intent returnIntent = new Intent();
        returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
        setResult(RESULT_OK, returnIntent);
        finish();

According to your logic. 根据您的逻辑。

I think this will help. 我认为这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM