gpt4 book ai didi

android - 为什么启动第二个 Activity 时应用会崩溃?

转载 作者:行者123 更新时间:2023-11-29 14:13:15 25 4
gpt4 key购买 nike

我是 android 编程的新手。我制作了一个有 2 个 Activity 的应用程序(一个是启动器,另一个是正在寻找的)。第二个 Activity 由主 Activity 中的按钮启动。当我这样做时,应用程序崩溃了。

主要节日。

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

最佳答案

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

我猜您想将当前进度设置为文本,但它调用 TextView.setText(int) 尝试加载 ID 等于给定进度的字符串资源。因为它在开始时是 0,所以它会尝试加载不存在的资源 0。

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

关于android - 为什么启动第二个 Activity 时应用会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40960409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com