0

我正在制作我的第一个android应用程序。我正尝试从另一个名为AskReport的活动开始称为SendMessage的活动。我已经在SO上看到过类似的问题,但没有找到符合我的问题的问题。Android:为什么第二个活动没有运行?

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> 
+1

您是否将'reportClick'设置为'Button'的'android:onClick =“reportClick”'然后向我们展示您的'XML'代码。 – Praveenkumar

+0

是的,我已经做到了。 – akaHuman

+0

向我们展示您的'ask_report.xml'文件。 – Praveenkumar

回答

0

嗯,我想我会这样回答我自己,让大家谁读这个知道答案。

三条标记线的问题在于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()); 

现在3号线将工作完全正常。

Credits: 感谢@marmor和@SpK的帮助。对不起,无法选择你的答案之一,因为我从你们那里拿走了一半。

1

从堆栈跟踪情况来看,我会说,这些线中的一条返回null

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

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

+0

是的你是对的。 'checkedRadioButton'的结果是null。任何想法为什么发生这种情况? – akaHuman

+0

它表示从group.getCheckedRadioButtonId()返回的值不会出现在ask_report的布局中。根据http://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId(),如果没有选择任何东西,它可能会返回-1,也许是这种情况。 – marmor

+0

尝试在您的RadioGroup中添加'android:checkedButton ='@ + id/road_accident''以默认选择第一个项目 – marmor

0

的Android只是实现OnClickListener你,当你定义

android:onClick="someMethod" 
1

这也许是因为你得到从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里面你ButtononClick方法获取文本从radioButton做这样的 -

// 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()); 
+0

它仍然不能正常工作...... :( 同时告诉我可以使用'getText()'和'RadioButton'? – akaHuman

+0

@ shrey347现在,你只有新的例外或者只有相同的例外 – Praveenkumar

+0

这也是一个例外,虽然我已经找出了错误。这是事情出错的地方: 'checkedRadioButton =(RadioButton)findViewById(group.getCheckedRadioButtonId());' 'checkedRadioButton'的值是空的 – akaHuman

相关问题