由于我还是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类中的某个地方。
最佳答案
检查一下,这可能会解决您的问题。
https://plus.google.com/110296148724389171136/posts/7R2h3gCzi1k