gpt4 book ai didi

java - 为 SearchActivity 创建 Intent 时出现 NoClassDefFoundError 异常

转载 作者:行者123 更新时间:2023-12-01 14:30:20 24 4
gpt4 key购买 nike

由于我还是 Android 新手(三周前开始),因此我的 MainActivity 类基于已弃用的 TabActivity 父类(super class)(因为我想适应版本 10-12)。在我的 MainActivity 类中,我创建了五个主要 Intent ,其中三个旨在成为基于 FragmentActivity 的 Activity (预计会有大量的深入分析)。

这是我的 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 类相关的 fragment 已在搜索布局 xml 文件中编写和声明。还检查了项目属性,包括支持 v4 相关库的使用。

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

最佳答案

检查一下,它可能会解决您的问题。 https://plus.google.com/110296148724389171136/posts/7R2h3gCzi1k

关于java - 为 SearchActivity 创建 Intent 时出现 NoClassDefFoundError 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16902913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com