package com.nando.testile;
import android.app.Activity;
import android.app.ActionBar;
import android.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.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener{
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "button was clicked", Toast.LENGTH_LONG).show();
}
}
<LinearLayout 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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
04-06 16:20:45.831: W/dalvikvm(2642): threadid=1: thread exiting with uncaught exception (group=0xb1cadb20)
04-06 16:20:45.841: E/AndroidRuntime(2642): FATAL EXCEPTION: main
04-06 16:20:45.841: E/AndroidRuntime(2642): Process: com.nando.testile, PID: 2642
04-06 16:20:45.841: E/AndroidRuntime(2642): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nando.testile/com.nando.testile.MainActivity}: java.lang.NullPointerException
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.os.Handler.dispatchMessage(Handler.java:102)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.os.Looper.loop(Looper.java:136)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-06 16:20:45.841: E/AndroidRuntime(2642): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 16:20:45.841: E/AndroidRuntime(2642): at java.lang.reflect.Method.invoke(Method.java:515)
04-06 16:20:45.841: E/AndroidRuntime(2642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-06 16:20:45.841: E/AndroidRuntime(2642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-06 16:20:45.841: E/AndroidRuntime(2642): at dalvik.system.NativeStart.main(Native Method)
04-06 16:20:45.841: E/AndroidRuntime(2642): Caused by: java.lang.NullPointerException
04-06 16:20:45.841: E/AndroidRuntime(2642): at com.nando.testile.MainActivity.onCreate(MainActivity.java:26)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.Activity.performCreate(Activity.java:5231)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-06 16:20:45.841: E/AndroidRuntime(2642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-06 16:20:45.841: E/AndroidRuntime(2642): ... 11 more
答案 0 :(得分:1)
fragment_main.xml
中的button1。
将其从那里放入activity_main.xml
。