0

由于我还是 Android 新手(3 周前开始),我的 MainActivity 类基于已弃用的 TabActivity 超类(因为我想适应版本 10-12)。在我的 MainActivity 类中,我创建了五个主要意图,其中三个意图是基于 FragmentActivity 的活动(预计会有很多向下钻取)。

这是我的 MainActivity 类代码:

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.mexter.rewards2go.activities.main.FavoritesActivity;
import com.mexter.rewards2go.activities.main.ImageListActivity;
import com.mexter.rewards2go.activities.main.SearchActivity;

public class MainActivity extends TabActivity {



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

        Resources resources = getResources();
        final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        final Intent imageIntent = new Intent().setClass(MainActivity.this, ImageListActivity.class);
        TabSpec tabHotDeals = tabHost.newTabSpec(TAB_IMAGE).setIndicator("", resources.getDrawable(R.drawable.tab_promotion_off_btn)).setContent(imageIntent);

        final Intent searchIntent = new Intent().setClass(MainActivity.this, SearchActivity.class);
        TabSpec tabSearch = tabHost.newTabSpec(TAB_SEARCH).setIndicator("", resources.getDrawable(R.drawable.tab_search_off_btn)).setContent(searchIntent);

        final Intent favoritesIntent = new Intent().setClass(MainActivity.this, FavoritesActivity.class);
        TabSpec tabFavorites = tabHost.newTabSpec(TAB_FAVORITES).setIndicator("", resources.getDrawable(R.drawable.tab_favourite_off_btn)).setContent(favoritesIntent);

        Intent aboutIntent = new Intent().setClass(this, AboutActivity.class);
        TabSpec tabAbout = tabHost.newTabSpec(TAB_ABOUT).setIndicator("", resources.getDrawable(R.drawable.wip_info)).setContent(aboutIntent);*/

        tabHost.addTab(tabHotDeals);
        tabHost.addTab(tabSearch);
        tabHost.addTab(tabFavorites);

    }
}

如前所述,SearchActivity 是 FragmentActivity 的子类,类似于以下内容(请注意,指定了与 Support v4 相关的库。):

public class SearchActivity extends FragmentActivity {

    private static final String LOG_TAG = "Search Activity";
    Fragment fragment;
    //Temporary placeholder.

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

        final FragmentManager fm = getSupportFragmentManager();
        fragment = fm.findFragmentById(R.id.fragmentContainer);

        ImageButton bankButton = (ImageButton) findViewById(R.id.bank_button);
        ImageButton merchantButton = (ImageButton) findViewById(R.id.merchant_button);

        merchantButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if(fragment == null){
                    fragment = new SearchMerchantFragment();
                    fm.beginTransaction().add(R.id.fragmentContainer, fragment);
                }
            }
        });

        bankButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(fragment == null){
                    fragment = new SearchBankFragment();
                    fm.beginTransaction().add(R.id.fragmentContainer, fragment);
                }
            }
        });

    }

}

通过模拟器运行应用程序时,系统崩溃,主要原因是无法检测到 SearchActivity 类。但是,如果我不使用 FragmentActivity 继承,它会起作用。

这是堆栈跟踪:

06-03 17:53:14.156: E/AndroidRuntime(588): FATAL EXCEPTION: main
06-03 17:53:14.156: E/AndroidRuntime(588): java.lang.NoClassDefFoundError: com.mexter.rewards2go.activities.main.SearchActivity
06-03 17:53:14.156: E/AndroidRuntime(588):  at com.mexter.rewards2go.MainActivity.onCreate(MainActivity.java:39)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.os.Looper.loop(Looper.java:123)
06-03 17:53:14.156: E/AndroidRuntime(588):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-03 17:53:14.156: E/AndroidRuntime(588):  at java.lang.reflect.Method.invokeNative(Native Method)
06-03 17:53:14.156: E/AndroidRuntime(588):  at java.lang.reflect.Method.invoke(Method.java:507)
06-03 17:53:14.156: E/AndroidRuntime(588):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-03 17:53:14.156: E/AndroidRuntime(588):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-03 17:53:14.156: E/AndroidRuntime(588):  at dalvik.system.NativeStart.main(Native Method)

与 SearchActivity 类相关的片段已在 Search 布局 xml 文件中编写和声明。还检查了项目属性,包括使用支持 v4 相关库。

我仍然不确定我是否错过了 MainActivity 或 SearchActivity 类的某个地方。

4

2 回答 2

1

看看这个,它可能会解决你的问题。 https://plus.google.com/110296148724389171136/posts/7R2h3gCzi1k

于 2013-06-03T18:10:38.680 回答
0

一点微调:

打开复选框 - Java Build Path 的 Order and Export 选项卡中的 Android Libraries and Dependencies。然后做一个修复项目属性。

于 2013-06-04T03:33:35.367 回答