这是Java,其中日志猫说我有错误...我将在该行添加注释...它说我已将文本视图投射到按钮但我根本没有。
package k.t.m;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView errors;
TextView tkm;
TextView result;
TextView main;
Button mainButton1;
Button mainButton2;
Button back;
Button go;
EditText textbox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (TextView) findViewById(R.id.main);
back = (Button) findViewById(R.id.secondaryButtonBack);
mainButton1 = (Button) findViewById(R.id.mainButton1);
mainButton2 = (Button) findViewById(R.id.mainButton2);
go = (Button) findViewById(R.id.goButton); // error here in the logcat... below
tkm = (TextView) findViewById(R.id.mkm);
result = (TextView) findViewById(R.id.result);
errors = (TextView) findViewById(R.id.errors);
textbox = (EditText) findViewById(R.id.TextBox);
textbox.setVisibility(View.INVISIBLE);
back.setVisibility(View.INVISIBLE);
go.setVisibility(View.INVISIBLE);
mainButton1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
milesToKilometers();
}
});
mainButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
kilometersToMiles();
}
});
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
back();
result.setText("");
tkm.setText("");
}
});
}
protected void back() {
main.setVisibility(View.VISIBLE);
mainButton1.setVisibility(View.VISIBLE);
mainButton2.setVisibility(View.VISIBLE);
back.setVisibility(View.INVISIBLE);
go.setVisibility(View.INVISIBLE);
textbox.setVisibility(View.INVISIBLE);
}
protected void kilometersToMiles() {
tkm.setText("Kilometers:");
main.setVisibility(View.INVISIBLE);
mainButton1.setVisibility(View.INVISIBLE);
mainButton2.setVisibility(View.INVISIBLE);
back.setVisibility(View.VISIBLE);
go.setVisibility(View.VISIBLE);
textbox.setVisibility(View.VISIBLE);
go.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(textbox.getText().toString().equals("")){
errors.setText("Error: Kilometers Missing");
}
}
});
}
protected void milesToKilometers() {
main.setVisibility(View.INVISIBLE);
mainButton1.setVisibility(View.INVISIBLE);
mainButton2.setVisibility(View.INVISIBLE);
back.setVisibility(View.VISIBLE);
go.setVisibility(View.VISIBLE);
textbox.setVisibility(View.VISIBLE);
go.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
现在这是 logcat 错误,我不知道出了什么问题……这对我来说毫无意义。
08-18 15:54:14.687: E/AndroidRuntime(15778): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
08-18 15:54:14.687: E/AndroidRuntime(15778): at k.t.m.MainActivity.onCreate(MainActivity.java:32)
提前感谢所有人......这是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" >
<TextView
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text=" What do you wish to convert?" />
<Button
android:id="@+id/mainButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="122dp"
android:text="Kilometers to Miles" />
<Button
android:id="@+id/mainButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/mainButton2"
android:layout_below="@+id/mainButton2"
android:layout_marginTop="126dp"
android:text="Miles to Kilometers" />
<Button
android:id="@+id/secondaryButtonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mainButton1"
android:layout_marginTop="57dp"
android:layout_toRightOf="@+id/mainButton1"
android:text="Back" />
<Button
android:id="@+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mainButton1"
android:layout_centerHorizontal="true"
android:text="Go" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="" />
<TextView
android:id="@+id/errors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="" />
<EditText
android:id="@+id/TextBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/result"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/mkm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/TextBox"
android:layout_toLeftOf="@+id/result" />
</RelativeLayout>
这是 r.java 我不知道要找什么所以在这里
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package k.t.m;
public final class R {
public static final class attr {
}
public static final class dimen {
public static final int padding_large=0x7f040002;
public static final int padding_medium=0x7f040001;
public static final int padding_small=0x7f040000;
}
public static final class drawable {
public static final int ic_action_search=0x7f020000;
public static final int ic_launcher=0x7f020001;
}
public static final class id {
public static final int TextBox=0x7f080008;
public static final int errors=0x7f080007;
public static final int goButton=0x7f080005;
public static final int main=0x7f080000;
public static final int mainButton1=0x7f080002;
public static final int mainButton2=0x7f080001;
public static final int menu_settings=0x7f080009;
public static final int mkm=0x7f080004;
public static final int result=0x7f080006;
public static final int secondaryButtonBack=0x7f080003;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f070000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050001;
public static final int menu_settings=0x7f050002;
public static final int title_activity_main=0x7f050003;
}
public static final class style {
public static final int AppTheme=0x7f060000;
}
}