Android中的Onclick功能

时间:2013-09-28 06:48:41

标签: java android

我正在尝试通过一个标签活动

启动新的tabactivity

我可以将内容加载到第一个活动......但照片按钮的onclick如上图所示,其中一个类正在显示错误,如日志猫所示。 ..

如何解决此问题

任何想法

AndroidTabRestaurantDescListView.java

public class AndroidTabRestaurantDescListView extends TabActivity {

    // TabSpec Names
        private static final String INBOX_SPEC = "Rating";
        private static final String OUTBOX_SPEC = "Price";
        private static final String PROFILE_SPEC = "Distance";

        Button Photos;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Photos=(Button) findViewById(R.id.PhotoButton); 

            TabHost tabHost = getTabHost();

            // Inbox Tab
            TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
            Intent inboxIntent = new Intent(this, MainActivity.class);
            inboxSpec.setIndicator(INBOX_SPEC);
            // Tab Content
            inboxSpec.setContent(inboxIntent);

            // Outbox Tab
            TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
            Intent PriceIntent = new Intent(this, PriceDescriptionActivity.class);
            PriceSpec .setIndicator(OUTBOX_SPEC);
            PriceSpec.setContent(PriceIntent);

            // Profile Tab
            TabSpec DistanceSpec = tabHost.newTabSpec(PROFILE_SPEC);
            Intent DistanceIntent = new Intent(this, DistanceDiscriptionActivity.class);
            DistanceSpec .setIndicator(PROFILE_SPEC); 
            DistanceSpec.setContent(DistanceIntent);

            // Adding all TabSpec to TabHost
            tabHost.addTab(inboxSpec); 
            tabHost.addTab(PriceSpec); 
            tabHost.addTab(DistanceSpec); 

            //Set the current value tab to default first tab
            tabHost.setCurrentTab(0);

            //Setting custom height for the tabs
            final int height = 45;
            tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
            tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
            tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;




            Photos.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Intent PhotoIntent=new Intent(AndroidTabRestaurantDescListView.this,AndroidTabRestaurantDescImageListView.class);
                    startActivity(PhotoIntent);

                }
            });

        }

}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.jsonparsetutorial"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.project.findmybuffet.AndroidTabRestaurantDescListView" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.project.findmybuffet.SingleItemView" >
        </activity>


        <activity
            android:name="com.project.findmybuffet.RestaurantDesc"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.project.findmybuffet.RestaurantPhotos"
            android:label="@string/app_name" >
        </activity>


        <activity
            android:name="com.project.findmybuffet.DistanceDiscriptionActivity">
        </activity>    

        <activity
            android:name="com.project.findmybuffet.PriceDescriptionActivity">
        </activity> 

        <activity
            android:name="com.project.findmybuffet.MainActivity">
        </activity> 

        <activity
            android:name="com.project.findmybuffet.AndroidTabRestaurantDescImageListView">
        </activity> 

        <activity
            android:name="com.project.findmybuffet.DistanceDiscriptionOnlyImageActivity">
        </activity> 

        <activity
            android:name="com.project.findmybuffet.PriceDescriptionOnlyImageActivity">
        </activity> 

    </application>

</manifest>

日志::

09-28 12:12:40.415: I/MemoryCache(1222): MemoryCache will use up to 8.0MB
09-28 12:12:40.875: D/dalvikvm(1222): GC_EXTERNAL_ALLOC freed 313K, 50% free 2967K/5895K, external 2130K/2137K, paused 119ms
09-28 12:12:40.925: I/MemoryCache(1222): cache size=47000 length=1
09-28 12:12:41.105: I/MemoryCache(1222): cache size=111500 length=2
09-28 12:12:41.245: I/MemoryCache(1222): cache size=179940 length=3
09-28 12:12:41.285: I/MemoryCache(1222): cache size=237060 length=4
09-28 12:12:48.255: D/AndroidRuntime(1222): Shutting down VM
09-28 12:12:48.255: W/dalvikvm(1222): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-28 12:12:48.287: E/AndroidRuntime(1222): FATAL EXCEPTION: main
09-28 12:12:48.287: E/AndroidRuntime(1222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.jsonparsetutorial/com.project.findmybuffet.AndroidTabRestaurantDescImageListView}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.androidbegin.jsonparsetutorial/com.project.findmybuffet.RatingDescriptionOnlyImageActivity}; have you declared this activity in your AndroidManifest.xml?
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.os.Looper.loop(Looper.java:123)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at java.lang.reflect.Method.invokeNative(Native Method)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at java.lang.reflect.Method.invoke(Method.java:507)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at dalvik.system.NativeStart.main(Native Method)
09-28 12:12:48.287: E/AndroidRuntime(1222): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.androidbegin.jsonparsetutorial/com.project.findmybuffet.RatingDescriptionOnlyImageActivity}; have you declared this activity in your AndroidManifest.xml?
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:1457)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.widget.TabHost.addTab(TabHost.java:216)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at com.project.findmybuffet.AndroidTabRestaurantDescImageListView.onCreate(AndroidTabRestaurantDescImageListView.java:44)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-28 12:12:48.287: E/AndroidRuntime(1222):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-28 12:12:48.287: E/AndroidRuntime(1222):     ... 11 more

2 个答案:

答案 0 :(得分:1)

错误消息非常具有描述性:

  

无法找到显式活动类{com.androidbegin.jsonparsetutorial / com.project.findmybuffet.RatingDescriptionOnlyImageActivity};你有没有在AndroidManifest.xml中声明这个活动?

答案是:不,你没有在清单中宣布这项活动。你的清单中有很多其他活动,但不是那个。

除了通过将活动添加到您的清单来简单地修复此问题,我建议您退一步思考如何为自己解决这个问题。你仔细阅读了日志文件吗?你有没有错过我引用的错误信息? (总是寻找“原因”异常消息。)学习如何为自己诊断事物非常重要 - 所以看看你是否可以解决为什么你这次不能到达那里,以便下次可以改进。

答案 1 :(得分:0)

发生此错误是因为您尚未在清单中声明此活动的RatingDescriptionOnlyImageActivity。您已宣布所有其他活动,但未宣布此活动。因此,请查看清单文件并正确检查软件包名称,然后将此活动写入清单文件。你的问题肯定会得到解决。