Android App因两项活动而崩溃

时间:2014-02-20 23:44:09

标签: java android

我遵循这个tutorial并且我遵循所有步骤但是当我在我的设备上启动应用程序时它说它崩溃了。所以我想首先制作MainActivity,然后如果我点击一个Action栏菜单,它将加载第二个活动(upload.java)蚂蚁它将显示upload_layout.I'm培训制作Android的应用程序所以我不好编程;)

这是我的代码:     MainActivity.java

 package com.example.tosseapp.app;

  import android.content.Intent;
  import android.content.res.AssetManager;
  import android.content.res.Resources;
  import android.media.AudioManager;
  import android.media.MediaPlayer;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
   import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.*;
  import java.io.IOException;

  import java.io.FileInputStream;
  import java.io.IOException;

  public class MainActivity extends ActionBarActivity  {

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

   }


     @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;
   }

   @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);
    }

    public void  tosse1(View v)  {
    Button one = (Button)this.findViewById(R.id.button1);
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.toxxe);
    one.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Resume the music player

            mp.start();
        }
    });


   }

    public void  tosse2(View v)  {
    Button two = (Button)this.findViewById(R.id.button2);
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.tozze);
    two.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Resume the music player

            mp.start();
        }
    });


  }

    public void  tosse3(View v)  {
    Button two = (Button)this.findViewById(R.id.button3);
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossesei);
    two.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Resume the music player

            mp.start();
        }
    });


    }

     public void  tosse4(View v)  {
      Button two = (Button)this.findViewById(R.id.button4);
     final MediaPlayer mp = MediaPlayer.create(this, R.raw.todde);
    two.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Resume the music player

            mp.start();
        }
    });


   }

     public void  tosse5(View v)  {
     Button two = (Button)this.findViewById(R.id.button5);
      final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossequattro);
      two.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Resume the music player

            mp.start();
        }
    });


   }

   public void  tosse6(View v)  {
    Button two = (Button)this.findViewById(R.id.button6);
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossecinque);
    two.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Resume the music player

            mp.start();
        }
    });


}
public void upload(View view) {
Intent intent = new Intent(this, upload.class);
startActivity(intent);


}
}

upload.java:

package com.example.tosseapp.app;

import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

 public class upload extends ActionBarActivity {

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

   }


   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.upload, menu);
    return true;
  }

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}


}

AndroidManifest:

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <activity
        android:name="com.example.tosseapp.app.upload"
        android:label="@string/app_name"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.tosseapp.app.MainActivity" />


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

main.xml(菜单)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tosseapp.app.MainActivity" >

<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:onClick="upload"
    android:orderInCategory="100"
    app:showAsAction="never" />
</menu>

2 个答案:

答案 0 :(得分:1)

您需要在MainActivity

中声明AndroidManifest.xml
 <application
android:allowBackup="true"

 <!-- other things -->

 <activity
  android:name="com.example.tosseapp.app.MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />

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

<!-- other activities, services, receivers & close application -->

答案 1 :(得分:0)

您可能需要将MainActivity和upload.java添加到清单中,如下所示:

<application

    ...

    <activity
        android:name="com.example.tosseapp.app.MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="com.example.tosseapp.app.upload"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.tosseapp.app.MainActivity" >

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.tosseapp.app.MainActivity" />
    </activity>

</application>