简体   繁体   中英

Unfortunately,“App” has stopped, OnClickListener

i'm new here. I just wrote this code, but on my emulator I get the message "unfortunately, "App" has stopped. Here are my codes. I don't know where the error is, maybe you do.

MainActivity.java

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();

}

}

fragment_main.xml

<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>

Logcatfile

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

Your button1 in fragment_main.xml .

Place it from there into activity_main.xml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM