我是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>
的strings.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;
}
}
我创建了一个classs1.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);
}
}
答案 0 :(得分:2)
U没有将事件监听器调用到您的视图
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
Plz也检查了这一点。