1

我是 android 新手,正在做一个温度转换器的简单应用。我在屏幕上放置了 2 个文本视图和一个按钮。我只是想看看,当我按下按钮时,当我运行应用程序时,是否有事件发生

未找到源:源附件不包含文件 view.class 的源

这是我的代码:

Activity_main.xml

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/red"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        style="@style/AppTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="136dp"
        android:layout_marginLeft="94dp"
        android:onClick="onclick"
        android:text="@string/Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="38dp"
        android:text="@string/Large_Text"
        android:textStyle="italic" 
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="42dp"
        android:text="@string/Large_Text1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView1"
        android:layout_toRightOf="@+id/button1"
        android:ems="10"
        android:inputType="numberSigned" />

</RelativeLayout>

字符串.XML

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Converter</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="Button">Convert</string>
   <string name="Large_Text">Deg C</string>
   <string name="Large_Text1">Deg F</string>
   <color name="red">#D3D3D3</color>
</resources>

Main_activity.java

package com.example.converter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;



public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我创建了一个 classes1.java

package com.example.converter;

import android.app.Activity;
import android.widget.TextView;



public class Classs1 extends Activity{

public void onclick(){
    setContentView(R.layout.activity_main);
    TextView t = (TextView)findViewById(R.id.textView1);
    t.setText(4);

}

}
4

1 回答 1

2

你没有调用事件监听器到你的视图

public class Classs1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    btn = (Button) findViewById(R.id.ur_button_id);
    btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 setContentView(R.layout.activity_main);
                             TextView t = (TextView)findViewById(R.id.textView1);
                             t.setText(4);
            }
          });
}

编辑:

错误 : Source Not Found:Source attachment does not contain the source for the file view.class

你是否classs1.java在你的manifest文件中添加了请检查一下。

于 2013-05-17T06:01:30.823 回答