ive just started with android developement and wondering why my app keeps crashing.... I'm trying to change activities as of now.....heres my code
package app.dm.com.tictac;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity{
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),twoP.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#4285f4"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:textColor="#FFF"
android:textSize="30dp"
android:text="TIC TAC TOE" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/b1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="98dp"
android:text="Play 1v1"
/>
</RelativeLayout>
The second class:
package app.dm.com.tictac;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
/**
* Created by damian on 23/4/15.
*/
public class twoP extends MainActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_v);
Button b=(Button)findViewById(R.id.b2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is its XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xlmns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#4285f4"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random BS "
android:textColor="#FFF"
android:textSize="30dp"
android:id="#+id/textView2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back"
android:id="#+id/b2"
android:layout_gravity="center_vertical"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="98dp" />
</RelativeLayout>
Once i install the app onto my device it just shows "Unfortunately ticTac has stopped". I really am not sure what my mistake is .Any help would be appreciated....Thank You
MY android manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.dm.com.tictac" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".twoP">
</activity>
</application>
</manifest>
Here is the logcat file:
https://drive.google.com/file/d/0B9Hzti3K8FV7WTRlb0U1LTJyZVU/view?usp=sharing
Try after replacing getApplicationContext() to MainActivity.this
And even after that the error not goes then please post the error logs here.
In your first xml you have android:layout_below="#+id/b1. You probably meant to typeandroid:id="#+id/b2
In your code, you call b=(Button)findViewById(R.id.b1); however there is no Button with that id, therefor b is assigned null. Afterwards, you try to assign to that an onClickListener and the app crashes.
This is exactly what this line is telling you:
04-23 19:42:21.979: E/AndroidRuntime(13918): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.dm.com.tictac/app.dm.com.tictac.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Your error is -
java.lang.RuntimeException: Unable to start activity ComponentInfo{app.dm.com.tictac/app.dm.com.tictac.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
It occurs at app.dm.com.tictac.MainActivity.onCreate(MainActivity.java:21)
You are using
b=(Button)findViewById(R.id.b1); but you don't have a button with this id defined anywhere in any of the layout files. I think you meant to use in the layout file of the first activity.
Related
I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.
Here's what I have so far;
LoginActivity.java
package com.example.app;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class LoginActivity extends Activity {
EditText subdomain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subdomain = (EditText) findViewById(R.id.subdomain);
Button btn=(Button)findViewById(R.id.sign_in_button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(LoginActivity.this,
MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
public void saveInfo (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", YourSchool.getText().toString());
editor.commit();
}
}
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a
browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");
// Stop local links and redirects from opening in browser instead
of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void displayData (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
String client_subdomain = sharedPref.getString("name", "");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_login.xml
<LinearLayout
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:gravity="center_horizontal"
android:orientation="vertical"
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.example.app.LoginActivity">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/YourSchool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your school"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="webView"
android:text="SIGN IN"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
activity_main.xml
<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"
tools:context=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="#string/app_name"></activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain
I think it's probably something small that I've missed out but any help would be greatly appreciated.
Many thanks,
Sam
Store the variable using sharedPrefeence as below,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("your_string_key", yourStringValue);
editor.commit();
Then retrieve the value as,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
String myStringValue = sp.getInt("your_string_key", -1);
Use the variable in the loadUrl as
mWebView.loadUrl("https://"+myStringValue+".mydomain.co.uk");
I'd seen this asked in many, many places and have tried to follow the instructions given to no avail. I dont know if the questions are old, i'm doing things incorrectly or if my android studio program isnt working. What i want to do is to only open a new activity when a button is clicked. I'm very new to developing android applications.
I've recently tried to follow the answer provided here: How do I get a button to open another activity in Android Studio?
OnClick on the button is called "goTutorials"
My original activity is called home (Not mainActivity)
The new one is called tutorials.
This is what i added from trying to follow the link above:
In home's java file:
btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(home.this, tutorials.class));
}
});
In manifest file:
<activity
android:name="tutorials"
android:label="#string/app_name">
</activity>
Method 1
Use the onclick attribute in XML (so that whenever you click the button, defined method will trigger)
Step 1. - go to the XML where you button is(activity_home) & add
<Button
............
android:onClick="gotoTutorial"/>
Step 2. - then go to the home.Java & add following
public void gotoTutorial(View v){
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}
Method 2
Use the setOnClickListener
Step 1. -
//create the link to the button in the interface
btn_tutorial = (Button)findViewById(R.id.tutorial_button);
btn_tutorial.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}
}
I have created a sample app please see the code below.
You need to specify information of all activities in Manifest file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nextech.startnewactivity">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TutorialsActivity"
android:label="#string/title_activity_tutorials"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
My Launcher activity is MainActivity.java
package com.nextech.startnewactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startTutorials = (Button)findViewById(R.id.startTutorials);
startTutorials.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tutorialsActivityIntent = new Intent(MainActivity.this,TutorialsActivity.class);
MainActivity.this.startActivity(tutorialsActivityIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.MainActivity"
tools:showIn="#layout/activity_main">
<TextView android:id="#+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_centerHorizontal="true"
android:text="Hello World! This is main activity!" />
<Button android:id="#+id/startTutorials"
android:layout_below="#+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Start Tutorials"/>
</RelativeLayout>
TutorialsActivity.java
package com.nextech.startnewactivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class TutorialsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorials);
}
}
activity_tutorials.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.TutorialsActivity"
tools:showIn="#layout/activity_tutorials">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is tutorial activity"
android:layout_centerInParent="true"/>
</RelativeLayout>
I hope it helps.
Your manifest file is not displaying, but make sure you add the activity in your manifest AndroidManifest.xml in the application field. Here's an example from Google: Link
<application ... >
...
<activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>
Otherwise, the method you're using should be correct. You create an intent to the class of the Activity you want to start.
Intent intent = new Intent(this, tutorials.class);
startActivity(intent);
Make sure you have the necessary methods such as onCreate(Bundle savedInstance) Overrided.
Note that in the sample above android:name="" is your activity's full path name to the class (without .class/.java) and label is going to be the action toolbar string you'll see. In the <meta-data/> section you can alter the android:value="" to the starting activity's name so that will need to change based on what you call it.
Finally, make sure it has an XML view to go with it, as it gets inflated in the onCreate(Bundle savedInstance) call.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_here);
}
inside your activity file write this
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(home.this, tutorials.class));
}
});
XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
.Java class
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, YourActivityYouWantToOpen.class);
startActivity(intent);
}
I am giving the details of all my files below.Please check them and let me know where I am doing the mistake due to which the error that the "unfortunately "app name" has stopped" error is coming when I am running my first android app using android SDK. Since I am a beginner in android development Please explain the solution nicely.
My MainActivity.java file :
package com.example.sunny.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.greetings_view);
}
public void showGreetings(View view)
{
String message= "welcome to my app";
textView.setText(message);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My activity_main.xml file is as follows:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="#+id/greetings_view">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="greetings here"
android:id="#+id/greetings_view"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="show greetings"
android:layout_below="#+id/greetings_view"
android:onClick="My button"
android:id="#+id/greetings_view"
/>
</RelativeLayout>
My AndroidManifest.xml file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sunny.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sunny.myfirstapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
My logcat :
06-26 10:37:23.137 31265-31265/? D/dalvikvm﹕ Late-enabling CheckJNI
06-26 10:37:24.013 31265-31265/com.example.sunny.myfirstapp D/AndroidRuntime﹕ Shutting down VM
06-26 10:37:24.021 31265-31265/com.example.sunny.myfirstapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62be288)
06-26 10:37:24.025 31265-31265/com.example.sunny.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sunny.myfirstapp/com.example.sunny.myfirstapp.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
all your views have the same ID greetings_view, elements must have no ID (blank) or different IDs.
ps.: in the future, the relevant log cat is the Stacktrace, is the part that have the direct message related to your crash. The log you posted is just some random stuff from your phone.
just try like this , it will Work
package com.example.sunny.myfirstapp;
public class MainActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.greetings_view);
String message= "welcome to my app";
textView.setText(message);
}
}
Remove from android:id="#+id/greetings_view" Relative Layout , You given that id in Two places TextvieW and Relative Layout
simple textview xml
<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=".MainActivity" >
<TextView
android:id="#+id/greetings_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
My application builds and says successful, but exits without launching when I open it on my phone. Is there something wrong with my manifest file?
Error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.terik.thirdattempt/com.example.terik.thirdattempt.MainActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class android.widget.Button
Here's the xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.terik.thirdattempt" >
<uses-sdk android:minSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here's my layout for kicks and giggles.
<TextView android:layout_width="500dp"
android:layout_height="200dp"
android:text="0"
android:textSize="72pt"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/Button1"
android:layout_alignStart="#+id/Button1"
android:layout_marginLeft="41dp"
android:layout_marginStart="71dp"
android:layout_alignRight="#+id/Button1"
android:layout_alignEnd="#+id/Button1"></TextView>
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/Button1"
android:background="#drawable/custom_button"
android:adjustViewBounds="false"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp" />
</RelativeLayout>
And I guess my MainActivity.java file
package com.example.terik.thirdattempt;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addOne(View view){
number = number +1;
textView.setText(number);
}
}
You got null pointer exception because you declare your textview before setcontent view. This should fix that (but i dont understand what method addOne is, so i leave that alone):
int number = 0;
TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addOne(View view){
number = number +1;
textView.setText(number);
}
You can initialize UI after setContentView(R.layout.activity_main) in the function of onCreate.
So your app crashed when you launched your device.You Should initialize the TextView after the function of onCreate like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
}
I'm working on a app. Past days i'm trying to get more then one button to work on my page. But i can't figur it out. Can some one help? I'm new to Development :)
Layout (xml)
Activity_main
<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.rust.rustapp.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button"
android:onClick="button1Click"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="38dp"
android:text="Button"
android:onClick="button1Click"
/>
</RelativeLayout>
Java:
MainActivty:
package com.rust.rustapp;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
private void button1Click()
{
startActivity(new Intent(MainActivity.this, Intro.class));
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
button1Click();
break;
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intro.xml (Button1 is opening this page)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/test"
>
</LinearLayout>
Intro.java
package com.rust.rustapp;
import android.app.Activity;
import android.os.Bundle;
public class Intro extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rust.rustapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Intro"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
The new layout name is: FunPage
Thx!
From the code you pasted for MainActivity, it looks like you are mixing two different ways to handle button clicks.
If you can to register the click listener from your layout xml, the method corresponding to the onClick attribute needs to be public and not declared as a method inside your button1 listener.
An alternative is to listen to click events solely with your Java code. If you do this, then remove the onClick attribute from your layout XML for that button. There are many, many examples out there.
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//TODO -- In your case, start the "Intro" activity
}
}
It might help if you extract that method to start the Intro activity to a method outside of the click listener (i.e., as a method of MainActivity).