I have been on this error now for a while. I am trying to open a list view in my dialog but its not opening for me this is my code.
LOGCAST
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mybasicapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Slashscreen"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".menu"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".tutorialsone"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.mybasicapp.TUTORIALSONE" />
<category android:name="android.intent.category.DEFAULT" />
Java code:
public class menu extends Activity{
Button ImagebuttonOpenDialog;
String KEY_TEXTPSS = "TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
String[] listContent = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImagebuttonOpenDialog = (Button)findViewById(R.id.imgbtn1);
ImagebuttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});
}
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(menu.this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Custom Dialog");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnCancelListener",
Toast.LENGTH_LONG).show();
}});
dialog.setOnDismissListener(new OnDismissListener(){
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
"OnDismissListener",
Toast.LENGTH_LONG).show();
}});
//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
dialog_ListView.setAdapter(adapter);
dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(menu.this,
parent.getItemAtPosition(position).toString() + " clicked",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}});
break;
}
return dialog;
}
#Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);
switch(id) {
case CUSTOM_DIALOG_ID:
//
break;
}
}
}
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:background="#drawable/carbon" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:background="#drawable/bottombar" >
</FrameLayout>
<ImageButton
android:id="#+id/imgbtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="42dp"
android:layout_marginTop="79dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imgbtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imgbtn1"
android:layout_marginRight="46dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgbtn1"
android:layout_below="#+id/imgbtn1"
android:layout_marginTop="36dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgbtn2"
android:layout_alignTop="#+id/imageButton3"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton3"
android:layout_below="#+id/imageButton3"
android:layout_marginTop="49dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton4"
android:layout_alignTop="#+id/imageButton5"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton5"
android:layout_below="#+id/imageButton5"
android:layout_marginTop="42dp"
android:src="#android:drawable/btn_star" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton6"
android:layout_alignTop="#+id/imageButton7"
android:src="#android:drawable/btn_star" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/kj" />
custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customdialog"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:minWidth="300dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
<ListView
android:id="#+id/dialoglist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/kj" />
</LinearLayout>
ALSO
I looked over the java code again and found that my code had (button) where in my XML had image button i have change my java code so you can see where i am at the moment..still no luck.I don't know how to copy log cast
From my count, buttonOpenDialog is null so you are getting NPE when you try to add a listener to it. Check your activity_main that you have a Button with an id of im1. You probably misspelled the id or that one simply doesn't exist in your layout. If you don't see a problem with that then post the activity_main.xml.
Also, this isn't your problem but you should consider using Java standard naming conventions. Class names should start with a capital letter. So you would have Main instead of main as your class name.
When you post a question please post the logcat in your question as it always makes it easier for members to read then needing to follow a link that sometimes isn't of the best quality.
Related
So Ive made my first animation screen for my phone, this works perfect, and now I wanted to try it out on my tablet, but it crashes.
The tablet is a Lenovo TAB3 7 Essential / Samsung SM-T550.
As you can see in the picture above it's line 34 setContentView(R.layout.activity_startscreen); the error occurs.
I've tried to Clean my project, but i think it might be in the .xml file?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#73FFFFFF"
android:orientation="vertical"
tools:context=".activitys.Startscreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/first_line"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:background="#drawable/blue_line" />
<View
android:id="#+id/second_line"
android:layout_width="20dp"
android:layout_height="200dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/first_line"
android:background="#drawable/gray_line" />
<View
android:id="#+id/third_line"
android:layout_width="20dp"
android:layout_height="150dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/second_line"
android:background="#drawable/blue_line" />
<View
android:id="#+id/fourth_line"
android:layout_width="20dp"
android:layout_height="250dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/third_line"
android:background="#drawable/gray_line" />
<View
android:id="#+id/fifth_line"
android:layout_width="20dp"
android:layout_height="100dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/fourth_line"
android:background="#drawable/blue_line" />
<View
android:id="#+id/six_line"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/fifth_line"
android:background="#drawable/gray_line" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp">
<ImageView
android:id="#+id/wrd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:srcCompat="#drawable/logo_wrd" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-black"
android:text="Wireless Rehab Device"
android:textColor="#676767"
android:textSize="24sp" />
</RelativeLayout>
</LinearLayout>
The activity I showed Startscreen.java
public class Startscreen extends AppCompatActivity {
private static int SPLASH_DURTION_OUT = 5000; // 5000ms = 5s
//Views
View first, second, third, fourth, fifth, sixth;
TextView bottomTag;
ImageView wrd;
//Animations
Animation topAnimation, bottomAnimation, middelAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_startscreen);
topAnimation = AnimationUtils.loadAnimation(this, R.anim.top_animation);
bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);
middelAnimation = AnimationUtils.loadAnimation(this, R.anim.middel_animation);
first = findViewById(R.id.first_line);
second = findViewById(R.id.second_line);
third = findViewById(R.id.third_line);
fourth = findViewById(R.id.fourth_line);
fifth = findViewById(R.id.fifth_line);
sixth = findViewById(R.id.six_line);
wrd = findViewById(R.id.wrd);
bottomTag = findViewById(R.id.tag);
//Animation settings
first.setAnimation(topAnimation);
second.setAnimation(topAnimation);
third.setAnimation(topAnimation);
fourth.setAnimation(topAnimation);
fifth.setAnimation(topAnimation);
sixth.setAnimation(topAnimation);
wrd.setAnimation(middelAnimation);
bottomTag.setAnimation(bottomAnimation);
//Splash Screen
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(Startscreen.this, Home.class); //Home = Mainactivity
startActivity(intent);
finish();
}
}, SPLASH_DURTION_OUT);
}
}
This is my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wrd">
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".activitys.Home" />
<activity android:name=".activitys.Startscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ble.BLE_Service"
android:enabled="true" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
Found the issue, all my .png images was in my Drawable-v24 folder and not Drawable, so it's fixed now!
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 am trying to create 8 buttons in one activity that will open their own activity. What am I missing? I am using Android Studio 1.1.0.
I get this error
java.lang.IllegalStateException:
Could not find a method AppleActivity(View) in the activity class
com.hashmi.omar.vodafonemobilephoneshop.Picker for onClick handler
on view class android.widget.Button with id 'button2'
Below is the Picker.class:
package com.hashmi.omar.vodafonemobilephoneshop;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.hashmi.omar.vodafonemobilephoneshop.util.SamsungActivity;
public class Picker extends Activity implements View.OnClickListener {
Button button2, button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker);
//Sets the font to Vodafone Light
Typeface vodaLt = Typeface.createFromAsset(getAssets(), "VODAFONELT.TTF");
TextView vodaHeading = (TextView)findViewById(R.id.textView4);
vodaHeading.setTypeface(vodaLt);
//Sets up a button
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
private void buttonapple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
private void buttonsam(){
startActivity(new Intent(Picker.this, SamsungActivity.class));
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.button2:
buttonapple();
break;
case R.id.button3:
buttonsam();
break;
}
}
}
Below is the activity_picker.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.hashmi.omar.vodafonemobilephoneshop.Picker"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please choose a brand"
android:id="#+id/textView4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffff0000"
android:textSize="31dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/appsel"
android:id="#+id/button2"
android:layout_column="0"
android:onClick="#string/title_activity_apple" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/samselect"
android:id="#+id/button3"
android:layout_column="20"
android:onClick="#string/title_activity_samsung" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/sonyselect"
android:id="#+id/button4"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/htcselect"
android:id="#+id/button5"
android:layout_column="20" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/bbselect"
android:id="#+id/button6"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/nokiaselect"
android:id="#+id/button7"
android:layout_column="20" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"></TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/nexselect"
android:id="#+id/button8"
android:layout_column="0" />
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/lgselect"
android:id="#+id/button9"
android:layout_column="20" />
</TableRow>
</TableLayout>
Here is AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HomeScreen"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Picker"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AppleActivity"
android:label="#string/title_activity_apple"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
<action android:name="com.hashmi.omar.vodafonemobilephoneshop.AppleActivity" />
</activity>
<activity
android:name=".SamsungActivity"
android:label="#string/title_activity_samsung"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
</activity>
<activity
android:name=".util.SamsungActivity"
android:label="#string/title_activity_samsung"
android:parentActivityName=".Picker" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
</activity>
</application>
Remove the simply onClick form your button2 because you are implemententing View.OnClickListener.
<Button
android:layout_width="160dp"
android:layout_height="125dp"
android:background="#drawable/appsel"
android:id="#+id/button2"
android:layout_column="0"
/>
You have written this:
<Button
... other stuff here ...
android:onClick="#string/title_activity_apple" />
If you use #string/title_activity_apple as the value of an attribute, Android will look up the string, and act as if that string's value was the actual value. So if the string has the value "AppleActivity", then it's equivalent to writing android:onClick="AppleActivity".
Next: what does setting android:onClick do? It tells the button the name of a method to call on the current activity when the button is clicked - see here. So you have told the button to call the method AppleActivity in the current activity when the button is pressed. (As you can also see on that page, it also expects to call a method with one parameter of type View - the arguments matter too)
Your activity class (the one containing the button0 does not have a method called AppleActivity which takes a View parameter. So the button can't find the method you told it to call, so it throws an exception (which crashes the app). This is exactly what the exception message said - the button could not find a method AppleActivity(View) in the activity class com.hashmi.omar.vodafonemobilephoneshop.Picker, which it was looking for because of your onClick attribute, on a android.widget.Button with id 'button2'.
You need to create Method named in your xml file in your activity class as well. instead of creating following method
private void buttonapple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
private void title_activity_apple(){
startActivity(new Intent(Picker.this, AppleActivity.class));
}
After above change check again.
I'm new to Android programming and have been searching this problem for a day now and couldn't come up with a solution. Kindly help me in solving this small problem which is creating a big problem for me.
The error is that when I press the login button in my MainActivity, the app says that unfortunately it has stopped working and the debug window shows an error "Source not found" and further it says ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2249
I've seen similar posts related to this but those posts couldn't help me.
Following my code.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Base.AppCompat.Light.DarkActionBar" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second" >
</activity>
<activity
android:name=".Registration"
android:label="#string/title_activity_registration" >
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
final Button button2=(Button) findViewById(R.id.button2_id);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent j = new Intent(MainActivity.this,Registration.class);
startActivity(j);
}
});
}
}
MainActivity_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="com.example.firstapp.MainActivity" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView1"
android:text="Welcome To Foodparkk"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button_id"
android:layout_below="#+id/textView3"
android:layout_marginTop="45dp"
android:contentDescription="#null"
android:src="#drawable/robo" />
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:text="Login" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_alignTop="#+id/button2_id"
android:text="E-Mail"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:layout_marginTop="19dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:ems="10"
android:inputType="textPassword" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:ems="10"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button_id"
android:layout_alignRight="#+id/button_id"
android:layout_marginBottom="42dp"
android:text="Register" />
</RelativeLayout>
activity_second.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="com.example.firstapp.Second">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/spinner1"
android:layout_alignParentTop="true"
android:layout_marginTop="44dp"
android:text="Select your Meal"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
Second.java
public class Second extends ActionBarActivity implements OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.food_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected (AdapterView<?> parent) {
// code
}
}
In your second.java class you are referencing your layout of main activity
public class Second extends ActionBarActivity implements OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Change SetContentView to
setContentView(R.layout.activity_second);
A "Source Not Found" error will be thrown when a class or activity can't be found or instantiated.
Please add the full LogCat log or at least the full ErrorStack.
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.