我正在关注newboston的教程(Android应用程序开发),但我运行的应用程序在运行一个类(启动)文件后崩溃了。
在启动类之后,菜单类将运行,然后是MainActivity,然后是textplay ..
我正在为所有课程编写代码。看看你能否找到错误的地方
Splash class
package com.example.testing;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
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.sound);
ourSong.start();
Thread timer=new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint=
new Intent("com.example.testing.Menu");
startActivity(openStartingPoint);
}
}
};timer.start();
}
protected void onPause(){
super.onPause();
ourSong.release();
finish();
}
}
菜单类
package com.example.testing;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity {
String classes[]={"MainActivity","TextPlay","example2","example3","example4","example5"};
@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.testing."+ cheese);
Intent ourIntent=new Intent(Menu.this , ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MainActivity Class
package com.example.testing;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add,sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button) findViewById(R.id.bAdd);
sub=(Button) findViewById(R.id.bSub);
display=(TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
display.setText("Your Total is "+ counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your Total is "+counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
TextPlay Class
package com.example.testing;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class TextPlay extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button chkCmd = (Button) findViewById (R.id.bResults);
final ToggleButton passTog = (ToggleButton) findViewById (R.id.tbPassword);
EditText input =(EditText) findViewById (R.id.etCommands);
TextView display = (TextView) findViewById (R.id.tvResults);
passTog.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(passTog.isChecked()){
}
else{
}
}
});
}
}
答案 0 :(得分:1)
Intent openStartingPoint=new Intent("com.example.testing.Menu");
而不是使用 -
Intent openStartingPoint=new Intent(Splash.this,Menu.class);
希望这有帮助。
答案 1 :(得分:0)
试试这个..
可能会在android.content.ActivityNotFoundException
Intent openStartingPoint=new Intent("com.example.testing.Menu");
finally{
Intent openStartingPoint=new Intent(Splash.this,Menu.class);
startActivity(openStartingPoint);
}
更改您的最终,如下所示
{{1}}