0

我是 Android 的新手,这是我的主要活动。我在每个项目中都遇到了这个错误:

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class MainActivity extends Activity {

        Button btnone,btntwo,btnthree,btnfour,btnfive,btnsix,btnseven,btneight,btnnine,btnzeo,btnstar,btnhash,btndial,btnbackspace;
        EditText etnum;

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

            btnone           = (Button)findViewById(R.id.btnone);
            btntwo           = (Button)findViewById(R.id.btntwo);
            btnthree         = (Button)findViewById(R.id.btnthree);
            btnfour          = (Button)findViewById(R.id.btfour);
            btnfive          = (Button)findViewById(R.id.btnfive);
            btnsix           = (Button)findViewById(R.id.btnsix);
            btnseven         = (Button)findViewById(R.id.btnseven);
            btneight         = (Button)findViewById(R.id.btneight);
            btnnine          = (Button)findViewById(R.id.btnnine);
            btnzeo           = (Button)findViewById(R.id.btnzero);
            btnstar          = (Button)findViewById(R.id.btnstar);
            btnhash          = (Button)findViewById(R.id.btnhash);
            btndial          = (Button)findViewById(R.id.btncall);
            btnbackspace     = (Button)findViewById(R.id.btnbacspace);
            etnum            = (EditText) findViewById(R.id.etnumbr);


            btnone.setOnClickListener((OnClickListener)this);
            btntwo.setOnClickListener((OnClickListener)this);
            btnthree.setOnClickListener((OnClickListener)this);
            btnfour.setOnClickListener((OnClickListener)this);
            btnfive.setOnClickListener((OnClickListener)this);
            btnsix.setOnClickListener((OnClickListener)this);
            btnseven.setOnClickListener((OnClickListener)this);
            btneight.setOnClickListener((OnClickListener)this);
            btnnine.setOnClickListener((OnClickListener)this);
            btnzeo.setOnClickListener((OnClickListener)this);
            btnstar.setOnClickListener((OnClickListener)this);
            btnhash.setOnClickListener((OnClickListener)this);
            btndial.setOnClickListener((OnClickListener)this);
            btnbackspace.setOnClickListener((OnClickListener)this);
            etnum.setOnClickListener((OnClickListener)this);
        }

        @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;
        }
    }

这是我的 logcat 日志,我几乎在每个项目中都会遇到这个错误:

   05-01 23:18:40.364: E/AndroidRuntime(5668): FATAL EXCEPTION: main
05-01 23:18:40.364: E/AndroidRuntime(5668): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.casbwp.mydialerapp/com.casbwp.mydialerapp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.casbwp.mydialerapp.MainActivity" on path: /data/app/com.casbwp.mydialerapp-1.apk
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.os.Looper.loop(Looper.java:137)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at java.lang.reflect.Method.invoke(Method.java:511)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at dalvik.system.NativeStart.main(Native Method)
05-01 23:18:40.364: E/AndroidRuntime(5668): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.casbwp.mydialerapp.MainActivity" on path: /data/app/com.casbwp.mydialerapp-1.apk
05-01 23:18:40.364: E/AndroidRuntime(5668):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-01 23:18:40.364: E/AndroidRuntime(5668):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-01 23:18:40.364: E/AndroidRuntime(5668):     ... 11 more

这是我的 xml 文件:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="30dp"
    android:minWidth="50dp"
    android:nextFocusDown="@string/btn0"
    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=".MainActivity" >


        <TableRow
            android:id="@+id/tblrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <EditText
                    android:id="@+id/etnumbr"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="number" >

                    <requestFocus />
                </EditText>
            </LinearLayout>
        </TableRow>

        <android.support.v7.widget.Space
            android:id="@+id/space1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TableRow
            android:id="@+id/tblrow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/btnone"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn1" />

                <Button
                    android:id="@+id/btntwo"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn2" />

                <Button
                    android:id="@+id/btnthree"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn3" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/btfour"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn4" />

                <Button
                    android:id="@+id/btnfive"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn5" />

                <Button
                    android:id="@+id/btnsix"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn6" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/btnseven"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn7" />

                <Button
                    android:id="@+id/btneight"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn8" />

                <Button
                    android:id="@+id/btnnine"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn9" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/btnstar"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btnstar" />

                <Button
                    android:id="@+id/btnzero"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btn0" />

                <Button
                    android:id="@+id/btnhash"
                    android:layout_width="95dp"
                    android:layout_height="80dp"
                    android:text="@string/btnhash" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/btncall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.44"
                    android:background="@drawable/ic_action_call"
                    android:text="@string/btnDial" />

                <Button
                    android:id="@+id/btnbacspace"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.45"
                    android:background="@drawable/ic_action_delete"
                    android:text="@string/btnbackspace" />
            </LinearLayout>
        </TableRow>

</TableLayout>
4

2 回答 2

1
05-01 23:18:40.364: E/AndroidRuntime(5668): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.casbwp.mydialerapp/com.casbwp.mydialerapp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.casbwp.mydialerapp.MainActivity" on path: /data/app/com.casbwp.mydialerapp-1.apk

你必须在你的 Android Manifest 中声明你的 Activity。

于 2013-05-02T19:27:40.090 回答
0

在您的类中使用适当的包名称以避免此类错误:

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    package com.casbwp.mydialerapp;

    public class MainActivity extends Activity {
    ........
    ........
    }
于 2013-05-02T19:34:30.343 回答