我想調用一個名爲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無法解析爲類型',我希望這可以幫助你。 –