1
votes

I'm trying to create an application such that when I click on a button in the 1st layout, it will extract the text from an EditText and place that value into a TextView from the 2nd layout. However, it seems that the application will crash because of the setText for the TextView.

I didn't put the logCat as it doesn't tell much. I believe that the error comes from the setText part. But I have no idea on how to fix that part, or maybe that my way of coding is wrong. Hope that someone will help me. Thanks in advance.

<TextView android:id="@+id/showtitle"
    android:layout_marginTop="1dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:textStyle="bold" />

<TextView android:id="@+id/showtemplate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/showtitle"
    android:layout_alignLeft="@+id/showtitle"
    android:paddingBottom="4dip"
    android:includeFontPadding="false"
    android:textSize="15sp"
    android:textStyle="normal" />

Below is my java code:

import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.EditText; import android.widget.Button; import android.widget.Toast;

import android.app.Activity;

public class AndroidGroupSMS extends Activity{

 private EditText title;
 private EditText template;
 private Button btnsave;
 private Button btnload;
 private TextView text,text2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 title = (EditText) findViewById(R.id.title);
    template = (EditText) findViewById(R.id.template);
    btnsave = (Button) findViewById(R.id.save);
    btnload = (Button) findViewById(R.id.load);
    text = (TextView) findViewById(R.id.showtitle);
    text2 = (TextView) findViewById(R.id.showtemplate);

    btnsave.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
    // TODO Auto-generated method stub   
    String sTitle = title.getText().toString();
    String sTemplate = template.getText().toString();
    if(sTitle.length() > 0 && sTemplate.length() > 0)
    {
     //this is where i'm setting the text extracted from the edittext boxes
     text.setText(sTitle.toString());
     text2.setText(sTemplate.toString());
    }
    else
     Toast.makeText(getBaseContext(),"Please enter the title and template" + "", Toast.LENGTH_SHORT).show();     
   }
  });       
}

