事件监听器无法正常工作

时间:2012-06-12 21:17:56

标签: android

以下是main activity class的代码。它将内容设置为main.xml,其中包含按钮btn。单击此按钮后,内容将设置为pic.xml,其中包含两个按钮btn1btn2。点击btn1后,应将内容设置回main.xml,但这种情况不会发生。

package com.asin.amit;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.PixelFormat;


import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class AsinActivity extends Activity {
/** Called when the activity is first created. */
private TextView tv ;
private VideoView myVideoView;
private Button btn;
private Button btn1;
private Button btn2;


@Override


public void onCreate(Bundle savedInstanceState) {


    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.b);
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);

        String str= "/sdcard/DCIM/a.mp4";


        tv = (TextView) findViewById(R.id.tv1);


        myVideoView = (VideoView)findViewById(R.id.myvideoview);
        myVideoView.setVideoPath(str);
        myVideoView.setMediaController(new MediaController(this));
        myVideoView.requestFocus();
        myVideoView.start();


        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                myVideoView.pause();                         
                setContentView(R.layout.pic);
            }                                   
        });

        btn1.setOnClickListener(new ButtonListener());


    } 
    catch (Exception e) {
        // handle any errors
        Log.e("HelloWorld", "1", e);  // log the error
        // Also let the user know something went wrong
        Toast.makeText(
                getApplicationContext(),
                e.getClass().getName() + " " + e.getMessage(),
                Toast.LENGTH_LONG).show();
    }
}

class ButtonListener implements View.OnClickListener{
    @Override
    public void onClick(View v) {

        setContentView(R.layout.main);
    }
}
}

btn1.setOnClickListener(new ButtonListener());行,logcat正在说java.lang.NullPointerException

我做错了什么?

2 个答案:

答案 0 :(得分:0)

根据您所写的内容,When this button is clicked, content is set to pic.xml, which has two buttons, btn1 and btn2您的btn1和btn2位于 R.layout.pic 中,但您正试图在 R.layout中找到它们。

主要布局

btn1 = (Button) findViewById(R.id.button1);

btn2 = (Button) findViewById(R.id.button2);

您应该仅在setContentView(R.layout.pic)之后将值设置为btn1和btn2变量(当然也是onClickListener),因为只有那时您的布局才会包含这些按钮

答案 1 :(得分:0)

在一项活动中不止一次使用setContentView()(几乎?)总是一个坏主意。您正在从main.xml的{​​{1}}布局中选择ID为R.id.button1的按钮,并将听众设置为该按钮。然后,当您致电onCreate()时,该按钮不再有效。设置内容视图后,您必须再次说setContentView(R.layout.pic)。这只是一个糟糕的设计,因为它有很多机会可以打破。您单击按钮时无法简单地使用btn = (Button)findViewById(R.id.button1)作为内容视图启动新活动的任何原因?

相关问题