移动到另一个活动时Android应用程序崩溃

时间:2014-03-25 16:19:14

标签: java android eclipse android-activity

嘿我试图用启动画面创建一个简单的Android应用程序然后转移到主要活动,但每次应用程序完成启动并移动到其他活动时它会崩溃。

ps:当我尝试删除启动时,MainActivity正常启动而没有错误

我的 MainActivity.java

package com.example.andava;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity  {

    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.bttnAdd);
        sub = (Button) findViewById(R.id.bttnSub);
        display = (TextView) findViewById(R.id.displaycount);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText(""+counter);
            }
        });
        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter-=1;
                display.setText(""+counter);
            }
        });


    }


}

Splash.java

package com.example.andava;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Splash extends Activity {

    MediaPlayer introSong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        introSong = MediaPlayer.create(Splash.this, R.raw.introsong);
        introSong.start();

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

        };
        timer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        introSong.release();
        finish();
    }

}

清单:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.andava.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>
        <activity
            android:name="com.example.andava.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

提前致谢..

3 个答案:

答案 0 :(得分:2)

你需要

Intent openMainAct = new Intent(CurrentActivity.this, MainActivity.class);
startActivity(openMainAct);

而不是

  Intent openMainAct = new Intent("com.example.andava.MainActivity");
  startActivity(openMainAct);

答案 1 :(得分:1)

按以下方式启动MainActivity ...

Intent openMainAct = new Intent(Splash.this, MainActivity.class);
startActivity(openMainAct);

而不是像这样...

Intent openMainAct = new Intent("com.example.andava.MainActivity");
startActivity(openMainAct);

还可以通过更改Manifest声明来更改您的MainActivity文件...

   <activity
        android:name="com.example.andava.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

到此......

   <activity
        android:name="com.example.andava.MainActivity"
        android:label="@string/app_name" >
    </activity>

答案 2 :(得分:1)

my Code for the App is given below:-

package com.android.kadin.kadin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                Intent i = new Intent(MainActivity.this,HomeScreen.class);
                startActivity(i);
                finish();
            }
        },3*1000);
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
    }
}




package com.android.kadin.kadin;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HomeScreen extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        ListView drawerList = (ListView)findViewById(R.id.drawer);

        String[] drawerItemList = new String[]{
               "الصفحةالرئيسية",
                "االملف الشخصي",
                "الاعلانات المفضلة"
        };

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,drawerItemList);

        drawerList.setAdapter(arrayAdapter);
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
    }

}


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.NoActionBar" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".HomeScreen"
            android:label="Kadin">
        </activity>


    </application>

</manifest>



<?xml version="1.0" encoding="utf-8"?>

<DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:layout_height="match_parent">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/store_icon" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:text="مشغل و اتيليه"
        android:textColor="#000EAF"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />


    <ListMenuItemView
        android:id="@+id/menuItemView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="3"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">


        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton2"
            android:src="@drawable/cat_1"
            android:background="#FFFFFF"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton3"
            android:background="#FFFFFF"
            android:src="@drawable/cat_2"
/>

        <ImageButton
            android:layout_width="wrap_content"
            android:background="#FFFFFF"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton4"
            android:src="@drawable/cat_3"/>
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="20dp"
    android:weightSum="3"
    android:layout_marginLeft="20dp"
    android:layout_gravity="bottom">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton5"
            android:background="#FFFFFF"
            android:src="@drawable/cat_4"
            android:layout_weight=".2"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton6"
            android:src="@drawable/cat_5"
            android:background="#FFFFFF"
            android:layout_weight=".2"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton7"
            android:src="@drawable/cat_6"
            android:background="#FFFFFF"
            android:layout_weight=".2"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="3"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton8"
            android:src="@drawable/cat_7"
            android:background="#FFFFFF"
            android:layout_weight=".2"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton9"
            android:src="@drawable/cat_8"
            android:background="#FFFFFF"
            android:layout_weight=".2"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton10"
            android:src="@drawable/cat_9"
            android:background="#FFFFFF"
            android:layout_weight=".2"/>
    </LinearLayout>    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="3"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton11"
        android:src="@drawable/cat_10"
        android:background="#FFFFFF"
        android:layout_weight=".2"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton12"
        android:src="@drawable/cat_11"
        android:background="#FFFFFF"
        android:layout_weight=".2"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton13"
        android:src="@drawable/cat_12"
        android:background="#FFFFFF"
        android:layout_weight=".2"/>
</LinearLayout>
</LinearLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />

</DrawerLayout>



<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="#FFFFFF"
    android:gravity="center_vertical|center_horizontal">

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/logo_icon"
        android:id="@+id/kadin_logo"
        />

</RelativeLayout>