2013-02-02 42 views
0

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

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

等于 “零”。

(我读的地方,但没有看到一个修复它)

如果这是没有问题的PLZ我不知道..

THX对于任何帮助我能。

真诚,罗宾

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

1.发布LogCat(检查你的Eclipse的logcat视图)2.不要粘贴你的完整项目在这里......否则我要求得到支付每行代码! – WarrenFaith

+0

像这样嵌套'OnClickListeners'绝对不是一个好主意(除非你喜欢三角形那么多),它会降低你的代码的可读性并增加错误的几率)。考虑使用某种计数器变量(这并不完全清楚你在做什么)。 –

+0

我正在做一个介绍屏幕,然后是一个mainScreen白色6按钮,然后是按钮进入的性别屏幕。 对不起,我是新来的,想学习。 @ A - C – user2034253

回答

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

应该

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

因为你在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. 
+0

Thx男子,我只做了R.id.tutorial1,现在它工作<3 – user2034253