当我点击按钮时,我的应用程序崩溃,这将带我另一个Java,Android Studio上的活动

时间:2016-04-26 14:47:57

标签: java android

首先抱歉我的英语不好,希望你能理解我。 我在Android Studio上制作Android应用程序。我在这里是因为我开始第三次活动时我的应用程序崩溃了。

好吧,我有三个活动。从第一个开始到第二个从第二个到第一个效果很好,但是当我尝试从第二个进入第三个时,我按下按钮进入第三个活动,我的应用程序崩溃,它说“不幸的是,Project Saya已经停止。“

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.missmagius.projectsaya.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Merhaba, Project Saya'ya hoşgeldiniz! Başlamadan önce sorun yaşamamak için lütfen talimatları gözden geçirin. İyi eğlenceler!"
        android:id="@+id/textView"
        android:enabled="false"
        android:textColor="@color/abc_input_method_navigation_guard" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Talimatlar"
        android:id="@+id/buttonActOne"
        android:layout_marginTop="29dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

这是MainActivity.java

    package com.missmagius.projectsaya;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button btnOne =(Button)findViewById(R.id.buttonActOne);
        btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getApplicationContext(),ActivityTwo.class);
                startActivity(intent);
            }
        });


    }
}

这是activity_two.xml

package com.missmagius.projectsaya;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button btnOne =(Button)findViewById(R.id.buttonActOne);
        btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getApplicationContext(),ActivityTwo.class);
                startActivity(intent);
            }
        });


    }
}

这是ActivityTwo.java

package com.missmagius.projectsaya;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ActivityTwo extends AppCompatActivity {

    Button btnTwo;
    Button getBtnTwo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

        Button btnTwo =(Button)findViewById(R.id.buttonActTwo);
        Button btnThree=(Button)findViewById(R.id.buttonActThree);

        btnTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(ActivityTwo.this,MainActivity.class);
                startActivity(intent2);
            }
        });

        btnThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent4 = new Intent(ActivityTwo.this,SpeechActivity.class);
                startActivity(intent4);
            }
        });
    }

}

这是activity_speech.xml(第三项活动)

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.missmagius.projectsaya.SpeechActivity">

    <TextView

        android:id="@+id/txtKGiris"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:textSize="26sp"
    android:textStyle="normal"
    />

    <LinearLayout

        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="60dp"
    android:gravity="center"
    android:orientation="vertical" >



    <ImageButton

        android:id="@+id/btnKonus"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@null"
        android:src="@+id/micc" />


    </LinearLayout>

    <EditText

        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
        android:text="@string/tap_on_mic"
    android:textSize="15dp"
    android:textStyle="normal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

这是SpeechActivity.java

package com.missmagius.projectsaya;


import android.content.Intent;
import android.speech.RecognizerIntent;
import android.os.Bundle;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;




public class SpeechActivity extends Activity {

    private TextView txtKGiris;
    private ImageButton btnKonus;
    private final int REQ_CODE_SPEECH_INPUT = 100;

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

        txtKGiris = (TextView) findViewById(R.id.txtKGiris);
        btnKonus = (ImageButton) findViewById(R.id.btnKonus);

        // action bar’ı gizle
        getActionBar().hide();

        btnKonus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                promptSpeechInput();

            }

        });
    }
    /**

     * Google konuşma girişi diyalogunu gösterme

     * */

    private void promptSpeechInput() {
        Intent intent3 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent3.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent3.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent3.putExtra(RecognizerIntent.EXTRA_PROMPT,
                getString(R.string.speech_prompt));

        try {

            startActivityForResult(intent3, REQ_CODE_SPEECH_INPUT);

        } catch (ActivityNotFoundException a) {

            Toast.makeText(getApplicationContext(),

                    getString(R.string.speech_not_supported),

                    Toast.LENGTH_SHORT).show();

        }

    }

    /**

     * Konuşma girişi kabulü.

     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT: {
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    txtKGiris.setText(result.get(0));
                }
                break;
            }
        }
    }
    @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;
    }

}

这是AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

这是Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Project Saya</string>
    <string name="action_settings">Ayarlar</string>
    <string name="hello_world">Merhaba Dünya!</string>
    <string name="speech_prompt">Bana bir şeyler söyle.</string>
    <string name="speech_not_supported">Çok Üzgünüm! Senin sürümün buna yetmiyor…</string>
    <string name="tap_on_mic">Mikrofona basıp konuşabilirsin.</string>
</resources>

我的错误日志:

04-27 09:55:26.880 6606-6606/com.missmagius.projectsaya E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.missmagius.projectsaya, PID: 6606
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.missmagius.projectsaya/com.missmagius.projectsaya.SpeechActivity}: android.view.InflateException: Binary XML file line #37: Error inflating class ImageButton
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                           Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class ImageButton
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                              at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
                                                                              at com.missmagius.projectsaya.SpeechActivity.onCreate(SpeechActivity.java:27)
                                                                              at android.app.Activity.performCreate(Activity.java:5933)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:135) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
                                                                           Caused by: android.content.res.Resources$NotFoundException: Resource "com.missmagius.projectsaya:id/micc" (7f0c0054)  is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c0054}
                                                                              at android.content.res.Resources.loadDrawableForCookie(Resources.java:2390)
                                                                              at android.content.res.Resources.loadDrawable(Resources.java:2330)
                                                                              at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
                                                                              at android.widget.ImageView.<init>(ImageView.java:146)
                                                                              at android.widget.ImageButton.<init>(ImageButton.java:86)
                                                                              at android.widget.ImageButton.<init>(ImageButton.java:82)
                                                                              at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:57)
                                                                              at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:53)
                                                                              at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:114)
                                                                              at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                                              at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
                                                                              at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                              at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
                                                                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) 
                                                                              at com.missmagius.projectsaya.SpeechActivity.onCreate(SpeechActivity.java:27) 
                                                                              at android.app.Activity.performCreate(Activity.java:5933) 
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:135) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

就是这样。我不知道我哪弄错了。我需要你的帮助。感谢。

4 个答案:

答案 0 :(得分:2)

android:src="@+id/micc"更改为android:src="@drawable/micc" />中的ImageButton。它导致了这个问题。

答案 1 :(得分:1)

更改以下代码

**

Intent intent=new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(intent);

**

致:

**

Intent intent=new Intent(MainActivity.this,ActivityTwo.class);
startActivity(intent);

**

答案 2 :(得分:0)

除非Intent包含FLAG_ACTIVITY_NEW_TASK标志,否则不允许应用程序上下文启动活动。

只需改变(至少)

Intent intent=new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(intent);

致:

Intent intent=new Intent(MainActivity.this,ActivityTwo.class);
startActivity(intent);

MainActivity

中的clickListener内

答案 3 :(得分:0)

您错过了完成旧活动,只需在finish()

之后添加startActivity()即可

所以这是代码:

startActivity(i);
finish();