2016-12-09 61 views
-1

我想調用一個名爲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> 
+0

什麼是錯誤信息? –

+0

你的問題到底是什麼? –

+0

Divyesh,我得到的錯誤是'R.layout.main無法解析爲類型',我希望這可以幫助你。 –

回答

1

R.class進口不正確。

替換:

import android.R; 

import dbviewer.number1.R; 

或任何你的主包是:-)

還有爲項目生成幾個R類,你需要有正確的一個導入來訪問資源:-)

+0

這似乎並沒有爲我工作我害怕,當我用你建議的導入替換導入錯誤'導入dbviewer.number1.R不能解析爲類型',我不知道您建議的進口有什麼問題。 –

相關問題