First of all I want to say that this is my first post so it may no be very well done, in general.
My issue is the following: I would like to use three different strings from three editTexts and display them in three TextViews in another activity. I have already been searching for different kinds of ways of doing it (arrays, bundle) but it continues crashing. Help me, please. Here you have my code:
Main activity
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
EditText eText1 = (EditText) findViewById(R.id.editText1);
EditText eText2 = (EditText) findViewById(R.id.editText2);
EditText eText3 = (EditText) findViewById(R.id.editText3);
String m1 = eText1.getText().toString();
String m2 = eText2.getText().toString();
String m3 = eText3.getText().toString();
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra("m1",m1);
intent.putExtra("m2",m2);
intent.putExtra("m3",m3);
startActivity(intent);
}
subActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Bundle extras = getIntent().getExtras();
// get the extras
String a = extras.getString("m1");
String b = extras.getString("m2");
String c = extras.getString("m3");
// Set the text views
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText(a); // This is line 23
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setText(b);
TextView tv3 = (TextView) findViewById(R.id.textView3);
tv3.setText(c);
}
Edit1: I have changed what #Squonk said but still crashes. I don't know how to upload the logcat because it is too large for a text but I still can't upload images.
Don't know what to do :(
Edit2: Thanks to #Squonk again. I have finally managed to add the logcat. This is the logcat for the code I recently changed in "Edit1".
03-29 16:05:39.905: E/AndroidRuntime(327): FATAL EXCEPTION: main
03-29 16:05:39.905: E/AndroidRuntime(327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.os.Looper.loop(Looper.java:123)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 16:05:39.905: E/AndroidRuntime(327): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327): at java.lang.reflect.Method.invoke(Method.java:521)
03-29 16:05:39.905: E/AndroidRuntime(327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 16:05:39.905: E/AndroidRuntime(327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 16:05:39.905: E/AndroidRuntime(327): at dalvik.system.NativeStart.main(Native Method)
03-29 16:05:39.905: E/AndroidRuntime(327): Caused by: java.lang.NullPointerException
03-29 16:05:39.905: E/AndroidRuntime(327): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:23)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 16:05:39.905: E/AndroidRuntime(327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 16:05:39.905: E/AndroidRuntime(327): ... 11 more
This is one of the textviews in fragment_display_message.xml. There are three of them. Could it be that when finding the textviews in the subactivity I have to write where to find them not to confuse with the main activity?
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="43dp"
android:hint="#string/edit_message"
android:textAppearance="?android:attr/textAppearanceLarge" />
Edit3: Eureka! I have finally discovered where the problem was. I had inserted the textviews in the fragment_display_message.xml instead of activity_display_message.xml. As in the mainactivity.java I use the fragment_main.xml I thought that in a subactivity would be the same. Well, I was wrong. Thanks to all! :D
Thank you for your support
The problem is these two lines...
Bundle extras = new Bundle();
...
intent.putExtras(extras);
An Intent already carries a Bundle but what you're doing is creating a second Bundle and adding that as an 'extra'.
Remove both of those lines from your main Activity and change the following lines...
extras.putString("m1", m1);
extras.putString("m2", m2);
extras.putString("m3", m3);
...to...
intent.putExtra("m1", m1);
intent.putExtra("m2", m2);
intent.putExtra("m3", m3);
In the first activity,
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("SomeValue", data);
startActivity(i);
// In second activity
String str2 = getIntent().getExtras().getString("SomeValue");
tvRecieve.setText(str2);
Related
I have two framelayout in my main.xml file. I add framelayouts to the class that extends Fragment. my main class extends FragmentActivity and this is Oncreate method of it:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm =getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=new Freg1();
Fragment f2=new Freg1();
ft.add(R.id.frame1, f);
ft.add(R.id.frame2, f2);
ft.commit();
tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
tv1=(TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
Log.i(TAG,"1");
lv1=(ListView) findViewById(R.id.listView1);
lv2=(ListView) findViewById(R.id.listView2);
Log.i(TAG,"2");
List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
Log.i(TAG,"3");
ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
Log.i(TAG,"4");
lv1.setAdapter(listAdapter);
Log.i(TAG,"5");
lv2.setAdapter(listAdapter);
Log.i(TAG,"6");
}
when i run the codes, it crashed after LOG no4. that mean setAdapter() method do not work. how can i resolve this problem?
this is my logcat resource:
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
thank for you that help me to resolve problem.
Your ListView lv1 is null as you can see here:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter);
Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it.
This is a very simple android app. I'm just learning to write code for android now and I am unsure why the line: add.setOnClickListener(new View.OnClickListener() throws a NullPointerException.
public class StartingPoint extends ActionBarActivity{
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
counter--;
display.setText("Your total is " + counter);
}
});
}
Here is the XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.thenewboston.jaredh.StartingPoint$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/total"
android:textSize="40sp"
android:id="#+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvDisplay"
android:layout_below="#+id/tvDisplay"
android:layout_gravity="center"
android:text="#string/addButton"
android:textSize="20sp"
android:id="#+id/bAdd" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/bAdd"
android:layout_below="#+id/bAdd"
android:layout_gravity="center"
android:text="#string/subButton"
android:textSize="20sp"
android:id="#+id/bSub"/>
Logcat Error:
03-29 17:44:24.930: D/AndroidRuntime(25367): Shutting down VM
03-29 17:44:24.930: W/dalvikvm(25367): threadid=1: thread exiting with uncaught exception (group=0x41831898)
03-29 17:44:24.940: E/AndroidRuntime(25367): FATAL EXCEPTION: main
03-29 17:44:24.940: E/AndroidRuntime(25367): java.lang.RuntimeException: Unable to start activity ComponentInf{com.thenewboston.jaredh/com.thenewboston.jaredh.StartingPoint}: java.lang.NullPointerException
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.access$700(ActivityThread.java:165)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.os.Looper.loop(Looper.java:137)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.main(ActivityThread.java:5455)
03-29 17:44:24.940: E/AndroidRuntime(25367): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 17:44:24.940: E/AndroidRuntime(25367): at java.lang.reflect.Method.invoke(Method.java:525)
03-29 17:44:24.940: E/AndroidRuntime(25367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-29 17:44:24.940: E/AndroidRuntime(25367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-29 17:44:24.940: E/AndroidRuntime(25367): at dalvik.system.NativeStart.main(Native Method)
03-29 17:44:24.940: E/AndroidRuntime(25367): Caused by: java.lang.NullPointerException
03-29 17:44:24.940: E/AndroidRuntime(25367): at com.thenewboston.jaredh.StartingPoint.onCreate(StartingPoint.java:39)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.Activity.performCreate(Activity.java:5372)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-29 17:44:24.940: E/AndroidRuntime(25367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
03-29 17:44:24.940: E/AndroidRuntime(25367): ... 11 more
As you can see it is a very simple app just adding 1 to the text field when either button is clicked. Any help could be greatly apprecitated. Just unsure why the NullPointerException is being caused.
I would guess that the XML you presented (without saying which file it really is, and despite that the XML text is not even valid) is the layout of the PlaceholderFragment you add at the beginning. However, the fragment is not immediately added, just scheduled to be added by the FragmentManager. Therefore when you are immediately trying to find its UI components, the activity fails to do so, and the add reference is set to null by findViewById.
My app currently boots directly into a layout, and the onCreate method is used to control that view, for instance responding to button presses and the likes. I wanted however to move to a different initial layout. I initially just moved all of the original control functionality to a new method, and changed the setContentView(R.layout.main); to match the new view, this however causes a crash. After some fiddling around I found that no matter what I change within this method I get a crash, even when commenting out minor method calls.
Here is my onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(new ArrayAdapter<String>(actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1, new String[] {
getString(R.string.lanc),
getString(R.string.lanb),
getString(R.string.lana),
}), this);
final Intent intent = new Intent("com.google.zxing.client.android.SCAN");
latituteField = (TextView) findViewById(R.id.latitude);
longitudeField = (TextView) findViewById(R.id.longtitude);
orgText = (TextView) findViewById(R.id.orgText);
accpointnameText = (TextView) findViewById(R.id.accpointnameText);
floorText = (TextView) findViewById(R.id.floorText);
setlocation();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("attempting get from input fields");
accpointText = (EditText) findViewById(R.id.accpointText);
passwordText = (EditText) findViewById(R.id.passwordText);
IDin = accpointText.getText().toString();
System.out.println(IDin);
Passwordin = passwordText.getText().toString();
System.out.println(Passwordin);
System.out.println("so far");
JSONstate = false;
new JSONDownloader().execute("https://apps.taskpixie.com/arSettings/?id="+IDin+"&password="+Passwordin+"");
}
});
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivityForResult(intent, 0);
}
});
}
Why does my app crash no matter what I remove? How can I change my initial layout?
If you need any more data I can provide it, thanks in advance.
For example, commenting out setlocation(); causes the log cat to spit out
08-31 14:15:47.120: E/AndroidRuntime(12326): FATAL EXCEPTION: main
08-31 14:15:47.120: E/AndroidRuntime(12326): java.lang.RuntimeException: Unable to resume activity {com.example.bilisattendancerecorder/com.example.bilisattendancerecorder.Main}: java.lang.NullPointerException
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.os.Looper.loop(Looper.java:137)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-31 14:15:47.120: E/AndroidRuntime(12326): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 14:15:47.120: E/AndroidRuntime(12326): at java.lang.reflect.Method.invoke(Method.java:525)
08-31 14:15:47.120: E/AndroidRuntime(12326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 14:15:47.120: E/AndroidRuntime(12326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-31 14:15:47.120: E/AndroidRuntime(12326): at dalvik.system.NativeStart.main(Native Method)
08-31 14:15:47.120: E/AndroidRuntime(12326): Caused by: java.lang.NullPointerException
08-31 14:15:47.120: E/AndroidRuntime(12326): at com.example.bilisattendancerecorder.Main.onResume(Main.java:299)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.Activity.performResume(Activity.java:5211)
08-31 14:15:47.120: E/AndroidRuntime(12326): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
I would like to stress here that changing something so minute should not cause such drastic errors, and that no matter what I take away from this method, I receive errors.
I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display is supposed to reference a TextView instance, but you're explictly casting into a Button, and these are not compatible types.
As you got the answer from A-C this time, but for next time in android application development a important suggestion:
Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error.
And try to find what can be wrong with that line of code.
For example: in your logcat it is showing-
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button. So you can easily remove your errors without any help.
Hope that helps not only you current problem but prevented your future time and efforts.
So whenever I click the button on the startup page, it gives me a force close error. Here's the class for the main.xml layout file:
public class ForeverAloneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnstrt = (Button) findViewById(R.id.toq1);
btnstrt.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent frstq = new Intent(v.getContext(), QuestionOne.class);
startActivityForResult(frstq, 0);
And this is what I believe is producing the error. This class is related to the page that that when pressing the button on the startup page, you are taken to:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView (R.layout.frstq);
Button startQ2 = (Button) findViewById(R.id.toq2);
startQ2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent toQ2 = new Intent(v.getContext(), QuestionTwo.class);
final EditText number = (EditText) findViewById(R.id.editText1);
final Toast error = Toast.makeText(QuestionOne.this, "Please insert a value", Toast.LENGTH_SHORT);
if (number.getText().toString().equals("")) {error.show();
}else{
startActivityForResult(toQ2, 0);}
That if statement is there as on the next page, there is an EditText box. I tried to make it so that if there is nothing in the EditText box, it displays a toast message saying "Please insert a value". Until an integer is put into the EditText box, then the button will not work.
If someone can help, it will be much appreciated.
Logcat:
04-07 19:33:58.199: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-07 19:33:58.221: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-07 19:33:58.221: E/AndroidRuntime(362): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kenning.foreveralone/com.kenning.foreveralone.QuestionOne}; have you declared this activity in your AndroidManifest.xml?
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.kenning.foreveralone.ForeverAloneActivity$1.onClick(ForeverAloneActivity.java:22)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2408)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:8816)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123)
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 19:33:58.221: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method)
are you adding QuestionTwo in manifiest file ?
check your error - have you declared this activity in your AndroidManifest.xml?
Declare your activity in manifeast file. It seems you forgetted that
<activity android:name="QuestionTwo" />
also dont forget to include every activity in your manifeast file
Hey have you added the QuestionTwo class as an activity in menifest file. If after adding the
Intent toQ2 = new Intent(YourCurrentActivity.this,QuestionTwo.class);
then you must have not added the activity in menifest.B'coz it clearly shows in the log that Activity is not found. You have to declare the activity in menifest file.
Hope you'll get run your app.