我有一个带有提交按钮的loginActivity,其中包含onClick的sendMessage(),当我尝试在模拟器上运行应用程序时,我得到的结果是:找不到资源,这是我第二个活动布局的资源。 任何帮助,将不胜感激。我试图删除R.java并重建,但这“没有用。
loginActivity
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>
答案 0 :(得分:3)
因此,基于此线程上发生的讨论,问题是布局资源被放置在错误的文件夹中,这是开发人员网站中解释如何为特定设备配置提供资源的部分。