0

以下是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

我在做什么错?

4

2 回答 2

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.main布局中找到它们

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

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

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

于 2012-06-12T21:31:03.120 回答
0

setContentView()在一项活动中多次使用(几乎?)总是一个坏主意。main.xml您正在从您的布局中选择ID 为 R.id.button1 的按钮onCreate(),并为该按钮设置一个侦听器。当您随后调用setContentView(R.layout.pic)该按钮时不再有效。btn = (Button)findViewById(R.id.button1)设置内容视图后,您必须再说一遍。这只是一个糟糕的设计,因为它有很多破坏的机会。pic.xml当您单击按钮时,有什么理由不能简单地启动一个新的 Activity作为其内容视图?

于 2012-06-12T21:38:22.947 回答