无法启动活动组件信息

时间:2014-04-24 12:27:00

标签: android nullpointerexception android-logcat

我的程序无效, 我对surfaceviewexample class有一些问题。 无法启动活动java.lang.NullPointerException. 我正在尝试按下tut5按钮,我开始我的表面视图。 它是什么原因? 我做错了什么?

SurfaceViewExample.java

    public class SurfaceViewExample extends Activity implements View.OnTouchListener{
    OurView v;
    Bitmap ball;
    float x,y;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        v=new OurView(this);
        v=setOnTouchListener(this);
        ball= BitmapFactory.decodeResource(getResources(),R.drawable.ball);
        x=y=0;
        setContentView(v);
    }

    private OurView setOnTouchListener(SurfaceViewExample surfaceViewExample) {
        return null;
    }


    @Override
    protected void onPause(){
        super.onPause();
        v.pause();
    }
    @Override
    protected void onResume(){
        super.onResume();
        v.resume();
    }

    @Override
    public boolean onTouch(View v , MotionEvent me) {
        return false;
    }

    public class  OurView extends SurfaceView implements Runnable {
        Thread t=null;
        SurfaceHolder holder;
        boolean isItOK=false;

        public OurView(Context context){
            super(context);
            holder=getHolder();
        }

        @Override
        public void run()
        {
           while (isItOK==true)
            {
                if(!holder.getSurface().isValid()){
                    continue;
                }
                Canvas c=holder.lockCanvas();
                c.drawARGB(255,150,150,10);
                c.drawBitmap(ball,x,y,null);
                holder.unlockCanvasAndPost(c);
            }
        }
        public void pause(){
            isItOK=false;
            while(true){
                try{
                    t.join();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                break;
            }
            t=null;

        }
        public void resume(){
            isItOK=true;
            t=new Thread(this);
            t.start();
        }
    }
 }

menu.java

 public class menu extends Activity   {


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView (com.example.youtubeproject.R.layout.activity_main);
        final MediaPlayer  buttonSound = MediaPlayer.create(menu.this,  
          com.example.youtubeproject.R.raw.button_click);
        Button tut1= (Button)findViewById(com.example.youtubeproject.R.id.tutorial1);
        Button tut2= (Button)findViewById(com.example.youtubeproject.R.id.tutorial2);
        Button tut3= (Button)findViewById(com.example.youtubeproject.R.id.tutorial3);
        Button tut4=(Button)findViewById(com.example.youtubeproject.R.id.tutorial4);
        Button tut5=(Button)findViewById(com.example.youtubeproject.R.id.tutorial5);
        tut1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                setContentView (com.example.youtubeproject.R.layout.tutorial1);

                Intent TutorialIntent=new Intent("com.example.youtubeproject.TUTORIAL1");
                startActivity(TutorialIntent);

                //startActivity(new Intent("com.example.youtubeproject.TUTORIAL1"));

            }
        });

        tut2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent("com.example.youtubeproject.TUTORIALTWO"));

            }
        });
        tut3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                setContentView (com.example.youtubeproject.R.layout.wallpapaer);
                Intent TutorialIntent=new
              Intent("com.example.youtubeproject.TUTORIALTHREE");
                startActivity(TutorialIntent);

            }
        });
        tut4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                Intent TutorialIntent=new 
            Intent("com.example.youtubeproject.TUTORIALFOUR");
                startActivity(TutorialIntent);
               //setContentView (com.example.youtubeproject.R.layout.wallpapaer);
               //Intent TutorialIntent=new  
             Intent("com.example.youtubeproject.TUTORIALTHREE");
                //startActivity(TutorialIntent);

            }
        });
        tut5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                //startActivity(new Intent("com.example.youtubeproject.TUTORIALFIVE"));
                Intent TutorialIntent=new   
          Intent("com.example.youtubeproject.TUTORIALFIVE");
               startActivity(TutorialIntent);
                //setContentView (com.example.youtubeproject.R.layout.wallpapaer);
                //Intent TutorialIntent=new  
        Intent("com.example.youtubeproject.TUTORIALTHREE");
                //startActivity(TutorialIntent);

            }
        });
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);
        MenuInflater awesome=getMenuInflater();
        awesome.inflate(com.example.youtubeproject.R.menu.main_menu,menu);
        return true;
    }
    public  boolean onOptionsItemSelected(MenuItem item){
        switch(item .getItemId()){
            case com.example.youtubeproject.R.id.menuSweet:
                startActivity(new Intent("com.example.youtubeproject.SWEET"));
                return true;

            case com.example.youtubeproject.R.id.menuToast:

                Toast andEggs=Toast.makeText(menu.this,"this is a toast 
         ",Toast.LENGTH_LONG);
                andEggs.show();
                return true;
        }
        return false;
    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.youtubeproject"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.youtubeproject.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
    android:name=".Tutorial1"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.example.youtubeproject.TUTORIAL1" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
    <activity
        android:name=".TutoralTwo"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.youtubeproject.TUTORIALTWO" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TutorialThree"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.youtubeproject.TUTORIALTHREE" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
    android:name=".SurfaceViewExample"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.example.youtubeproject.TUTORIALFIVE" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
    <activity
    android:name=".TutorialFour"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.example.youtubeproject.TUTORIALFOUR" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
    <activity

        android:name=".Sweet"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Dialog" >
        <intent-filter>
            <action android:name="com.example.youtubeproject.SWEET" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

   </manifest>

1 个答案:

答案 0 :(得分:0)

将此SurfaceViewExample活动声明为Manifest;

    <activity
        android:name="com.example.youtubeproject.SurfaceViewExample" >
    </activity>