为在按钮上获取点击事件而编写的代码
public class MainActivity extends ActionBarActivity {
@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();
}
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Button pressed.",Toast.LENGTH_LONG).show();
}
});
}
这导致can't launch error
开发环境 带ADT的Eclipse
+ logcat错误日志
08-13 16:49:46.558:E / AndroidRuntime(1143):致命异常:mai 08-13 16:49:46.558:E / AndroidRuntime(1143):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.test1 / com.example.test1.MainActivity}:java.lang.NullPointerException 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.os.Handler.dispatchMessage(Handler.java:99) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.os.Looper.loop(Looper.java:130) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.app.ActivityThread.main(ActivityThread.java:3687) 08-13 16:49:46.558:E / AndroidRuntime(1143):at java.lang.reflect.Method.invokeNative(Native Method) 08-13 16:49:46.558:E / AndroidRuntime(1143):at java.lang.reflect.Method.invoke(Method.java:507) 08-13 16:49:46.558:E / AndroidRuntime(1143):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:847) 08-13 16:49:46.558:E / AndroidRuntime(1143):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 08-13 16:49:46.558:E / AndroidRuntime(1143):at dalvik.system.NativeStart.main(Native Method) 08-13 16:49:46.558:E / AndroidRuntime(1143):引起:java.lang.NullPointerException 08-13 16:49:46.558:E / AndroidRuntime(1143):at com.example.test1.MainActivity.onCreate(MainActivity.java:23) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 08-13 16:49:46.558:E / AndroidRuntime(1143):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 08-13 16:49:46.558:E / AndroidRuntime(1143):... 11更多
如果iv编辑了一个xml文件,那就是这个..否则全部都是默认生成的
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="Button" />
答案 0 :(得分:0)
试试此代码
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Button pressed.",Toast.LENGTH_LONG).show();
}
});