2016-07-11 249 views
-1

成功启动启动活动后,我的应用无法移至启动点的第二个活动。应用程序关闭时说:“不幸的是,新波士顿已停止工作。”Android应用无法在启动活动后启动活动

我附上相关文件。希望有人能帮助我。 StartingPoint.java

package com.example.thenewboston; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class StartingPoint extends Activity { 
    int counter; 
    Button add,sub; 
    TextView display; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_starting_point); 
     counter=0; 
     add=(Button)findViewById(R.id.addButton); 
     sub=(Button)findViewById(R.id.subButton); 
     display=(TextView) findViewById(R.id.display); 
     add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
      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.starting_point, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

Splash.java

package com.example.thenewboston; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class Splash extends Activity{ 

    @Override 
    public void onCreate(Bundle TV) { 
     // TODO Auto-generated method stub 
     super.onCreate(TV); 
     setContentView(R.layout.splash); 
     Thread timer = new Thread(){ 
      public void run(){ 
       try{ 
        sleep(5000); 
       }catch(InterruptedException e){ 
        e.printStackTrace(); 
       }finally{ 
        Intent openStatingPoint= new Intent("com.thenewboston.StartingPoint"); 
        startActivity(openStatingPoint); 

       }  

      } 
     }; 
     timer.start(); 
    } 
} 

的Manifest.xml

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".StartingPoint" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.example.thenewboston.StartingPoint" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Splash" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

添加logcat的的崩溃。 –

+2

@Sumeet Shah只需替换Intent openStatingPoint = new Intent(“com.thenewboston.StartingPoint”);以意向i =新的意图( 这 ,StartingPoint.class); – Nisarg

回答

1

尝试如下更改代码:

Intent openStatingPoint= new Intent(Splash.this,StartingPoint.class); 
       startActivity(openStatingPoint); 

说明:

这里使用的构造函数参数: 甲语境作为第一个参数(Splash.this被使用,因为活动类是背景信息的子类) 系统应传递意图的应用程序组件的类(在这种情况下,应该启动的活动,这里是StartingPoint.class

查看更多Build an Intent

+0

这是正确的@pRaNay然而,而不是倾销的代码,请解释你做了什么,以便未来的人也可以看到答案。 – basic

+0

@basic谢谢。我正在编辑,现在回答已编辑。 – pRaNaY

+0

现在我想再次upvote :) – basic

-1

检查的异常堆栈跟踪logcat的,也可能是很多原因。 和

+1

这应该是一个评论,并没有增加任何答案。 – basic

0

如下Thread类不上UI线程,这样,而不是使用主题类使用处理器类工作 -

Splash.java

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent openStatingPoint= new Intent(Splash.this,StartingPoint.class); // Use Explicit Intents 
      startActivity(openStatingPoint); 
      finish(); 
     } 
    }, 5000); //time in milliseconds