【问题标题】:Android: Why isnt't the second activity running?Android:为什么第二个活动没有运行?
【发布时间】:2012-09-11 03:41:08
【问题描述】:

我正在制作我的第一个 Android 应用程序。我正在尝试从另一个名为AskReport 的活动开始一个名为SendMessage 的活动。我在 SO 上看到过类似的问题,但我找不到与我的问题相匹配的问题。

AskReport活动代码如下:

public class AskReport extends Activity
{
    Button done;
    RadioButton checkedRadioButton;
    RadioGroup group;
    EditText email, info;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ask_report);
        done = (Button) findViewById(R.id.Report);
        group = (RadioGroup) findViewById(R.id.radioGroup1);
        checkedRadioButton = (RadioButton) findViewById(group.getCheckedRadioButtonId());
        info = (EditText) findViewById(R.id.Info);
        email = (EditText) findViewById(R.id.Email);
        }

    public void reportClick(View view){
        Intent intent = new Intent(this, SendMessage.class);
/* Line1 */ intent.putExtra("sender_email", email.getText());      
/* Line2 */ intent.putExtra("additional_info", info.getText());    
/* Line3 */ intent.putExtra("checked_button",checkedRadioButton.getText()); 
        startActivity(intent);
    }
}

活动SendMessage的代码如下:

    public class SendMessage extends Activity
    {
    String message = "";
    EditText message_to_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_message);
        message_to_send = (EditText) findViewById(R.id.message_to_send);
        Intent intent = getIntent();
        message += intent.getStringExtra("sender_email");
        message += intent.getStringExtra("additional_info");
        message += intent.getStringExtra("checked_button");
        message_to_send.setText(message);
        }
}   

一旦我注释掉AskReport活动中标记的三行,SendMessage活动就被成功调用了,虽然出现的文字是nullnullnull(可以理解)。

我已在清单中正确标记了活动条目。

另外,我还在这里给出了出现的 logcat 消息:

09-10 15:12:57.555: E/AndroidRuntime(1311): FATAL EXCEPTION: main
09-10 15:12:57.555: E/AndroidRuntime(1311): java.lang.IllegalStateException: Could not execute method of the activity
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.view.View$1.onClick(View.java:3591)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.view.View.performClick(View.java:4084)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.view.View$PerformClick.run(View.java:16966)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.os.Handler.handleCallback(Handler.java:615)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.os.Looper.loop(Looper.java:137)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at java.lang.reflect.Method.invoke(Method.java:511)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at dalvik.system.NativeStart.main(Native Method)
09-10 15:12:57.555: E/AndroidRuntime(1311): Caused by: java.lang.reflect.InvocationTargetException
09-10 15:12:57.555: E/AndroidRuntime(1311):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at java.lang.reflect.Method.invoke(Method.java:511)
09-10 15:12:57.555: E/AndroidRuntime(1311):     at android.view.View$1.onClick(View.java:3586)
09-10 15:12:57.555: E/AndroidRuntime(1311):     ... 11 more
09-10 15:12:57.555: E/AndroidRuntime(1311): Caused by: java.lang.NullPointerException
09-10 15:12:57.555: E/AndroidRuntime(1311):     at com.example.safeworld.AskReport.reportClick(AskReport.java:44)
09-10 15:12:57.555: E/AndroidRuntime(1311):     ... 14 more
09-10 15:13:00.725: I/Process(1311): Sending signal. PID: 1311 SIG: 9

我也把android:onClick="XXXXXX"中的函数放在了对应的layout.xml中。

请帮我弄清楚这三行出了什么问题。感谢您的帮助!

编辑:

这是我的ask_report.xml

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/ask"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ask_report"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="24dp" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/road_accident"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:text="@string/road_accident" />

        <RadioButton
            android:id="@+id/fire"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:text="@string/fire" />

        <RadioButton
            android:id="@+id/theft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:text="@string/theft" />

        <RadioButton
            android:id="@+id/other"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="12dp"
            android:text="@string/other" />

    </RadioGroup>

    <TextView
        android:id="@+id/AddInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_detail" />

    <EditText
        android:id="@+id/Info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text" >
    </EditText>

    <TextView
        android:id="@+id/AskEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/enter_email"
        android:layout_marginTop="10dp" />

    <EditText
        android:id="@+id/Email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/Report"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/done"
        android:onClick="reportClick" />

