Android – textview keeps crashing while trying to setText

android

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

Best Answer

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.

Related Topic