gpt4 book ai didi

java - 为什么运行应用程序时出现 fatal error ?

转载 作者:行者123 更新时间:2023-12-01 06:16:02 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,其中当按下按钮时,在本例中按钮为 b1,调用 toast,它从输入中获取数字并从文本字段中烘烤字符串文本。我在 logcat 下面遇到了一些错误.

MainActivity.java

package com.x.losey;    
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.net.NetworkInterface;

public class MainActivity extends Activity {

@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);
final Button b = (Button) (findViewById(R.id.Button2));
final Button b1 = (Button) (findViewById(R.id.button));
final EditText txt = (EditText) (findViewById(R.id.editText));
final EditText num = (EditText) (findViewById(R.id.editText2));
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean a1 ;
Intent first = new Intent(MainActivity.this,second.class);
Toast.makeText(MainActivity.this, "OK" , Toast.LENGTH_SHORT).show();
startActivity(first);
}

});
b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
int i;
int n;
n = Integer.parseInt(String.valueOf(num));

while(n!=0) {
String ab = txt.getText().toString();
Toast.makeText(MainActivity.this, ab, Toast.LENGTH_SHORT).show();
n--;
}
}
});
return true;
}

}

activity_main.xml

<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:gravity="right"
android:orientation="vertical"
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=".MainActivity" >

<!--<TextView-->
<!--android:id="@+id/txtview1"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:layout_gravity="top"-->
<!--android:text="@string/LoseY"-->
<!--android:textSize="25sp"-->
<!--android:textStyle="bold" />-->

<Button
android:id="@+id/Button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/desbtn"
android:onClick=""
android:text="@string/btn1"
android:textColor="#FFFF"
android:textSize="50sp"
android:typeface="monospace" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"

android:layout_gravity="top"
android:hint="@string/enter" android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:height="50dp" android:textSize="28sp"/>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/Button2"
android:layout_alignLeft="@+id/Button2"
android:layout_alignParentEnd="true"
android:layout_marginBottom="40dp"
android:background="@drawable/desbtn"
android:fontFamily="@string/btn1"
android:gravity="center"
android:height="79dp"
android:text="@string/showMe"
android:textColor="#ffff9b80"
android:textSize="35sp"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:hint="@string/input"
android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:layout_marginTop="46dp"
android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText"
/>

</RelativeLayout>

当单击按钮 = b1 时,我会得到 toast ,但只有 4 或 2 次,当我删除 n 时,我会添加一个值,然后得到正确的输出。

这个 Logcat 错误是什么?

07-04 21:53:56.709  17773-17773/com.x.losey W/ResourceType﹕ No package identifier when getting value for resource number 0x0000005d
07-04 21:53:56.710 17773-17773/com.x.losey D/AndroidRuntime﹕ Shutting down VM
07-04 21:53:56.710 17773-17773/com.x.losey W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4165ed40)
07-04 21:53:56.712 17773-17773/com.x.losey E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.x.losey, PID: 17773
android.content.res.Resources$NotFoundException: String resource ID #0x5d
at android.content.res.Resources.getText(Resources.java:244)
at android.widget.Toast.makeText(Toast.java:268)
at com.x.losey.MainActivity$2.onClick(MainActivity.java:65)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
07-04 21:53:58.084 17773-17773/com.x.losey I/Process﹕ Sending signal. PID: 17773 SIG: 9
07-04 21:53:59.709 18458-18458/com.x.losey I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.24.00.01
Build Date: 12/11/13 Wed
Local Branch: 8226workspace
Remote Branch:
Local Patches:
Reconstruct Branch:
07-04 21:53:59.735 18458-18458/com.x.losey D/OpenGLRenderer﹕ Enabling debug mode 0
07-04 21:53:59.844 18458-18458/com.x.losey E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-04 21:53:59.844 18458-18458/com.x.losey E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-04 22:00:06.784 18458-18458/com.x.losey W/ResourceType﹕ No package identifier when getting value for resource number 0x0000005d
07-04 22:00:06.784 18458-18458/com.x.losey D/AndroidRuntime﹕ Shutting down VM
07-04 22:00:06.784 18458-18458/com.x.losey W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4165ed40)
07-04 22:00:06.788 18458-18458/com.x.losey E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.x.losey, PID: 18458
android.content.res.Resources$NotFoundException: String resource ID #0x5d
at android.content.res.Resources.getText(Resources.java:244)
at android.widget.Toast.makeText(Toast.java:268)
at com.x.losey.MainActivity$2.onClick(MainActivity.java:65)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invo`enter code here`ke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
07-04 22:00:08.438 18458-18458/com.x.losey I/Process﹕ Sending signal. PID: 18458 SIG: 9

最佳答案

将 MainActivity 更改为:

private Button b;
private Button b1;
private EditText txt;
private EditText num;

public class MainActivity extends Activity {

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

b = (Button) findViewById(R.id.Button2);
b1 = (Button) findViewById(R.id.button);
txt = (EditText) findViewById(R.id.editText);
num = (EditText) findViewById(R.id.editText2);

b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean a1 ;
Intent first = new Intent(MainActivity.this,second.class);

Toast.makeText(MainActivity.this, "OK" , Toast.LENGTH_SHORT).show();

startActivity(first);

}


});
b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
int i;
int n;
n = Integer.parseInt(String.valueOf(num));

while(n!=0)
{
String ab = txt.getText().toString();

Toast.makeText(MainActivity.this, ab, Toast.LENGTH_SHORT).show();

n--;
}


}

});
}

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

}

}

关于java - 为什么运行应用程序时出现 fatal error ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24578523/

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