这是主要的java代码,我在其中创建了一个主游戏菜单,我尝试使用xml文件设置页面:
package com.self;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class main extends Activity implements OnClickListener {
TextView c, ng, h, cr, s, e;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(com.self.R.layout.main);
c = (TextView) findViewById(com.self.R.id.cont);
ng = (TextView) findViewById(com.self.R.id.newg);
h = (TextView) findViewById(com.self.R.id.help);
cr = (TextView) findViewById(com.self.R.id.credits);
s = (TextView) findViewById(com.self.R.id.settings);
e = (TextView) findViewById(com.self.R.id.exit);
c.setOnClickListener(this);
ng.setOnClickListener(this);
h.setOnClickListener(this);
cr.setOnClickListener(this);
s.setOnClickListener(this);
e.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case com.self.R.id.settings:
Class myclass;
try {
myclass = Class.forName("com.self.settings");
Intent myint = new Intent(main.this, myclass);
startActivity(myint);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
和settings.xml文件,当我点击主xml文件中的设置textview时,它应该打开。
<TextView android:text="@string/sound"
android:layout_width="fill_parent" android:layout_height="20dp"
android:layout_gravity="right" android:id="@+id/sound">
</TextView>
<TextView android:text="@string/music" android:id="@+id/music"
android:layout_width="fill_parent"
android:layout_height="20dp" style="@style/MenuFont">
</TextView>
<TextView android:text="@string/vibrate" android:layout_width="fill_parent"
android:layout_height="20dp" android:id="@+id/vibrate"
style="@style/MenuFont">
</TextView>
<TextView android:text="@string/graphics"
android:layout_width="fill_parent" android:layout_height="20dp"
android:id="@+id/graphics" style="@style/MenuFont">
</TextView>
<TextView android:text="@string/back"
android:layout_width="fill_parent" android:layout_height="20dp"
android:id="@+id/back" style="@style/MenuFont">
</TextView>
</LinearLayout>
这是我的表现:
<activity android:name=".main" 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=".settings" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
这是我的Logcat报告:
ERROR/AndroidRuntime(438): FATAL EXCEPTION: main ERROR/AndroidRuntime(438): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.self/com.self.settings}: android.content.res.Resources$NotFoundException: Resource ID #0x7f060002 type #0x12 is not valid
ERROR/AndroidRuntime(438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
ERROR/AndroidRuntime(438): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
答案 0 :(得分:1)
问题在于contentview。你可以告诉我为什么你把布局设置为id? setContentView(com.self.R.id.settings);
除此之外,您应将其设为setContentView(R.layout.settings);