简单的noob应用程序每次崩溃..(null)?

时间:2013-02-02 17:49:59

标签: java android

我认为

Button bTutorial1 = (Button) findViewById(R.layout.tutorial1);
    bTutorial1.setOnClickListener(new View.OnClickListener() {

等于“Null”。

(我在某个地方读过,但没看到修复它)

如果那不是问题,我不知道......

我可以获得任何帮助。

真诚的,罗宾

activity_main代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/gotoKanal2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gotoKanal3"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="14dp"
        android:text="@string/tv_2" />

    <Button
        android:id="@+id/gotoKanal3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tutorial1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="14dp"
        android:text="@string/tv_3" />

    <Button
        android:id="@+id/tutorial1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tutorial2"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="14dp"
        android:text="TUTORIALONE" />

    <Button
        android:id="@+id/tutorial2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gotoKanal6"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="14dp"
        android:text="-||- 2" />

    <Button
        android:id="@+id/gotoKanal6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="14dp"
        android:text="@string/tv_6" />

    <Button
        android:id="@+id/gotoKanal1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/gotoKanal2"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="14dp"
        android:text="@string/tv_1" />

</RelativeLayout>

mainifest代码:忽略所有.tv

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.Robin.Robin.MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
         <activity
            android:name="myMenu"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter >
                <action android:name="com.Robin.Robin.CLEARSCREEN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
          <activity
            android:name=".tutorialOne"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter >
                <action android:name="com.Robin.Robin.TUTORIALONE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".tv2"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter >
                <action android:name="com.Robin.Robin.tv2"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".tv3"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter >
                <action android:name="com.Robin.Robin.tv3"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name="com.Robin.Robin.tv4"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter >
                <action android:name="com.Robin.Robin.tv4"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".tv5"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter >
                <action android:name="com.Robin.Robin.tv5"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".tv6"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter >
                <action android:name="com.Robin.Robin.tv6"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

启动代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/default_320x480" />

</LinearLayout>

myMeny.java代码:

 package com.Robin.Robin;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;


    public class myMenu extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button bTutorial1 = (Button) findViewById(R.layout.tutorial1);
        bTutorial1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.Robin.Robin.TUTORIALONE"));
            }
        });

            Button bgotoKanal4 = (Button) findViewById(R.id.gotoKanal4);
            bgotoKanal4.setOnClickListener( new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity (new Intent ("package com.Robin.Robin.tv4"));


                    Button bgotoKanal2 = (Button) findViewById(R.id.gotoKanal2);
                    bgotoKanal2.setOnClickListener( new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            startActivity (new Intent ("package com.Robin.Robin.tv2"));


                            Button bgotoKanal3 = (Button) findViewById(R.id.gotoKanal3);
                            bgotoKanal3.setOnClickListener( new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    startActivity (new Intent ("package com.Robin.Robin.tv3"));


                                    Button bgotoKanal1 = (Button) findViewById(R.id.gotoKanal1);
                                    bgotoKanal1.setOnClickListener( new View.OnClickListener() {

                                        @Override
                                        public void onClick(View v) {
                                            // TODO Auto-generated method stub
                                            startActivity (new Intent ("package com.Robin.Robin.tv1"));


                                            Button bgotoKanal5 = (Button) findViewById(R.id.gotoKanal5);
                                            bgotoKanal5.setOnClickListener( new View.OnClickListener() {

                                                @Override
                                                public void onClick(View v) {
                                                     TODO Auto-generated method stub
                                                    startActivity (new Intent ("package com.Robin.Robin.tv5"));


                                                    Button bgotoKanal6 = (Button) findViewById(R.id.gotoKanal6);
                                                    bgotoKanal6.setOnClickListener( new View.OnClickListener() {

                                                        @Override
                                                        public void onClick(View v) {
                                                            // TODO Auto-generated method stub
                                                            startActivity (new Intent ("package com.Robin.Robin.tv6"));


                                                        }
                                                    } );
                                                }
                                            } );
                                        }
                                    } );
                                }
                            } );
                        }
                    } );
                }
            } );



        }



    }

MainActivity代码:

package com.Robin.Robin;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {

MediaPlayer mpSplash;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);


    mpSplash = MediaPlayer.create(this, R.raw.javaintro);
    mpSplash.start();

    Thread logoTimer = new Thread(){
        public void run(){
            try{
                int logoTimer = 0;
                while(logoTimer < 5000){
                    sleep(100);
                    logoTimer = logoTimer + 100;
                }
                startActivity(new Intent("com.Robin.Robin.CLEARSCREEN"));
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
            finally{
                finish();
            }
        }


    };
    logoTimer.start();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    mpSplash.release();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    mpSplash.pause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mpSplash.start();
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
}
}

1 个答案:

答案 0 :(得分:1)

Button bTutorial1 = (Button) findViewById(R.layout.tutorial1);

应该是

Button bTutorial1 = (Button) findViewById(R.id.tutorial1);

因为xml中的Button被定义为

 <Button
        android:id="@+id/tutorial1"
        android:layout_width="match_parent"
        ...

将在R.id中定义哪个,因此,您将其检索为R.id.tutorial1
此外问题,您是否忘记关闭OnClickListener?

bgotoKanal2.setOnClickListener( new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            startActivity (new Intent ("package com.Robin.Robin.tv2"));
                      });//closes the listener, add it to each one.