为什么应用程序在第二个活动启动时崩溃?

时间:2016-12-04 15:48:22

标签: android

我是Android编程新手。 我做了一个有2个活动的应用程序(一个是发射器,另一个是寻求)。第二个活动由主活动中的按钮启动。 当我这样做时,应用程序崩溃了。

主要节日。

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

    <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=".Seek">
            <intent-filter>
                <action android:name="com.a1.starklabs.myapplication.Seek" />

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

</manifest>
**Main_activity.xml**
        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:id="@+id/activity_main"
    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.a1.starklabs.myapplication.MainActivity">

    <ImageView
        android:layout_height="100dp"
        app:srcCompat="@mipmap/game"
        android:id="@+id/imageView3"
        android:scaleType="centerCrop"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:layout_width="100dp" />

    <Button
        android:text="Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        style="@android:style/Widget.Button"
        android:elevation="24dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        android:layout_marginTop="27dp"
        android:layout_below="@+id/imageView3"
        android:layout_centerHorizontal="true" />

    <TextView
        android:text="Other Activities:"
        android:layout_width="150dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp"
        android:id="@+id/textView"
        android:elevation="5dp"
        android:fontFamily="cursive"
        android:textColorLink="?attr/actionMenuTextColor"
        android:textColor="@android:color/holo_green_dark" />

    <Button
        android:text="SEEK"
        android:layout_below="@+id/textView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="12dp"
        android:id="@+id/button2"
        android:layout_height="33dp"
        android:layout_width="60dp" />

</RelativeLayout>

Main_activity.java

package com.a1.starklabs.myapplication;

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

public class MainActivity extends AppCompatActivity {
    public static ImageView i1;
    public static Button b1;
    private int img_index;
    int[] images={R.mipmap.game,R.mipmap.mustang};
    private static Button b_seek;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonClick();
        seek_button();
    }
    public void seek_button()
    {
       b_seek=(Button)findViewById(R.id.button2);
        b_seek.setOnClickListener(
                new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Intent i=new Intent("com.a1.starklabs.myapplication.Seek");
                        startActivity(i);
                    }
                }
        );
    }
    public void buttonClick()
    {
        i1=(ImageView)findViewById(R.id.imageView3);
        b1=(Button)findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                img_index++;
                img_index=img_index%images.length;
                i1.setImageResource(images[img_index]);
            }
        });
    }
}

activity_seek.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:id="@+id/activity_seek"
    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.a1.starklabs.myapplication.Seek">

    <SeekBar
        android:layout_marginTop="86dp"
        android:id="@+id/seekBar"
        android:layout_height="50dp"
        android:layout_width="300dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:text="TextView"
        android:layout_width="100dp"
        android:layout_below="@+id/seekBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:id="@+id/textView2"
        android:textSize="20dp"
        android:layout_height="30dp"
        android:textAppearance="@style/TextAppearance.AppCompat"
        style="@android:style/Widget.Material.TextView" />
</RelativeLayout>

seek.java

 package com.a1.starklabs.myapplication;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.SeekBar;
    import android.widget.TextView;

    public class Seek extends AppCompatActivity {
        private static SeekBar s1;
        private static TextView t1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_seek);
            seekbar();
        }
        public void seekbar(){
            s1=(SeekBar)findViewById(R.id.seekBar);
            t1=(TextView)findViewById(R.id.textView2);
            t1.setText(s1.getProgress());
            s1.setOnSeekBarChangeListener(
                    new SeekBar.OnSeekBarChangeListener() {
                        int prog_ress;
                        @Override
                        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                          prog_ress=i;
                            t1.setText(s1.getProgress());
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                            t1.setText(s1.getProgress());
                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            t1.setText(s1.getProgress());
                        }
                    }
            );
        }
    }

错误日志

    $ adb shell am start -n "com.a1.starklabs.myapplication/com.a1.starklabs.myapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Connected to process 2913 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/com.a1.starklabs.myapplication-2/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.a1.starklabs.myapplication, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.a1.starklabs.myapplication-2/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/ResourceType: No package identifier when getting value for resource number 0x00000000
D/AndroidRuntime: Shutting down VM


                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.a1.starklabs.myapplication, PID: 2913
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a1.starklabs.myapplication/com.a1.starklabs.myapplication.Seek}: android.content.res.Resources$NotFoundException: String resource ID #0x0
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
                      at android.content.res.Resources.getText(Resources.java:335)
                      at android.widget.TextView.setText(TextView.java:4555)
                      at com.a1.starklabs.myapplication.Seek.seekbar(Seek.java:36)
                      at com.a1.starklabs.myapplication.Seek.onCreate(Seek.java:27)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

2 个答案:

答案 0 :(得分:0)

t1.setText(s1.getProgress());可能是您遇到问题的原因。

我想你想把当前的进度设置为文本,但它会调用TextView.setText(int),它会尝试加载id等于给定进度的字符串资源。由于它在开始时为0,因此它会尝试加载不存在的资源0。

要解决此问题,请将进度显式转换为字符串:t1.setText(Integer.toString(s1.getProgress()));

答案 1 :(得分:-1)

试试这个:

Intent i=new Intent(MainActivity.this, Seek.class);
startActivity(i);

希望它有所帮助!!