android - 尝试切换到选项卡式 fragment Activity 时出现布局 "resource not found"异常

标签 android android-intent android-activity

我有一个带有提交按钮的登录 Activity ,该按钮具有用于 onClick 的 sendMessage() ,当我尝试在模拟器上运行该应用程序时,我得到的原因是:找不到资源,这是我的第二个 Activity 的布局的资源。 任何帮助,将不胜感激。我尝试删除 R.java 并重建,但这没有帮助。

登录 Activity

public class Login extends Activity {
    public final static String USERNAME = "com.example.seattleivg.USERNAME";

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

    public void sendMessage(View view) {
        Intent intent = new Intent(this, TabActionBarHomeActivity.class);
        //get username from usernameText field
        //EditText editText = (EditText) findViewById(R.id.usernameText);
        //String message = editText.getText().toString();
        //key-value pair to pass between activities
        //intent.putExtra(USERNAME, message);
        startActivity(intent);
    }

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.seattleivf"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.seattleivf.Login"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.seattleivf.TabActionBarHomeActivity"
            android:label="@string/app_name" 
            android:parentActivityName="com.example.seattleivf.Login">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.seattleivf.Login"/>
        </activity>
    </application>

</manifest>

TabActionBarHomeActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_tab_action_bar_home);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        //Make sure we're running on HoneyComb or higher to user ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ActionBar actionBar = getActionBar();

            //show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);

            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            String label1 = getResources().getString(R.string.label1);
            Tab tab = actionBar.newTab();
            tab.setText(label1);
            TabListener<AndrologyHomeFragment> tl = new TabListener<AndrologyHomeFragment>(
                    this, label1, AndrologyHomeFragment.class);
            tab.setTabListener(tl);
            actionBar.addTab(tab);

            String label2 = getResources().getString(R.string.label2);
            tab = actionBar.newTab();
            tab.setText(label2);
            TabListener<EmbryologyHomeFragment> tl2 = new TabListener<EmbryologyHomeFragment>(
                    this, label2, EmbryologyHomeFragment.class);
            tab.setTabListener(tl2);
            actionBar.addTab(tab);
        }
      //tried added here too
      //setContentView(R.layout.activity_tab_action_bar_home);
    }

activity_tab_action_bar_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".TabActionBarHomeActivity"
    android:orientation="vertical" >

</LinearLayout>

R.java

public static final class layout {
    public static final int activity_login=0x7f030000;
    public static final int activity_tab_action_bar_home=0x7f030001;
    public static final int andrology_home=0x7f030002;
    public static final int embryology_home=0x7f030003;
}

清理项目后更新了 LogCat 文件

05-01 15:30:28.609: E/AndroidRuntime(710): FATAL EXCEPTION: main
05-01 15:30:28.609: E/AndroidRuntime(710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.seattleivf/com.example.seattleivf.TabActionBarHomeActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.os.Looper.loop(Looper.java:137)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-01 15:30:28.609: E/AndroidRuntime(710):  at java.lang.reflect.Method.invokeNative(Native Method)
05-01 15:30:28.609: E/AndroidRuntime(710):  at java.lang.reflect.Method.invoke(Method.java:511)
05-01 15:30:28.609: E/AndroidRuntime(710):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-01 15:30:28.609: E/AndroidRuntime(710):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-01 15:30:28.609: E/AndroidRuntime(710):  at dalvik.system.NativeStart.main(Native Method)
05-01 15:30:28.609: E/AndroidRuntime(710): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.content.res.Resources.getValue(Resources.java:1013)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2098)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.content.res.Resources.getLayout(Resources.java:852)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-01 15:30:28.609: E/AndroidRuntime(710):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.Activity.setContentView(Activity.java:1867)
05-01 15:30:28.609: E/AndroidRuntime(710):  at com.example.seattleivf.TabActionBarHomeActivity.onCreate(TabActionBarHomeActivity.java:53)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.Activity.performCreate(Activity.java:5008)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-01 15:30:28.609: E/AndroidRuntime(710):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-01 15:30:28.609: E/AndroidRuntime(710):  ... 11 more

编辑 AndrologyHomeFragment.java(EmbryologyHomeFragment.java与各自的xml相同)

public class AndrologyHomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
        return (LinearLayout) inflater.inflate(R.layout.andrology_home, container, false);
    }
}

andrology_home.xml(embryology_home.xml 相同)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/andrology_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="andrology layout" />


</LinearLayout>

最佳答案

因此,根据此线程上发生的讨论,问题是布局资源放置在错误的文件夹中,这是开发人员网站中解释如何为特定设备配置提供资源的部分。

AlternativeResources

关于android - 尝试切换到选项卡式 fragment Activity 时出现布局 "resource not found"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16325935/

相关文章:

Android Activity findviewbyid() 为空

java - 相机 Intent ,无法将照片保存到外部存储

android - 我的光标只显示从 sqlite 数据库导入的 5 个项目

android - RecyclerView 中的 AddMore 按钮未被删除

Android Studio AVD 错误启动

android - 如何通过 ADB shell 广播带有附加功能的 Intent ?

android - Activity 和服务 : on swipe behavior

android - Activity 和 fragment 之间有什么区别?

java - 开始新 Activity 时应用程序崩溃

android RecyclerView setOnScrollChangeListener 需要 api 23?