简体   繁体   English

Android:找不到用于处理Intent的活动(尝试向现有应用添加活动)

[英]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. 好的,所以我对Android编程非常陌生,我将在本课程的第二周开始学习,无法终生了解出了什么问题。 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. 注意:我编辑了原始应用程序,使其在“主要活动”上具有按钮,因为它使我在d-pad上击中了中心,发现它很笨。 Another note is that Activity 2 & 3 use the same layout and do basically the same thing from what I can tell 另一个需要注意的是,活动2和活动3使用相同的布局,并且根据我的判断,基本上可以完成相同的操作

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. 我已经输入了“主要活动”,“我的活动”(我创建的活动)和“活动2”的代码。我的活动运行良好,并且完全可以实现我想要的功能,但是如果我尝试从main访问它,它就会死。

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 928-928 / net.learn2develop.Activities E / AndroidRuntime:致命异常:主进程:net.learn2develop.Activities,PID:928 android.content.ActivityNotFoundException:找不到用于处理Intent {act = net.learn2develop.MYACTIVITY}的活动android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)位于android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)位于android.app.Activity.startActivityForResult(Activity.java:3736)位于android.app.Activity net.learn2develop.Activities.MainActivity $ 3.onClick(MainActivity.java:48)的.startActivityForResult(Activity.java:3697)android.view.View.performClick(View.java:4756)的android.view.View $ PerformClick .run(View.java:19749)android.os.Handler.handleCallback(Handler.java:739)android.os.Handler.dispatchMessage(Handler.java:95)android.os.Looper.loop(Looper。 java:135)位于android.app.ActivityThread.main(ActivityThread.java:5221)位于java.lang.reflect.Method.invoke(本地方法)位于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) t.Method.invoke(Method.java:372)位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)位于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. 我所做的最后一件事是使“我的活动”的行为与其他行为相同,并使用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. 我不知道这是否重要,但是我在bin目录中没有用于My Activity的.class,但其他所有目录都有一个。

If you need any more info please just ask. 如果您需要更多信息,请询问。

Like I said before I'm really new to the whole Android area. 就像我之前说过的,我真的是整个Android领域的新手。

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. 您可能需要将Activity添加到清单。 If you are sure you have done this, I would recommend using an Intent slightly differently. 如果您确定已完成此操作,则建议您使用稍有不同的Intent

Try using an Intent the following way: 尝试通过以下方式使用Intent

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

The first parameter of this Intent is the current Activity . 这个Intent的第一个参数是当前的Activity The second parameter is the Activity you wish to navigate to. 第二个参数是您希望导航到的Activity Handling an Intent this way prevents a small typo in the package name which would throw that exception. 以这种方式处理Intent可以防止在软件包名称中出现小错字,而这会引发该异常。 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. 只要您要导航到的“ Activity在清单中,并且您已经像上面的代码一样设置了“ Intent ”,一切都应该可以正常工作。

Good luck and happy coding! 祝您好运,编码愉快!

net.learn2develop.MYACTIVITY

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

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

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