简体   繁体   中英

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.

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.

There was one Logcat entry that seemed relevant, but searched didn't really yield anything helpful to me:

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

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/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 754681 , only wrote 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.

However, if you get: Button clicked in the logs, so main Activity has no problem but, check the codes in the second 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(); 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:

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?

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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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