扩展实现onclicklistener后,我的应用程序崩溃了

时间:2014-06-24 12:08:16

标签: android onclicklistener

当我在android开发中训练时遇到麻烦,我扩展了实现onclicklistener之后我的应用程序崩溃了

这是我的主要活动:

package com.UAS.mathgame;

import android.app.Activity;
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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;


public class MainActivity extends Activity implements OnClickListener {

private Button playBtn, helpBtn, highBtn;
private String[] levelNames = {"Easy", "Medium", "Hard"};

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

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

    playBtn = (Button)findViewById(R.id.play_btn);
    helpBtn = (Button)findViewById(R.id.help_btn);
    highBtn = (Button)findViewById(R.id.high_btn);

    playBtn.setOnClickListener(this);
    helpBtn.setOnClickListener(this);
    highBtn.setOnClickListener(this);

}

@Override
public void onClick(View view) {
    if(view.getId()==R.id.play_btn){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose a level")
        .setSingleChoiceItems(levelNames, 0, new    DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //start gameplay
                startPlay(which);
            }
        });
        AlertDialog ad = builder.create();
        ad.show();
    }
    else if(view.getId()==R.id.help_btn){
        //how to play button
        Intent helpIntent = new Intent(this, HowToPlay.class);
        this.startActivity(helpIntent);
    }
    else if(view.getId()==R.id.high_btn){
        //high scores button
        Intent highIntent = new Intent(this, HighScores.class);
        this.startActivity(highIntent);
    }    
}

private void startPlay(int chosenLevel)
{
    //start gameplay
    Intent playIntent = new Intent(this, PlayGame.class);
    playIntent.putExtra("level", chosenLevel);
    this.startActivity(playIntent);
}

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

}

这是我的logcat:

06-24 06:15:08.057: E/AndroidRuntime(1701): FATAL EXCEPTION: main
06-24 06:15:08.057: E/AndroidRuntime(1701): Process: com.UAS.mathgame, PID: 1701
06-24 06:15:08.057: E/AndroidRuntime(1701): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.UAS.mathgame/com.UAS.mathgame.MainActivity}: java.lang.NullPointerException
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.os.Looper.loop(Looper.java:136)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at java.lang.reflect.Method.invoke(Method.java:515)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at dalvik.system.NativeStart.main(Native Method)
06-24 06:15:08.057: E/AndroidRuntime(1701): Caused by: java.lang.NullPointerException
06-24 06:15:08.057: E/AndroidRuntime(1701):     at com.UAS.mathgame.MainActivity.onCreate(MainActivity.java:37)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.app.Activity.performCreate(Activity.java:5231)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-24 06:15:08.057: E/AndroidRuntime(1701):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-24 06:15:08.057: E/AndroidRuntime(1701):     ... 11 more
06-24 06:15:26.507: I/Process(1701): Sending signal. PID: 1701 SIG: 9

抱歉英语不好,谢谢你们帮助我们......

1 个答案:

答案 0 :(得分:0)

改变这个..

setContentView(R.layout.activity_main);

setContentView(R.layout.fragment_main);

因为Button位于fragment_main.xml,所以setContentView应该引用fragment_main.xml

删除

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

或者将您的Button初始化和听众移至PlaceholderFragment.class,如下所示

public static class PlaceholderFragment extends Fragment implements OnClickListener{

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    playBtn = (Button) rootView.findViewById(R.id.play_btn);
    helpBtn = (Button) rootView.findViewById(R.id.help_btn);
    highBtn = (Button) rootView.findViewById(R.id.high_btn);

    playBtn.setOnClickListener(getActivity());
    helpBtn.setOnClickListener(getActivity());
    highBtn.setOnClickListener(getActivity());

    return rootView;
}

@Override
public void onClick(View view) {
if(view.getId()==R.id.play_btn){
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Choose a level")
    .setSingleChoiceItems(levelNames, 0, new    DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            //start gameplay
            startPlay(which);
        }
    });
    AlertDialog ad = builder.create();
    ad.show();
}
else if(view.getId()==R.id.help_btn){
    //how to play button
    Intent helpIntent = new Intent(getActivity(), HowToPlay.class);
    getActivity().startActivity(helpIntent);
}
else if(view.getId()==R.id.high_btn){
    //high scores button
    Intent highIntent = new Intent(getActivity(), HighScores.class);
    getActivity().startActivity(highIntent);
}    
}

}