简体   繁体   中英

Android: No Activity found to handle Intent (trying to add an activity to exsisting app)

Ok so I am very new to the Android programming, I am starting week 2 of this class and cannot for the life of me figure out what is going wrong. I have read/watched tons of tutorials on adding new Activities and nothing works.

Assignment: Use the Activities app and add a fourth activity

My activity is simple, 3 buttons and an image. One button makes the image visible and the other makes it invisible. Third returns back to the main.

Note: I edited the original app to have buttons on Main Activity because it had me hitting center on the d-pad which I found dumb. Another note is that Activity 2 & 3 use the same layout and do basically the same thing from what I can tell

public class MainActivity extends Activity {
String tag = "Events";

int request_Code = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //---hides the title bar---
    //requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);
    Log.d(tag, "In the onCreate() event");

    Button act2Butt = (Button) findViewById(R.id.act2Butt);
    Button act3Butt = (Button) findViewById(R.id.act3Butt);
    Button act4Butt = (Button) findViewById(R.id.act4Butt);

    act2Butt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
        }
    });

    act3Butt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
        }
    });

    act4Butt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            startActivityForResult(new Intent("net.learn2develop.MYACTIVITY"), request_Code);
        }
    });
}
/*
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
    {
        //startActivity(new Intent("net.learn2develop.ACTIVITY2"));    
        //startActivity(new Intent(this, Activity2.class));

        startActivityForResult(new Intent(
            "net.learn2develop.ACTIVITY2"), 
            request_Code);

    }
    return false;
}
*/

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == request_Code) {
        if (resultCode == RESULT_OK) {                
            Toast.makeText(this,data.getData().toString(), 
                Toast.LENGTH_LONG).show();
        }            
    }
} 

public void onStart()
{
    super.onStart();
    Log.d(tag, "In the onStart() event");
}    
public void onRestart()
{
    super.onRestart();
    Log.d(tag, "In the onRestart() event");
}    
public void onResume()
{
    super.onResume();
    Log.d(tag, "In the onResume() event");
}
public void onPause()
{
    super.onPause();
    Log.d(tag, "In the onPause() event");
}    
public void onStop()
{
    super.onStop();
    Log.d(tag, "In the onStop() event");
}    
public void onDestroy()
{
    super.onDestroy();
    Log.d(tag, "In the onDestroy() event");
}    

public class MyActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity4);

    Button yesButt = (Button) findViewById(R.id.yesButton);
    Button noButt = (Button) findViewById(R.id.noButton);
    Button finButt = (Button) findViewById(R.id.finButton);
    final ImageView img1 = (ImageView) findViewById(R.id.image1);


    yesButt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            img1.setVisibility(View.VISIBLE);
        }
    });
    noButt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            img1.setVisibility(View.INVISIBLE);
        }
    });
    finButt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent data = new Intent();
            data.setData(Uri.parse("OMG IT WORKS"));
            setResult(RESULT_OK, data);

            finish();
        }
    });
}

public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);

    String defaultName=""; 
    Bundle extras = getIntent().getExtras();
    if (extras!=null)
    {
        defaultName = extras.getString("Name");        
    }
    //---get the EditText view--- 
    EditText txt_username = 
        (EditText) findViewById(R.id.txt_username);        
    txt_username.setHint(defaultName);

    //---get the OK button---
    Button btn = (Button) findViewById(R.id.btn_OK);

    //---event handler for the OK button---
    btn.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) {
            Intent data = new Intent();

            //---get the EditText view--- 
            EditText txt_username = 
                (EditText) findViewById(R.id.txt_username);

            //---set the data to pass back---
            data.setData(Uri.parse(
                txt_username.getText().toString()));                           
            setResult(RESULT_OK, data);

            //---closes the activity---
            finish(); 
        }
    });  
}

I have entered the code for Main Activity, My Activity (the one I made), and Activity 2. My Activity runs great and does exactly what I want it to but if I try to access it from main it dies.

928-928/net.learn2develop.Activities E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: net.learn2develop.Activities, PID: 928 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=net.learn2develop.MYACTIVITY } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485) at android.app.Activity.startActivityForResult(Activity.java:3736) at android.app.Activity.startActivityForResult(Activity.java:3697) at net.learn2develop.Activities.MainActivity$3.onClick(MainActivity.java:48) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflec t.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

This code is for my last attempt and making it work before throwing my hands up. Last thing I did was make My Activity act like the other and use startActivityForResult.

Any help would help greatly. I don't know if it matters or not but I do not have a .class for My Activity in the bin directory but there is one for all the others.

If you need any more info please just ask.

Like I said before I'm really new to the whole Android area.

Edit: Manifest

<activity android:name=".MyActivity"
              android:label="My Activity">
        <intent-filter>
            <action android:name="net.learn2develop.MYACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
     </activity>

you need to add your activity to your manifest

<activity 
    android:name=".MySecondActivity" 
    android:label="@string/title_second_activity">
</activity>

You may need to add the Activity to the manifest. If you are sure you have done this, I would recommend using an Intent slightly differently.

Try using an Intent the following way:

Intent intent = new Intent(MyActivity.this, SecondActivity.class);
startActivityForResult(intent, requestCode);

The first parameter of this Intent is the current Activity . The second parameter is the Activity you wish to navigate to. Handling an Intent this way prevents a small typo in the package name which would throw that exception. As long as the Activity you are trying to navigate to is in the manifest and you have set up your Intent like the code above, everything should work fine.

Good luck and happy coding!

net.learn2develop.MYACTIVITY

Android并未像其他工程师所说的那样完成上述活动,而是将此活动添加到清单文件中,以便JVM可以找到您要引用的类。

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