When I run the analize it on comes back with one error in the manafest android name = is not public
but when i build it says built with no error but when i run it on my emu and my tablet it crashes i can not figure this out please help. This is my code with names omitted.
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"
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="com.xxxx.xxxx.app.MainActivity"
android:background="#drawable/appbackground">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/facebook"
android:id="#+id/button1"
android:background="#drawable/facebook"
android:clickable="true"
android:enabled="true"
android:gravity="end"
android:width="50dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button3"
android:layout_alignEnd="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dealer"
android:id="#+id/button2"
android:enabled="true"
android:background="#drawable/dealer"
android:clickable="true"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/button4"
android:layout_alignStart="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/map"
android:id="#+id/button3"
android:enabled="true"
android:clickable="true"
android:background="#drawable/google_navigation"
android:layout_below="#+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about"
android:id="#+id/button4"
android:enabled="true"
android:clickable="true"
android:background="#drawable/about"
android:layout_alignBottom="#+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pthdh"
android:id="#+id/button5"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:enabled="true"
android:clickable="true"
android:background="#drawable/pthdh" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rpe"
android:id="#+id/button6"
android:layout_alignTop="#+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:enabled="true"
android:clickable="true"
android:background="#drawable/rpebutton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/call"
android:id="#+id/button7"
android:enabled="true"
android:clickable="true"
android:background="#drawable/callus"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contact"
android:id="#+id/button8"
android:enabled="true"
android:clickable="true"
android:background="#drawable/contact"
android:layout_marginBottom="31dp"
android:layout_above="#+id/button7"
android:layout_centerHorizontal="true" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textClock"
android:textStyle="bold"
android:textColor="#000000"
android:background="#ff5300"
android:gravity="center"
android:textSize="30sp"
android:textIsSelectable="true"
android:layout_above="#+id/button8"
android:layout_alignLeft="#+id/button8"
android:layout_alignStart="#+id/button8"
android:layout_marginBottom="34dp" />
</RelativeLayout>
MainActivity.java
package com.xxxx.xxxx.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
class MainActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana",
"com.facebook.katana.ProfileTabHostActivity");
Long uid = new Long("xxxx");
intent.putExtra("extra_user_id", uid);
startActivity(intent);
break;
case R.id.button2:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse
("http://www.xxxx.com"));
startActivity(intent);
break;
case R.id.button3:
Uri location = Uri.parse("geo:xx.xxx7629,-xx.xxxx584?z=14");
new Intent(Intent.ACTION_VIEW, location);
break;
case R.id.button4:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse
("http://www.xxxx.com/default.asp?page=info"));
startActivity(intent);
break;
case R.id.button5:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse
("http://www.xxxx.com/"));
startActivity(intent);
break;
case R.id.button6:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse
("https://members.xxxx.com/"));
startActivity(intent);
break;
case R.id.button7:
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:xxxx"));
startActivity(dial);
break;
case R.id.button8:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse
("http://www.xxxx.com/default.asp?page=xcontact"));
startActivity(intent);
break;
default:
Log.d(getApplication().getPackageName(), "Button click error!");
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();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
}
AndroidManifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx.xxxx.app" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.xxxx.xxxx.app.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxx"/>
</application>
</manifest>
You have to put "public" before "class":
public class MainActivity extends Activity implements OnClickListener
Related
I have a problem in this app, it show me "Unfortunately, app has stopped"
MainActivity.class
package ahmedchtn.stockmanager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText username;
EditText password;
Button loginbutton;
Button bpasschange;
int counter = 3;
String username_string ;
String password1 = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.identifer);
password = (EditText) findViewById(R.id.password);
loginbutton = (Button) findViewById(R.id.bLogin);
bpasschange = (Button) findViewById(R.id.bCh);
username_string = "admin";
if (password1 == "") {
password1 = "admin";
} else {
Intent intent4 = getIntent();
password1 = intent4.getStringExtra("oldpassw");
}
bpasschange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentoldpass = new Intent(MainActivity.this, PasswordChange.class);
intentoldpass.putExtra("oldpassw", password1);
Intent intentpasschan = new Intent(getApplicationContext(), PasswordChange.class);
startActivity(intentpasschan);
}
});
loginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (username.getText().toString().equals(username_string) &&
password.getText().toString().equals(password1)) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
Intent iop = new Intent(getApplicationContext(), ManagementPage.class);
startActivity(iop);
} else {
Toast.makeText(getApplicationContext(),
"Wrong Entries", Toast.LENGTH_SHORT).show();
counter--;
if (counter == 0) {
loginbutton.setEnabled(false);
}
}
}
}
);
}
}
PasswordChange.class
package ahmedchtn.stockmanager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PasswordChange extends AppCompatActivity {
Button bchange;
EditText oldpassword;
EditText newpassword;
String oldpass;
String newpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password_change);
bchange = (Button) findViewById(R.id.bChange2);
oldpassword = (EditText) findViewById(R.id.old_password);
newpassword = (EditText) findViewById(R.id.new_password);
bchange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent ioldpass = getIntent();
oldpass = ioldpass.getStringExtra("oldpassw");
if (oldpass == (oldpassword.getText().toString())) {
newpass = newpassword.getText().toString();
Intent intentnew = new Intent(PasswordChange.this, MainActivity.class);
intentnew.putExtra("passnew", newpass);
startActivity(intentnew);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
});
}
}
I think that the problem is with the intents..
Thanks in advance for your help !
Is there any other solutions
The logcat
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at ahmedchtn.stockmanager.MainActivity$2.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:4439)
at android.widget.Button.performClick(Button.java:139)
at android.view.View$PerformClick.run(View.java:18395)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
Xml Codes
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ahmedchtn.stockmanager.MainActivity">
<EditText
android:id="#+id/identifer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_id" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/bLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/login" />
<Button
android:id="#+id/bCh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="changer passe" />
</LinearLayout>
</LinearLayout>
activity_password_change.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_password_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="ahmedchtn.stockmanager.PasswordChange">
<EditText
android:id="#+id/old_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_old_password" />
<EditText
android:id="#+id/new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:hint="#string/enter_your_new_password" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/activity_vertical_margin"
android:weightSum="1">
<Button
android:id="#+id/bChange2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/confirm" />
<Button
android:id="#+id/main_page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/main_page" />
</LinearLayout>
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ahmedchtn.stockmanager">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ManagementPage"
android:parentActivityName=".MainActivity" />
<activity android:name=".ProductsSearch" />
<activity android:name=".ProductsCommand" />
<activity
android:name=".Editor"
android:parentActivityName=".ProductsList" />
<activity android:name=".ProductsList" />
<provider
android:name=".data.ProductProvider"
android:authorities="ahmedchtn.stockmanager"
android:exported="false" />
<activity android:name=".PasswordChange"></activity>
</application>
</manifest>
I have updated my code
From the information you've provided so far (NPE on line 28 of PasswordChange.class), it looks like this line is causing the issue:
bchange = (Button) findViewById(R.id.bChange);
bchange.setOnClickListener(new View.OnClickListener() {
...
You have a button named bChange in both of your layout xml files - change one of them into something unique. I highly recommend that all components should be uniquely named through the app, as that stops the possibility of an activity accidentally referencing the wrong resource.
Example:
activity_password_change.xml
...
<Button
android:id="#+id/bPasswordChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
android:layout_weight="0.5"
android:text="#string/confirm" />
...
PasswordChange.java
...
bchange = (Button) findViewById(R.id.bPasswordChange);
...
For the second issue that's arisen in MainActivity.java:
Intent intentbool = getIntent();
bmain = intentbool.getBooleanExtra("bool", bmain);
However, your problem here lies with line 20:
Boolean bmain;
Because you've declared a Boolean object rather than a boolean primitive (note the capital B), it is null by default as you've not initialised it. Change it to a primitive:
boolean bmain;
Make sure you have defined PasswordChange.class in your Manifest.xml file.
An example would be <activity android:name=".PasswordChange"/>. That's the most common reason for an app to crash when going to a new activity.
I have two Activities MainActivity and Register when i intent from MainActivity to Register the button listener in Register class is not working.
Here is the code
MainActivity
public class MainActivity extends AppCompatActivity {
// defining all the components
AlertDialog builder;
public EditText Email,Password;
public ImageButton fb,twitter;
public Button register;
public String email;
public String password;
public BackgroungWorker backgroungWorker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
builder = new AlertDialog.Builder(this).create();
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
Email= (EditText) findViewById(R.id.email);
Password= (EditText) findViewById(R.id.pass);
fb= (ImageButton) findViewById(R.id.fb_button);
twitter= (ImageButton) findViewById(R.id.tw_button);
register = (Button) findViewById(R.id.register);
}
// for listening to the events
public void eventHappend(View view)
{
if(view.getId()==R.id.login)
{
email = Email.getText().toString();
password = Password.getText().toString();
if(email.equals(""))
{
showMessage("Error","Email cannot be blank");
return;
}
if(password.equals(""))
{
showMessage("Error","Enter your password");
return;
}
backgroungWorker = new BackgroungWorker(this);
backgroungWorker.execute("Login",email,password);
}
if(view.getId()==R.id.register)
{
// INTENT TO REGISTER ACTIVITY
Intent intent = new Intent(MainActivity.this,Register.class);
startActivity(intent);
}
if(view.getId()==R.id.forgot)
{
}
if(view.getId()== R.id.fb_button)
{
showToast("Facebook!!");
}
if(view.getId()==R.id.tw_button)
{
showToast("Twitter");
}
}
public void showToast(String s)
{
Toast toast = new Toast(this);
toast.makeText(this,s, Toast.LENGTH_SHORT).show();
}
public void showMessage(String title,String message)
{
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(true);
builder.show();
}}
Register
public class Register extends Activity {
String user_name,pass,confim_password,email;
AlertDialog builder;
private EditText name,mail,password,confirm_pass;
private Button register;
private BackgroungWorker backgroungWorker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
mail = (EditText) findViewById(R.id.email);
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.pass);
confirm_pass= (EditText) findViewById(R.id.confirmpass);
register = (Button) findViewById(R.id.register_btn);
builder = new AlertDialog.Builder(this).create();
}
public void registerClicked(View view) // this is not working
{
if(view.getId()==R.id.register_btn)
{
if(name.getText().equals(""))
{
showMessage("Error","Name Cannot be left blank");
return;
}
if(mail.getText().equals(""))
{
showMessage("Error","Email Cannot be left blank");
return;
}
if(password.getText().equals(""))
{
showMessage("Error","Password Cannot be left blank");
return;
}
if(password.getText().equals(confirm_pass.getText().toString()))
{
backgroungWorker = new BackgroungWorker(this);
backgroungWorker.execute("Register",name.getText().toString(),password.getText().toString(),mail.getText().toString());
}
}
}
public void showMessage(String title,String message)
{
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(true);
builder.show();
}}
register.xml
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:background=
android:backgroundTint="#02b864">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#02b864">
<ImageView
android:layout_width="212dp"
android:layout_height="176dp"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal"
android:background="#drawable/splash"
android:layout_marginTop="20dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Name"
android:id="#+id/name"
android:drawableLeft="#drawable/ic_username"
android:drawableTint="#color/colorPrimaryDark"
android:drawablePadding="#dimen/activity_vertical_margin"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:singleLine="true"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Email"
android:id="#+id/email"
android:drawableLeft="#drawable/ic_username"
android:drawableTint="#color/colorPrimaryDark"
android:drawablePadding="#dimen/activity_vertical_margin"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:singleLine="true"
android:layout_marginTop="5dp"
/>
<EditText
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Password"
android:drawableLeft="#drawable/ic_pwd"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTint="#color/colorPrimaryDark"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginRight="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:singleLine="true" />
<EditText
android:inputType="textPassword"
android:ems="10"
android:id="#+id/confirmpass"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:hint="Confirm Password"
android:drawableLeft="#drawable/ic_pwd"
android:drawablePadding="#dimen/activity_horizontal_margin"
android:drawableTint="#color/colorPrimaryDark"
android:focusable="true"
android:background="#drawable/editext_bg_gray"
android:layout_gravity="center_horizontal"
android:layout_marginRight="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:singleLine="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/register_btn"
android:onClick="registerClicked"
android:clickable="true"
android:layout_gravity="center_horizontal"
android:background="#e5e7e8"
android:text="Register"
android:src="#drawable/editext_bg_gray"
android:textColor="#0fbb6c"
android:singleLine="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="50dp"
android:textStyle="normal"
android:textAlignment="center"
android:textSize="22dp"
/>
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>`
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.umarsafdar.dev96.chat">
<uses-permission android:name="android.permission.INTERNET"/>
<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:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Splash"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Register"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.DEFAULT" />
</action>
</intent-filter>
</activity>
</application>
</manifest>
thanks in advance.
I have an ImageButton that when I want it to be pressed to launch a second Activity. The second activity is called "Dust 2" as seen in my android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pete.smokesapp" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".homepage"
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=".Dust2"
android:label="#string/title_activity_dust2" >
</activity>
</application>
</manifest>
I tried following a guide online but got me as far as this stage now my app crashed on launch.
package pete.smokesapp;
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.Toast;
public class homepage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this);
}
public void imgDust2(View view) {
switch (view.getId()){
case R.id.imgDust2:
Toast.makeText(this, "Dust 2 Clicked", Toast.LENGTH_LONG ).show();
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.menu_homepage, 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);
}
Here is also my main activity, this is the activity with the ImageButton, i am also new to this so sorry if I seem clueless.
<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">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Maps"
android:id="#+id/txtMaps"
android:layout_weight="0.44" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgDust2"
android:clickable="false"
android:src="#drawable/dust2"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgInferno"
android:layout_below="#+id/txtOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp"
android:src="#drawable/inferno" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgOverpass"
android:src="#drawable/overpass"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgMirage"
android:clickable="false"
android:src="#drawable/mirage"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgCache"
android:clickable="false"
android:src="#drawable/cache"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgNuke"
android:clickable="false"
android:src="#drawable/nuke"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
There are two methods available in android using which you can go from one Activity to another.
1. Use button.setOnClickListener()
Create a button in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Now set event listener for the button in your .class file
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the event you want to perform when button is clicked
//you can go to another activity in your app by creating Intent
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
});
2. Use <android:onClick="goNext">
Put the onClick as the attribute of the button you have created in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goNext" />
Now in your .class file define an event for that button as,
goNext() {
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
In your first activity do this:
Button button = (Button) findViewById(R.id.imgDust2);
button.setOnClickListener(new View.OnClickListener () {
#Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(), dust2.class);
startActivity(intent);
}
});
Try the following:
Add implements OnClickListener to your class definition: public class homepage extends ActionBarActivity implements OnClickListener
An error will appear and ask you to add unimplemented methods. Do so.
Change findViewById(R.id.imgDust2).setOnClickListener((View.OnClickListener) this); to findViewById(R.id.imgDust2).setOnClickListener(this);
Place the stuff you want to do when the button is clicked into the new method that was added in step 2.
Set onClick to the xml imageView and put the name of the method:
<ImageButton
android:layout_width="350dp"
android:layout_height="150dp"
android:id="#+id/imgDust2"
android:clickable="false"
android:src="#drawable/dust2"
android:layout_above="#+id/imgOverpass"
android:layout_alignLeft="#+id/imgOverpass"
android:layout_alignStart="#+id/imgOverpass"
android:layout_marginBottom="10dp"
android:onClick="imgDust2"/>
The Start class:
It as a very simple program, i designed two screen, and by pressing a button on the main screen i wanted the app to open the second screen, but unfortunately its not happening, the app keeps crushing over and over again.
package com.example.snakesnladders;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Start extends Activity implements OnClickListener {
Button start, settings;
TextView snakes, and, ladders;
ImageView snakePic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
init();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
private void init() {
start = (Button) findViewById(R.id.btStart);
settings = (Button) findViewById(R.id.btSettings);
snakes = (TextView) findViewById(R.id.tvSnakes);
and = (TextView) findViewById(R.id.tvAnd);
ladders = (TextView) findViewById(R.id.tvLadders);
snakePic = (ImageView) findViewById(R.id.snakePic);
start.setOnClickListener(this);
settings.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btStart:
break;
case R.id.btSettings:
Intent i = new Intent("com.example.snakesnladders.SET");
startActivity(i);
break;
default: break;
}
}
}
The Set class:
package com.example.snakesnladders;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Set extends Activity implements OnClickListener {
Button sound, difficulty, back;
TextView settings;
ImageView snakePic;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.setscreen);
init();
}
private void init() {
sound = (Button) findViewById(R.id.btSound);
difficulty = (Button) findViewById(R.id.btDifficulty);
back = (Button) findViewById(R.id.btBack);
settings = (TextView) findViewById(R.id.tvSetPage);
snakePic = (ImageView) findViewById(R.id.setSnakePic);
sound.setOnClickListener(this);
difficulty.setOnClickListener(this);
back.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.btSound:
String s = sound.getText().toString();
if (s.equals("Sound:on")) {
sound.setText("Sound:off");
ControlSounds.player.stop();
} else {
sound.setText("Sound:on");
ControlSounds.player.start();
}
break;
case R.id.btDifficulty:
break;
case R.id.btBack:
Intent i = new Intent(Set.this, Start.class);
startActivity(i);
finish();
break;
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snakesnladders"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Start"
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=".Set"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.snakesnladders.SET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
mainscreen.xml:
<?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:background="#color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="175dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSnakes"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Snakes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="30sp" />
<TextView
android:id="#+id/tvAnd"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="#string/and"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="30sp" />
<TextView
android:id="#+id/tvLadders"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Ladders"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/green"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="125dp"
android:orientation="vertical" >
<Button
android:id="#+id/btStart"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Start New Game"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btSettings"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Settings"
android:textColor="#FFFFFF"
android:textSize="30sp" />
</LinearLayout>
<ImageView
android:id="#+id/snakePic"
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_gravity="center"
android:layout_weight="0.47"
android:background="#color/black"
android:src="#drawable/snake" />
</LinearLayout>
setscreen.xml:
<?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:background="#color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSetPage"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center"
android:text="Settings"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/green"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:orientation="vertical" >
<Button
android:id="#+id/btSound"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Sound:on"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btDifficulty"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Difficulty:easy"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button
android:id="#+id/btBack"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Back To Menu"
android:textColor="#FFFFFF"
android:textSize="30sp" />
</LinearLayout>
<ImageView
android:id="#+id/setSnakePic"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.25"
android:background="#color/black"
android:src="#drawable/snake1" />
</LinearLayout>
Change this
Intent i = new Intent("com.example.snakesnladders.SET");
startActivity(i);
To
Intent i = new Intent(Start.this,Set.class);
startActivity(i);
And Change this
<activity
android:name=".Set"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.snakesnladders.SET" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
To
<activity
android:name=".Set"
android:label="#string/app_name" >
</activity>
Use Explicit Intent's
To know why read
http://developer.android.com/guide/components/intents-filters.html
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.
I have this problem that just seems strange.
I have this Android View with six buttons and when i click on one of them(the only one with the action defined apart from the exit button), it gives me a RuntimeError.
The Activity is declared on the manifest, and this is why i don't understand my error.
Can someone please help me with this.
I would really appreciate it :)
So here's my code:
package com.example.calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View createButton = findViewById(R.id.btn_create);
View deleteButton = findViewById(R.id.btn_delete);
View moveButton = findViewById(R.id.btn_move);
View searchButton = findViewById(R.id.btn_search);
View translateButton = findViewById(R.id.btn_translate);
View exitButton = findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
}
#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 void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_create:
Intent i = new Intent(this, CreateAppointment.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}
My layout is like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="225dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin" >
<CalendarView
android:id="#+id/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="2dip"
android:layout_marginTop="2dip"
android:stretchColumns="*" >
<TableRow>
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
<Button
android:id="#+id/btn_viewEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/viewEdit" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/delete" />
<Button
android:id="#+id/btn_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/move" />
</TableRow>
<TableRow>
<Button
android:id="#+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/search" />
<Button
android:id="#+id/btn_translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/translate" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/exit"/>
</LinearLayout>
And the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calendar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.calendar.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=".CreateAppointment"
android:theme="#android:style/Theme.Dialog"
android:label="#string/create" >
</activity>
</application>
</manifest>
You have to define correctly what kind of views ares using in onCreate()
View createButton = findViewById(R.id.btn_create);
for example:
since you are using a Button:
<Button
android:id="#+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/create" />
Make the cast as button:
Button createButton = (Button)findViewById(R.id.btn_create);
try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createButton = (Button)findViewById(R.id.btn_create);
Button deleteButton = (Button)findViewById(R.id.btn_delete);
Button moveButton = (Button)findViewById(R.id.btn_move);
Button searchButton = (Button)findViewById(R.id.btn_search);
Button translateButton = (Button)findViewById(R.id.btn_translate);
Button exitButton = (Button)findViewById(R.id.exit);
createButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
moveButton.setOnClickListener(this);
searchButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
exitButton.setOnClickListener(this);
}