</TableLayout>

【问题讨论】:

  • 您是否将reportClick 设置为您的Buttonandroid:onClick="reportClick" 并且向我们展示您的XML 代码。
  • 向我们展示您的ask_report.xml 文件。

标签: android android-intent android-activity illegalstateexception


【解决方案1】:

从堆栈跟踪来看,我会说这些行之一正在返回 null

checkedRadioButton = (RadioButton) findViewById(group.getCheckedRadioButtonId());
info = (EditText) findViewById(R.id.Info);
email = (EditText) findViewById(R.id.Email);

打印所有三个视图的值,如果其中一个为空,则检查 id 是否正确并且它们在布局中ask_report

【讨论】:

  • 是的,你是对的。 checkedRadioButton 的值结果为空。知道为什么会这样吗?
  • 表示group.getCheckedRadioButtonId()返回的值没有出现在ask_report的布局中。根据developer.android.com/reference/android/widget/…,如果未选择任何内容,它可能会返回-1,也许就是这样。
  • 尝试将 'android:checkedButton="@+id/road_accident"' 添加到 RadioGroup 以默认选择第一项
  • 如果这回答了您的问题,请标记为正确答案
【解决方案2】:

这可能是因为您从EditText 获取文本,请执行以下操作 -

intent.putExtra("sender_email", email.getText().toString());
intent.putExtra("additional_info", info.getText().toString());
intent.putExtra("checked_button",checkedRadioButton.getText().toString());

您忘记使用 .toString()EditText 获取文本

更新

好的,最好这样做以在 RadioGroup 中从 radioButton 获取文本 在您的 ButtononClick 方法中这样做 -

// Gets a reference to our radio group
// rBtnDigits is the name of our radio group (code not shown)
RadioGroup g = (RadioGroup) findViewById(R.id.radioGroup1); 

// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();

// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);

intent.putExtra("sender_email", email.getText().toString());
intent.putExtra("additional_info", info.getText().toString());

// Now you can get the text or whatever you want from the "selected" radio button
intent.putExtra("checked_button",b.getText());

【讨论】:

  • 它仍然无法正常工作... :( 还告诉我可以将getText()RadioButton 一起使用吗?
  • @shrey347 现在,您遇到了新的异常或只有相同的异常。
  • 同样的例外。虽然我已经弄清楚出了什么问题。这就是问题所在:checkedRadioButton = (RadioButton) findViewById(group.getCheckedRadioButtonId());checkedRadioButton 的值变成了 null。
  • 我已经尝试过更新..但问题是g.getCheckedRadioButtonId() 正在返回 null,即使我单击其中一个单选按钮也是如此。所以selected的值为null
  • @shrey347 对于,你必须使用onCheckedchangeListener 从当前选定的RadioButton 获取文本
【解决方案3】:

Android 只是在您定义

时为您实现 OnClickListener
android:onClick="someMethod"

【讨论】:

    【解决方案4】:

    好吧,我想我会自己回答这个问题,让所有阅读本文的人都知道答案。

    三个标记行的问题在于Line3,因为那是在checkedRadioButton 中具有null 值的行。

    有效的是:

    • 我将android:checkedButton="@+id/road_accident" 添加到我的RadioGroup 以默认选择RoadAccident
    • 接下来,我将onCheckedChangedListener 添加到RadioGroup,这成功给了我checkedRadioButton

    代码如下:

    group = (RadioGroup) findViewById(R.id.radioGroup1);
            group.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    checkedRadioButton = (RadioButton) group.findViewById(checkedId);
                }
            });
    
            checkedRadioButton = (RadioButton) group.findViewById(group.getCheckedRadioButtonId());
    

    现在 Line3 可以正常工作了。

    致谢: 感谢@marmor 和@SpK 的帮助。抱歉,我从你的两个答案中选了一半,所以无法选择正确的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 2021-10-16
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多