2015-10-14 24 views
0

任何人都可以幫助我調試出錯的地方。測試代碼可以得到:使用多種Android活動

https://drive.google.com/file/d/0Bz1lc03pNQm6Qkw3bE93dWxjdXc/view?usp=sharing

情景,我有3個activities 主,飛濺和菜單。首先,我在5秒鐘後調用Splash活動5秒鐘我稱菜單活動不在菜單中工作我想要調用主要活動。如果我跳過菜單活動比它的工作正常。

飛濺活動

public class Splash extends Activity{ 
    MediaPlayer ourSong; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 
     ourSong = MediaPlayer.create(Splash.this, R.raw.dj); 
     ourSong.start(); 
     Thread timer = new Thread() 
     { 
      public void run() 
      { 
       try 
       { 
        sleep(5000); 
       } 
       catch (InterruptedException e) 
       { 
        e.printStackTrace(); 
       } 
       finally 
       { 
       Intent start = new Intent("com.example.test.menu"); 
       startActivity(start); 
       } 
      } 
     }; 
     timer.start(); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     ourSong.release(); 
     finish(); 
    } 


} 

菜單活動

public class menu extends ListActivity{ 

    String classes[] = {"MainActivity", "example1", "example1", "example1", "example1"}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(menu.this, android.R.layout.simple_list_item_1, classes)); 
    } 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 
     String cheese = classes[position]; 
     try 
     { 
     Class ourclass = Class.forName("com.example.test."+cheese); 
     Intent ourIntent = new Intent(menu.this, ourclass); 
     startActivity(ourIntent); 
     } 
     catch(ClassNotFoundException e) 
     { 
       e.printStackTrace(); 
     } 

    } 



} 

主要活動

public class MainActivity extends Activity { 

    int counter; 
    Button add, subtruct; 
    TextView textarea; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     counter = 0 ; 
     add = (Button)findViewById(R.id.button1); 
     subtruct = (Button)findViewById(R.id.button2); 
     textarea = (TextView)findViewById(R.id.textView1); 
     add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter++; 
       textarea.setText("Your total is"+counter); 

      } 
     }); 
     subtruct.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter--; 
       textarea.setText("Your total is"+counter); 

      } 
     }); 
    } 


} 
+2

請在這裏直接發佈相關代碼,並突出顯示哪部分不適合你。謝謝。 – fasteque

回答

0

您的代碼:

Thread timer = new Thread() 
    { 
     public void run() 
     { 
      try 
      { 
       sleep(5000); 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 
      Intent start = new Intent("com.example.test.menu"); 
      startActivity(start); 
      } 
     } 
    }; 
    timer.start(); 

權代碼:

Thread timer = new Thread() 
    { 
     public void run() 
     { 
      try 
      { 
       sleep(5000); 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 
      Intent start = new Intent(Splash.this, menu.class); 
      startActivity(start); 
      } 
     } 
    }; 
    timer.start(); 

提示:不要使用小寫的類名

0

我想你在你的菜單活動」的onCreate()缺少setContentView(R.layout.activity_main); ..