Android按钮不工作ErrorLog

时间:2014-10-24 21:24:59

标签: android

我正在开发一款应用。但是当我添加一个按钮并尝试启动它时,它会崩溃。

错误2

  10-24 22:08:25.930: E/AndroidRuntime(600): FATAL EXCEPTION: main
10-24 22:08:25.930: E/AndroidRuntime(600): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.exaample.werkajb.Class2 }
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.app.Activity.startActivityForResult(Activity.java:3351)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.app.Activity.startActivityForResult(Activity.java:3312)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.app.Activity.startActivity(Activity.java:3522)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.app.Activity.startActivity(Activity.java:3490)
10-24 22:08:25.930: E/AndroidRuntime(600):  at com.exaample.werkajb.MainActivity$1.button1Click(MainActivity.java:27)
10-24 22:08:25.930: E/AndroidRuntime(600):  at com.exaample.werkajb.MainActivity$1.onClick(MainActivity.java:35)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.view.View.performClick(View.java:4084)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.view.View$PerformClick.run(View.java:16966)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.os.Handler.handleCallback(Handler.java:615)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.os.Looper.loop(Looper.java:137)
10-24 22:08:25.930: E/AndroidRuntime(600):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 22:08:25.930: E/AndroidRuntime(600):  at java.lang.reflect.Method.invokeNative(Native Method)
10-24 22:08:25.930: E/AndroidRuntime(600):  at java.lang.reflect.Method.invoke(Method.java:511)
10-24 22:08:25.930: E/AndroidRuntime(600):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 22:08:25.930: E/AndroidRuntime(600):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 22:08:25.930: E/AndroidRuntime(600):  at dalvik.system.NativeStart.main(Native Method)

Class.2java

    package com.exaample.werkajb;

import android.app.Activity;
import android.os.Bundle;

public class Class2 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    }


}

ERROR:

10-24 06:00:53.188: E/AndroidRuntime(620): FATAL EXCEPTION: main
10-24 06:00:53.188: E/AndroidRuntime(620): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=cp3.tutorials.Class2 }
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.app.Activity.startActivityForResult(Activity.java:3351)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.app.Activity.startActivityForResult(Activity.java:3312)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.app.Activity.startActivity(Activity.java:3522)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.app.Activity.startActivity(Activity.java:3490)
10-24 06:00:53.188: E/AndroidRuntime(620):  at com.exaample.werkajb.MainActivity$1.button1Click(MainActivity.java:27)
10-24 06:00:53.188: E/AndroidRuntime(620):  at com.exaample.werkajb.MainActivity$1.onClick(MainActivity.java:35)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.view.View.performClick(View.java:4084)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.view.View$PerformClick.run(View.java:16966)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.os.Handler.handleCallback(Handler.java:615)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.os.Looper.loop(Looper.java:137)
10-24 06:00:53.188: E/AndroidRuntime(620):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 06:00:53.188: E/AndroidRuntime(620):  at java.lang.reflect.Method.invokeNative(Native Method)
10-24 06:00:53.188: E/AndroidRuntime(620):  at java.lang.reflect.Method.invoke(Method.java:511)
10-24 06:00:53.188: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 06:00:53.188: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 06:00:53.188: E/AndroidRuntime(620):  at dalvik.system.NativeStart.main(Native Method)

MainActivity java代码:

package com.exaample.werkajb;

    import android.support.v7.app.ActionBarActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends ActionBarActivity
    {

        Button button1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(new View.OnClickListener() {


                private void button1Click()
                {
                    startActivity(new Intent("cp3.tutorials.Class2"));
                }

                @Override
                public void onClick(View v) {
                    switch (v.getId())
                    {
                    case R.id.button1:
                        button1Click();
                        break;

                    }

                }
            });


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

Class2.java代码:

    package com.exaample.werkajb;

public class Class2 {

}

布局活动主xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.exaample.werkajb.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open New Activity" />

</LinearLayout>

清单xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exaample.werkajb"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="Class2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

</manifest>

我如何让它工作,以便打开我制作的活动?

3 个答案:

答案 0 :(得分:0)

似乎你复制了代码但没有改变一切。当您的包裹为cp3.tutorials时,它仍会使用旧包com.exaample.werkajb

你可以改变它或做一些更自然和多样化的事情:

startActivity(new Intent(this, Class2.class));

编辑:在内部使用时,例如OnClickListener中的MainActivity使用此内容:

startActivity(new Intent(MainActivity.this, Class2.class));

答案 1 :(得分:0)

由于您的LogCat显示此错误:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=cp3.tutorials.Class2 }

我认为你定义了一个错误的包:

 startActivity(new Intent("cp3.tutorials.Class2"));

如果您的班级Class2位于套餐内:com.exaample.werkajb,请将您的意图更改为:

 startActivity(new Intent("com.exaample.werkajb.Class2"));

更新:根据您的AndroidManifest.xml,问题是,Class2所在的软件包必须是:

 package="com.exaample.werkajb"

请记住,活动Class2必须从Activity

延伸
package com.exaample.werkajb;

public class Class2 extends Activity {

}

AndroidManifest.xml定义中添加一个点,您不需要另一个类别为MAIN的意图过滤器,您可以添加更多活动:

  <activity
        android:name=".Class2"
        android:label="@string/app_name" >
   </activity>
  <activity
        android:name=".Class3"
        android:label="MyClass3" >
    </activity>
  <activity
        android:name=".Class4"
        android:label="MyClass4" >
    </activity>

详细了解: Intents and intent Filters

答案 2 :(得分:0)

在清单活动声明中在Class2之前加一个点(。)。在您对评论和答案进行了所有更改之后,这将对您有所帮助。

<activity
        android:name=".Class2"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

似乎您错误地使用了Intent构造函数。尝试使用此

startActivity(new Intent(MainActivity.this, Class2.class));