Android 中 Object toString() 命令中的异常
好吧,我已经搜索了大量关于同一件事的其他问题,但不知何故,这些解决方案都没有解决我的问题。所以这是我的问题,我的 Android 应用程序一旦必须将文本从 EditText 获取到字符串就会崩溃。我在这里有我的“主要”活动:
package ign.test.fre;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class testfre extends Activity {
public static String cv = "";
public static String cm = "";
public static String cz = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//OTHER DEFINITIONS
EditText ev = (EditText) findViewById(R.id.v);
final Spinner em =(Spinner) findViewById(R.id.m);
final Spinner ez = (Spinner) findViewById(R.id.z);
final Button ee = (Button) findViewById(R.id.GO);
//LOTS OF SPINNER STUFF
ee.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
if(initialsetup == false){
enter = true;
run();
}
}
});
ev.setOnKeyListener(new AdapterView.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
int key = event.getKeyCode();
if(key == KeyEvent.KEYCODE_ENTER){
enter = true;
}
return false;
}
});
initialsetup = false;
}
public void run(){
if(enter == true){
Calc.Test0();
Calc.Test1_se();
Calc.Test2_etre();
Calc.Test3_irr();
if(Test3_irr == false){
//MORE CODE
textview6.setText("" + ausgabezeile[5]);
}
}
}
我的 Calc 类如下所示: 包 ign.test.fre;
public class Calc {
public static void Test0(){
testfre.cv = testfre.ev.getText().toString();
testfre.cm = Data.m()[testfre.em.getSelectedItemPosition()];
testfre.cz = Data.zeiten()[testfre.ez.getSelectedItemPosition()];
testfre.cv = testfre.cv.toLowerCase();
testfre.per1.setText(Data.personen()[0]);
//...
}
public static void Test1_se(){
//MORE CODE
Test0() 的前 3 行有问题。哪一行在第一行或者它们在代码中的位置并不重要,它总是它们。即使将它们移动到 run() 或 onClick() 也没有改变任何东西。
我的 xml 布局如下所示:
<android:weightSum="1">
<EditText android:text="Enter Text" android:id="@+id/v" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true"></EditText>
<Spinner android:layout_width="100dp" android:id="@+id/m" android:layout_height="wrap_content" android:layout_below="@+id/v" android:layout_alignParentLeft="true"></Spinner>
<Button android:id="@+id/GO" android:layout_width="70dp" android:layout_height="wrap_content" android:text="GO" android:layout_alignTop="@+id/z" android:layout_alignParentRight="true"></Button>
<Spinner android:layout_width="150dp" android:id="@+id/z" android:layout_height="wrap_content" android:layout_below="@+id/v" android:layout_toRightOf="@+id/m"></Spinner>
我的 Logcat 条目如下所示(ignian.lomoko.french 与 ign.test.fre 相同,依此类推):
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at dalvik.system.NativeStart.main(Native Method)
10-25 11:58:41.038: WARN/ActivityManager(61): Force finishing activity ignian.lomoko.french/.lomokofrench
10-25 11:58:41.558: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{405c6018 ignian.lomoko.french/.lomokofrench}
10-25 11:58:43.698: INFO/Process(316): Sending signal. PID: 316 SIG: 9
10-25 11:58:43.708: INFO/ActivityManager(61): Process ignian.lomoko.french (pid 316) has died.
10-25 11:58:43.708: INFO/WindowManager(61): WIN DEATH: Window{40691bd8 ignian.lomoko.french/ignian.lomoko.french.lomokofrench paused=false}
10-25 11:58:43.809: WARN/InputManagerService(61): Got RemoteException sending setActive(false) notification to pid 316 uid 10034
10-25 11:58:52.691: WARN/ActivityManager(61): Activity destroy timeout for HistoryRecord{405c6018 ignian.lomoko.french/.lomokofrench}
感谢您的任何帮助,因为我真的找不到任何解决方案其他任何地方。到目前为止,我在其他人的帮助下理解的问题是我无法从我的“Calc”类访问我的变量。我想我正在寻找的是一种公开 Spinners 和 EditText 的方法。
好吧,我遵循了 dten 的解决方案,现在它可以工作了!
Ok, so I have searched through a massive amount of other questions about the same thing but somehow none of those solutions have worked for my problem. So here is my problem, my Android-App crashes as soon as it has to get the text from an EditText to a String. I have my "Main"-Activity here:
package ign.test.fre;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class testfre extends Activity {
public static String cv = "";
public static String cm = "";
public static String cz = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//OTHER DEFINITIONS
EditText ev = (EditText) findViewById(R.id.v);
final Spinner em =(Spinner) findViewById(R.id.m);
final Spinner ez = (Spinner) findViewById(R.id.z);
final Button ee = (Button) findViewById(R.id.GO);
//LOTS OF SPINNER STUFF
ee.setOnClickListener(new AdapterView.OnClickListener() {
public void onClick(View arg0) {
if(initialsetup == false){
enter = true;
run();
}
}
});
ev.setOnKeyListener(new AdapterView.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
int key = event.getKeyCode();
if(key == KeyEvent.KEYCODE_ENTER){
enter = true;
}
return false;
}
});
initialsetup = false;
}
public void run(){
if(enter == true){
Calc.Test0();
Calc.Test1_se();
Calc.Test2_etre();
Calc.Test3_irr();
if(Test3_irr == false){
//MORE CODE
textview6.setText("" + ausgabezeile[5]);
}
}
}
My Calc-Class looks like this:
package ign.test.fre;
public class Calc {
public static void Test0(){
testfre.cv = testfre.ev.getText().toString();
testfre.cm = Data.m()[testfre.em.getSelectedItemPosition()];
testfre.cz = Data.zeiten()[testfre.ez.getSelectedItemPosition()];
testfre.cv = testfre.cv.toLowerCase();
testfre.per1.setText(Data.personen()[0]);
//...
}
public static void Test1_se(){
//MORE CODE
and it's in the first 3 lines of the Test0() that there's a problem. It dosen't matter which of those lines is first or where in the code they are it's always them. Even moving them to the run() or onClick() didn't change anything.
My xml-layout looks like this:
<android:weightSum="1">
<EditText android:text="Enter Text" android:id="@+id/v" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true"></EditText>
<Spinner android:layout_width="100dp" android:id="@+id/m" android:layout_height="wrap_content" android:layout_below="@+id/v" android:layout_alignParentLeft="true"></Spinner>
<Button android:id="@+id/GO" android:layout_width="70dp" android:layout_height="wrap_content" android:text="GO" android:layout_alignTop="@+id/z" android:layout_alignParentRight="true"></Button>
<Spinner android:layout_width="150dp" android:id="@+id/z" android:layout_height="wrap_content" android:layout_below="@+id/v" android:layout_toRightOf="@+id/m"></Spinner>
My Logcat-entry looks like this (ignian.lomoko.french is the same as ign.test.fre and so forth):
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-25 11:58:41.028: ERROR/AndroidRuntime(316): at dalvik.system.NativeStart.main(Native Method)
10-25 11:58:41.038: WARN/ActivityManager(61): Force finishing activity ignian.lomoko.french/.lomokofrench
10-25 11:58:41.558: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{405c6018 ignian.lomoko.french/.lomokofrench}
10-25 11:58:43.698: INFO/Process(316): Sending signal. PID: 316 SIG: 9
10-25 11:58:43.708: INFO/ActivityManager(61): Process ignian.lomoko.french (pid 316) has died.
10-25 11:58:43.708: INFO/WindowManager(61): WIN DEATH: Window{40691bd8 ignian.lomoko.french/ignian.lomoko.french.lomokofrench paused=false}
10-25 11:58:43.809: WARN/InputManagerService(61): Got RemoteException sending setActive(false) notification to pid 316 uid 10034
10-25 11:58:52.691: WARN/ActivityManager(61): Activity destroy timeout for HistoryRecord{405c6018 ignian.lomoko.french/.lomokofrench}
Thanks for any help upfront as I can seriously not find any kind of solution anywhere else. The problem as I understand it upto now with the help of others is that I can't access my variables from my "Calc"-Class. What I'm looking for I guess is a way to make the Spinners and EditText public.
Well I followed the solution from dten and now it works!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在您的代码中看到的一个问题是您声明了对象两次。
在您进行的活动中看到您
在 Activity 的 onCreate 中再次执行相同的操作:
尝试将上面的代码替换为 testfre Activity 的 onCreate 中的以下代码:
One problem i see in your code is you are declaring the objects twice.
See in your activity you took
and inside onCreate of Activity you are again doing the same thing :
Try replacing above code with below one in onCreate of testfre Activity :
这段代码有很多问题,很难知道从哪里开始,
然后你在一个方法中为一些静态变量创建重复的变量,然后尝试在外部引用
你正在引用 testfre 中的一个静态变量(它应该有一个大写字母代表name),你并不意味着是静态的,但你似乎已经设置为静态,因为你的 Calc 方法是静态的,
你在你的 calc 类中引用了 testfre 中的东西,这些东西甚至不存在于 testfre 中,
这是一个疯狂的 行为混乱
为什么你要设置一个按键侦听器而不是
在静态类的活动中的编辑文本上使用按键设置文本?
请为变量提供正确的名称,而不仅仅是 1 个字母
Test3_irr 和 textview6 是什么?
...更多内容
请添加
到 XML 中的按钮(这将在活动中调用 onClick)
您的 Calc 类应该能够获取您输入的单词和选择框索引(您似乎正在获取值这与其他地方有关)并返回字符串以填充文本视图
重要的是您的 Calc 类应该对您的 Activity 一无所知
There are SO many problems with this code it's hard to know where to start
There are statics you then make duplicate variables for in a method but then try reference outside
You're referencing one static from your testfre (which should have a capitcal letter for a name) which you don't mean to be static but you seem to have set to be static because your Calc methods are static
you're referencing things from testfre in your calc class that dont'# even exist in testfre
it's one insane mess
why do you set a keylistener rathen than use on keydown
setting text on your edittext that is in your activity from a static class?
please give variables proper names not just 1 letter
what is Test3_irr and textview6?
... more to come
please add
to your button in the XML (this will call onClick in the activity)
Your Calc class should be able to take the word you've typed and the select box index (you seem to be getting the value this relates to from somewhere else) and return you the string to fill the textview
The important thing is your Calc class should know NOTHING about your Activity