I have no idea how to read this. Below is my logCat as requested: 11-12 04:32:51.652: DEBUG/AndroidRuntime(283): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 11-12 04:32:51.662: DEBUG/AndroidRuntime(283): CheckJNI is ON 11-12 04:32:51.982: DEBUG/AndroidRuntime(283): --- registering native functions --- 11-12 04:32:52.464: DEBUG/ddm-heap(283): Got feature list request 11-12 04:32:53.092: DEBUG/AndroidRuntime(283): Shutting down VM 11-12 04:32:53.102: DEBUG/dalvikvm(283): DestroyJavaVM waiting for non-daemon threads to exit 11-12 04:32:53.102: DEBUG/dalvikvm(283): DestroyJavaVM shutting VM down 11-12 04:32:53.102: DEBUG/dalvikvm(283): HeapWorker thread shutting down 11-12 04:32:53.112: DEBUG/dalvikvm(283): HeapWorker thread has shut down 11-12 04:32:53.112: DEBUG/jdwp(283): JDWP shutting down net... 11-12 04:32:53.122: INFO/dalvikvm(283): Debugger has detached; object registry had 1 entries 11-12 04:32:53.132: DEBUG/dalvikvm(283): VM cleaning up 11-12 04:32:53.192: DEBUG/dalvikvm(283): LinearAlloc 0x0 used 629532 of 5242880 (12%) 11-12 04:32:53.842: DEBUG/AndroidRuntime(291): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 11-12 04:32:53.842: DEBUG/AndroidRuntime(291): CheckJNI is ON 11-12 04:32:54.182: DEBUG/AndroidRuntime(291): --- registering native functions --- 11-12 04:32:54.682: DEBUG/ddm-heap(291): Got feature list request 11-12 04:32:55.302: DEBUG/dalvikvm(104): GC freed 2293 objects / 132304 bytes in 2157ms 11-12 04:32:55.352: DEBUG/ActivityManager(52): Uninstalling process joel.AndroidGroupSMS 11-12 04:32:55.362: DEBUG/ActivityManager(52): Force removing process ProcessRecord{43d9daf0 275:joel.AndroidGroupSMS/10024} (joel.AndroidGroupSMS/10024) 11-12 04:32:55.401: INFO/Process(52): Sending signal. PID: 275 SIG: 9 11-12 04:32:55.492: INFO/UsageStats(52): Unexpected resume of com.android.launcher while already resumed in joel.AndroidGroupSMS 11-12 04:32:55.512: INFO/WindowManager(52): WIN DEATH: Window{43cf7840 joel.AndroidGroupSMS/joel.AndroidGroupSMS.AndroidGroupSMS paused=false} 11-12 04:32:55.592: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=joel.AndroidGroupSMS/.AndroidGroupSMS } 11-12 04:32:55.612: DEBUG/ActivityManager(52): Received spurious death notification for thread android.os.BinderProxy@43bc8370 11-12 04:32:55.652: DEBUG/AndroidRuntime(291): Shutting down VM 11-12 04:32:55.652: DEBUG/dalvikvm(291): DestroyJavaVM waiting for non-daemon threads to exit 11-12 04:32:55.662: DEBUG/dalvikvm(291): DestroyJavaVM shutting VM down 11-12 04:32:55.662: DEBUG/dalvikvm(291): HeapWorker thread shutting down 11-12 04:32:55.672: DEBUG/dalvikvm(291): HeapWorker thread has shut down 11-12 04:32:55.672: DEBUG/jdwp(291): JDWP shutting down net... 11-12 04:32:55.672: INFO/dalvikvm(291): Debugger has detached; object registry had 1 entries 11-12 04:32:55.672: DEBUG/dalvikvm(291): VM cleaning up 11-12 04:32:55.732: ERROR/AndroidRuntime(291): ERROR: thread attach failed 11-12 04:32:55.832: DEBUG/dalvikvm(291): LinearAlloc 0x0 used 639500 of 5242880 (12%) 11-12 04:32:55.952: INFO/ActivityManager(52): Start proc joel.AndroidGroupSMS for activity joel.AndroidGroupSMS/.AndroidGroupSMS: pid=298 uid=10024 gids={1015} 11-12 04:32:56.062: WARN/InputManagerService(52): Got RemoteException sending setActive(false) notification to pid 275 uid 10024 11-12 04:32:56.392: DEBUG/ddm-heap(298): Got feature list request 11-12 04:32:56.592: WARN/ActivityThread(298): Application joel.AndroidGroupSMS is waiting for the debugger on port 8100... 11-12 04:32:56.642: INFO/System.out(298): Sending WAIT chunk 11-12 04:32:56.691: INFO/dalvikvm(298): Debugger is active 11-12 04:32:56.872: INFO/System.out(298): Debugger has connected 11-12 04:32:56.882: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:57.082: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:57.282: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:57.483: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:57.692: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:57.893: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:58.092: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:58.300: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:58.502: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:58.707: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:58.912: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:59.112: INFO/System.out(298): waiting for debugger to settle... 11-12 04:32:59.322: INFO/System.out(298): debugger has settled (1361) 11-12 04:33:00.792: INFO/ActivityManager(52): Displayed activity joel.AndroidGroupSMS/.AndroidGroupSMS: 4860 ms (total 4860 ms) 11-12 04:33:02.942: WARN/KeyCharacterMap(298): No keyboard for id 0 11-12 04:33:02.952: WARN/KeyCharacterMap(298): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 11-12 04:33:04.592: DEBUG/dalvikvm(99): GC freed 481 objects / 24704 bytes in 185ms

3
@Joel Seah: can u post your logcat message, or just highlight which line of gets error and what error you got in your logcat? - Sankar Ganesh PMP
With something like this, I'm afraid if the logCat doesn't tell you much, you're not reading it right. - Blumer
My wild guess would be that something somewhere is null when you try to use it, logcat will tell you and us more :) - Marek Szanyi
At minimum, we really need the stacktrace from logcat, and if possible the corresponding layout XML file. - Alexander Lucas
@Tilsan The Fighter: I posted the logCat but I have no idea why it is so jumbled up. @Blumer & psicho: I have no idea how to read logCat. @Alexander Lucas: I tried posting my main layout but it only shows a few lines. I'm only able to post my 2nd layout with such clarity. - Joel Seah

3 Answers

1
votes

Everything looks fine except the part where you use toString() on String object, that is not neccessary.

But are the text and the text2 TextViews located in the same XML layout as EditText views? In this case the main.xml? If the answer is no, then you have NullPointerException when you try to use them because findViewById can't find them.

0
votes

You could just do text2.setText(template.getText().toString());

Instead of text2.setText(sTemplate.toString());

See if that works.

0
votes

Joel,

Considering you have the edittext's in a different xml file. You might be able to create another resource file for what is displayed in your xml files. For instance you could create an xml file that will hold the values of what is displayed in your other xml files. Take a look at this example (notepad). The example is from the android development web site. You could use the values that are placed in your "values" xml for the values as all your other xml files. This way, you have a central place for your other xml files to grab their values. This may solve your problem. Let us know what results you find.

You will need to open the res folder and the values folder to see what im talking about when you are viewing the example.