我想调用一个名为MainScreenActivity.java的java类中名为main.xml的.xml文件。我想用setContentView来导入一个.xml文件按钮在我的android应用程序中使用
我不能设法使用setContentView调用main.xml,我想知道如果我没有得到正确的语法,但我似乎可以解决它,任何帮助将不胜感激。
这是我MainScreenActivity java类:
package dbviewer.number1;
import AllProductsActivity.AllProductsActivity;
import NewProductActivity.NewProductActivity;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainScreenActivity extends Activity{
Button btnViewProducts;
Button btnNewProduct;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Buttons
btnViewProducts = (Button) findViewById(R.id.btnViewProducts);
btnNewProduct = (Button) findViewById(R.id.btnCreateProduct);
而下面是我的main.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"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainScreenActivity" >
<!-- Sample Dashboard screen with Two buttons -->
<!-- Button to view all products screen -->
<Button
android:id="@+id/btnViewProducts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:text="View Products" />
<!-- Button to create a new product screen -->
<Button
android:id="@+id/btnCreateProduct"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:text="Add New Products" />
</LinearLayout>
什么是错误信息? –
你的问题到底是什么? –
Divyesh,我得到的错误是'R.layout.main无法解析为类型',我希望这可以帮助你。 –