I am following along with a tutorial on the internet and have run into a problem. I'm trying to add an event listener to a button, when that button is pressed, the image view picture is changed. I keep getting an error and it wont let me launch the app on the emulator. If I remove one line of code it works. I have made sure that the id names are correct, I have checked many times. What's the problem here? Thanks!

if I remove "button.setOnClickListener(mCorkyListener);"

it runs fine, but of course with no listeners.

LOGCAT:

05-15 20:56:02.505: D/AndroidRuntime(1956): Shutting down VM
05-15 20:56:02.505: W/dalvikvm(1956): threadid=1: thread exiting with uncaught exception (group=0xb1d1cb20)
05-15 20:56:02.505: E/AndroidRuntime(1956): FATAL EXCEPTION: main
05-15 20:56:02.505: E/AndroidRuntime(1956): Process: com.example.androidapplication_clicklisteners, PID: 1956
05-15 20:56:02.505: E/AndroidRuntime(1956): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidapplication_clicklisteners/com.example.androidapplication_clicklisteners.MainActivity}: java.lang.NullPointerException
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.os.Looper.loop(Looper.java:136)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at java.lang.reflect.Method.invoke(Method.java:515)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at dalvik.system.NativeStart.main(Native Method)
05-15 20:56:02.505: E/AndroidRuntime(1956): Caused by: java.lang.NullPointerException
05-15 20:56:02.505: E/AndroidRuntime(1956):     at com.example.androidapplication_clicklisteners.MainActivity.onCreate(MainActivity.java:39)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.Activity.performCreate(Activity.java:5231)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 20:56:02.505: E/AndroidRuntime(1956):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-15 20:56:02.505: E/AndroidRuntime(1956):     ... 11 more

Also: My Application will launch but I will get an error popup on the emulator saying My application has to stop. The logcat only fills once I click ok to the popup.

heres my code:

package com.example.androidapplication_clicklisteners;

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.ImageView;
import android.os.Build;


public class MainActivity extends ActionBarActivity {

private ImageView imageToShow;
private Button button;

private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
        imageToShow.setBackgroundResource(R.drawable.bubble_image);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    imageToShow = (ImageView)findViewById(R.id.imageView1);


    button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(mCorkyListener);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@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;
    }
}

}
有帮助吗?

解决方案

If the button is inside the fragment then your class should look like

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);
    Button button = (Button)rootView.findViewById(R.id.button1);
    button.setOnClickListener(mCorkyListener);
    return rootView;
}
}

Otherwise you get the NullPointerException 'cause no view is found. Notice that the imageView member must be obtained properly as well.

Hope it helps.

其他提示

You are getting a NullPointerException because your button id is in the fragment_main.xml.
The recent upgrade of the ADT will confuse lots of beginners who rely on old tutorials.
Move your button operation in the fragment class.

/**
 * 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);
       Button button = (Button)rootView.findViewById(R.id.button1);
       button.setOnClickListener(mCorkyListener);
         imageToShow = (ImageView)rootView.findViewById(R.id.imageView1);
        return rootView;
    }
}

Another way to solve your problem is to let your MainActivity extends Activity, instead ActionBarActivity and get rid of fragments.

You have a Null Pointer Exception on line 39 in onCreate.

E/AndroidRuntime(1956): at com.example.androidapplication_clicklisteners.MainActivity.onCreate(MainActivity.java:39)

You seem to be getting a NullPointerException. mCorkyListener is clearly not null, so I surmise that button is null. In other words, verify that there exists a Button with anid of R.id.imageView1 in R.layout.activity_main

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top