启动后,Android应用无法启动活动

时间:2016-07-11 13:02:18

标签: java android

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

我正在附上相关文件。希望有人可以帮助我。 的 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>

3 个答案:

答案 0 :(得分:1)

尝试更改您的代码,如下所示:

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

说明:

此处使用的构造函数采用两个参数: 上下文作为其第一个参数(使用 Splash.this ,因为Activity类是Context的子类) 系统应向其传递Intent的应用程序组件的类(在本例中,应该启动的活动,此处为 StartingPoint.class

查看更多Build an Intent

答案 1 :(得分:0)

线程类不适用于 UI线程,因此不使用线程类,而是使用 Handler 类,如下所示 -

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 

答案 2 :(得分:-1)

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