2016-12-09 41 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不能解析为类型',我不知道您建议的进口有什么问题。 –

相关问题