抱歉我的英语不好...... 我是Android编程的新手。 我尝试使用TextView和Button制作一个非常简单的Android应用程序。我认为我的所有代码都是正确的,但是当我在AVD上运行时。它崩溃了。这是我的MainActivity.java代码:
package com.example.m;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
tv=(TextView)findViewById(R.id.textView1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("hola");
}
}
这是.XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.m.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:layout_width="110dp"
android:layout_height="122dp"
android:layout_below="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginTop="96dp"
android:layout_toRightOf="@+id/textView1"
android:text="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="27dp"
android:layout_marginTop="34dp"
android:text="TextView" />
</RelativeLayout>
这是logCat:
03-13 16:10:32.715: E/AndroidRuntime(272): FATAL EXCEPTION: main
03-13 16:10:32.715: E/AndroidRuntime(272): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.m/com.example.m.MainActivity}: java.lang.ClassCastException: android.widget.TextView
03-13 16:10:32.715: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.os.Looper.loop(Looper.java:123)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 16:10:32.715: E/AndroidRuntime(272): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 16:10:32.715: E/AndroidRuntime(272): at java.lang.reflect.Method.invoke(Method.java:521)
03-13 16:10:32.715: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 16:10:32.715: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 16:10:32.715: E/AndroidRuntime(272): at dalvik.system.NativeStart.main(Native Method)
03-13 16:10:32.715: E/AndroidRuntime(272): Caused by: java.lang.ClassCastException: android.widget.TextView
03-13 16:10:32.715: E/AndroidRuntime(272): at com.example.m.MainActivity.onCreate(MainActivity.java:30)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 16:10:32.715: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-13 16:10:32.715: E/AndroidRuntime(272): ... 11 more
答案 0 :(得分:1)
private TextView tv;
private Button btn1;
onCreate()
方法:
tv=(TextView)findViewById(R.id.textView1);
//add the listener.
tv.setOnClickListener(this);
//you can define too a listener to your button1.
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(this);
使用onClick()
方法中的开关来处理多个视图:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.textView1:
tv.setText("hola");
break;
case R.id.button1:
tv.setText("click on button1");
break;
default:
//code..
break;
};
}