java - 简单的菜鸟应用程序每次都会崩溃..(空)?

标签 java android

我认为

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

等于“空”。

(我在某处读过它,但没有看到解决方案)

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

感谢我能得到的任何帮助。

此致,罗宾

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>

ma​​inifest代码:忽略所有.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"));


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



        }



    }

主要 Activity 代码:

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();
}
}

最佳答案

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

应该是

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

因为你的Button在xml中定义为

 <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.

关于java - 简单的菜鸟应用程序每次都会崩溃..(空)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664678/

相关文章:

java - 异常:OutOfMemoryError: Java heap space when running the code

java - 用于在模式之后提取值的正则表达式

android - 从 BroadcastReceiver 启动 Activity

java - 如何让Java从XML文件中调用字符串(在Android Studio中)

java - 如何在 low api 中使用 getCurrencyInstance 和 format?

java - 在 JBoss 中添加依赖 jar

android - 是否需要明确支持AndroidX

android - Eclipse 中覆盖注释的错误

Android studio Bumblebee build.gradle 根项目无法添加类路径依赖

Android Studio AVD Manager 在 AVD 创建期间崩溃