2016-12-04 77 views
2

我是新来的android编程。 我做了一个应用程序有2个活动(其中一个是发射其他正在寻求)。第二项活动由主要活动中的一个按钮启动。 虽然我这样做的应用程序崩溃。为什么第二项活动启动时应用程序崩溃?

Mainfest。

 <?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. 
+0

检查您的xml中的错误的大小或ID声明 –

回答

0

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

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

要修复它,请将进度显式转换为字符串:t1.setText(Integer.toString(s1.getProgress()));

+0

非常感谢!那样做了.. –

-1

试试这个:

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

希望它可以帮助!

+0

对不起!但该应用程序仍然崩溃 –