Automatic Click Button For Android - android-button

I'm new in Android Development. I reviewed the automatic click button post before but I still cannot configure the these error. The App Stop Working after 5 secs timer.
Below is my code on MainActivity:
package com.example.cynog.autobutton;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private Button button1;
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
performClick();
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
button1.performClick();
}
}
};
timer.start();
} catch (Resources.NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void performClick() {
button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, activity.class);
startActivity(i);
}
});
}
}
And This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.cynog.autobutton.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="performClick"
tools:layout_editor_absoluteX="153dp"
tools:layout_editor_absoluteY="209dp" />
</android.support.constraint.ConstraintLayout>

Button is created in the ui thread that is main thread.
so if u want to make any action with it do it inside handler.post(new Runnable(){void run(){//perform action here}. make a Handler object in oncreate(). Handler handler=new Handler().
and put the handler.post() code in the timer thread run method after sleep. I hope it works.

Related

Why doesn't my radioButton get checked or unchecked?

I followed a tutorial to check a single radioButton. I tried to change it so you can change multiple radioButtons.
I succeeded to get the value out of the arraylist and show it on the screen, but when I click on it doesn't change. Only the radioButton that is unchecked get checked. Can anyone help me with this.
public interface ItemClickListener {
//Create method
void onClick(String s);
}
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//Initialize variable
RecyclerView recyclerView;
ItemClickListener itemClickListener;
MainAdapter adapter;
ArrayList<String> arrayList;
ArrayList<Boolean> arrayList_B;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assign variable
recyclerView = findViewById(R.id.recycler_view);
fillArrayList();
//Initialize listener
itemClickListener = new ItemClickListener() {
#Override
public void onClick(String s) {
//Notify adapter
recyclerView.post(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
}
});
//Display toast
Toast.makeText(getApplicationContext(),
"Selected : " + s, Toast.LENGTH_SHORT).show();
}
};
//Set layout manager
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Initialize adapter
adapter = new MainAdapter(arrayList, arrayList_B, itemClickListener);
//Set adapter
recyclerView.setAdapter(adapter);
}
public void fillArrayList() {
//initialize array list
arrayList = new ArrayList<>();
arrayList_B = new ArrayList<>();
//Use for loop
for (int i = 0; i < 10; i++) {
//Add values in array list
arrayList.add("RB " + i);
arrayList_B.add(true);
}
arrayList_B.set(3,false);
}
}
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
//Initialize variable
ArrayList<String> arrayList;
ArrayList<Boolean> arrayList_B;
ItemClickListener itemClickListener;
int selectedPosition = -1;
//Create constructor
public MainAdapter(ArrayList<String> arrayList, ArrayList<Boolean> arrayList_B, ItemClickListener itemClickListener) {
this.arrayList = arrayList;
this.itemClickListener = itemClickListener;
this.arrayList_B = arrayList_B;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//Initialize view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_main, parent, false);
//Pass holder view
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
//Set text on radio button
holder.radioButton.setText(arrayList.get(position));
//Set true or false radio button
holder.radioButton.setChecked(arrayList_B.get(position));
//Set listener on radio button
holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//When checked
//Update selected position
selectedPosition = holder.getAdapterPosition();
//if (arrayList_B.get(selectedPosition).equals(true)) {
//if (b == true) {
// arrayList_B.set(selectedPosition, false);
//}
//if (b == false) {
//
//}
arrayList_B.set(selectedPosition, b);
holder.radioButton.setChecked(arrayList_B.get(selectedPosition));
//Call listener
//Get RadioButton name.
itemClickListener.onClick(holder.radioButton.getText().toString());
//set array list radiobutton
}
});
}
#Override
public long getItemId(int position) {
//Pass position
return position;
}
#Override
public int getItemViewType(int position) {
//Pass position
return position;
}
#Override
public int getItemCount() {
//Pass total List size
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//Initialize variable
RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
//Assign variable
radioButton = itemView.findViewById(R.id.radiobutton);
}
}
}
The Layout: activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view"
tools:listitem="#layout/item_main"/>
</RelativeLayout>
The Layout: item_main.xml
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radiobutton"
android:padding="12dp"
android:textSize="18sp"
android:textColor="#android:color/darker_gray"/>

What is making my app crash without an error

I am writing an app. that you can search info about celebs and save it to a SQlite database. I followed tutorials in showing the information but when I run the app it crashes without an error... please assist
When I request the data I only want the names display in a Listview, then when the user clicks on the name of the Celeb, it will open a page with all the saved data..I have no idea where to even start looking for the problem..I can open the app, save the data but when I open the activity to show the ListView it crashes without an error...
my xml for displaying the listview
<?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="wrap_content"
android:background="#drawable/mway"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#color/colorPrimaryDark"
android:text="#string/app_name"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_view"
android:background="#drawable/mway">
</ListView>
</LinearLayout>
Here is my java file for displaying the listview
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class viewceleb extends AppCompatActivity {
DatabaseHelper db;
ArrayList<String> listItems;
ArrayAdapter adapter;
ListView userList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewceleb);
userList = findViewById(R.id.list_view);
listItems = new ArrayList<>();
viewData();
userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
String text = userList.getItemAtPosition(i).toString();
Toast.makeText(viewceleb.this, "Now something can happen", Toast.LENGTH_LONG).show();
}
});
}
private void viewData() {
Cursor cursor = db.viewData();
if (cursor.getCount()== 0){
Toast.makeText(this,"No Data To Show", Toast.LENGTH_LONG).show();
}else {
while(cursor.moveToNext()){
listItems.add(cursor.getString(1));//index 1 is name, 0 is ID
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
userList.setAdapter(adapter);
}
}
}
You are only declaring db, it needs to be instantiated.
That is you have DatabaseHelper db; and you additionall need db = new DatabaseHelper(?); where ? are the parameters required by the DatabaseHelper constructor. Typically just the Context so perhaps db = new DatabaseHelper(this);. This would be coded in the activity's onCreate method BEFORE calling the viewData method.
e.g.
public class viewceleb extends AppCompatActivity {
DatabaseHelper db;
ArrayList<String> listItems;
ArrayAdapter adapter;
ListView userList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewceleb);
dn = new DatabaseHelper(this); //<<<<<<<<<< ADDED note just the context has been assumed, the parameters depend upon the constructor as per what is coded in the DatabaseHelper class.
userList = findViewById(R.id.list_view);
listItems = new ArrayList<>();
viewData();
userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
String text = userList.getItemAtPosition(i).toString();
Toast.makeText(viewceleb.this, "Now something can happen", Toast.LENGTH_LONG).show();
}
});
}
private void viewData() {
Cursor cursor = db.viewData();
if (cursor.getCount()== 0){
Toast.makeText(this,"No Data To Show", Toast.LENGTH_LONG).show();
}else {
while(cursor.moveToNext()){
listItems.add(cursor.getString(1));//index 1 is name, 0 is ID
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);
userList.setAdapter(adapter);
}
}
}

Using Picasso Images not showing in Recycler view

I am trying to show images from a Firebase database in recycler view in android studio but the images are not displaying. I am not sure where I have gone wrong or what the problem is.
Main Activity.xml
<?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:orientation="vertical"
tools:context="com.test.test.AddBrandPage"
android:weightSum="1">
<TextView
android:id="#+id/editText"
android:layout_width="383dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Select your favourite stores..."
android:textAlignment="center"
android:textSize="22sp" />
<LinearLayout
android:id="#+id/alphaindex"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"></LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recylView"
android:layout_width="337dp"
android:layout_height="370dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/editText2"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="24dp"
android:layout_weight="0.23"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.11"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="#+id/btn_skip"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:background="#color/colorPrimary"
android:text="Skip"
android:textAllCaps="true"
android:textColor="#android:color/background_light"
android:textSize="20sp"
android:textStyle="normal|bold" />
<Button
android:id="#+id/btn_submit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="20dp"
android:background="#color/colorPrimary"
android:text="Submit"
android:textAllCaps="true"
android:textColor="#android:color/background_light"
android:textSize="20sp"
android:textStyle="normal|bold" />
</LinearLayout>
</LinearLayout>
Logo_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:id="#+id/logo_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/darker_gray"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:visibility="visible"
cardView:layout_collapseParallaxMultiplier="1.0">
<android.support.v7.widget.CardView
android:layout_width="155dp"
android:layout_height="100dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
cardView:cardBackgroundColor="#android:color/white">
<GridLayout
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/img_logo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_row="0"
android:paddingBottom="5dp"
android:paddingLeft="2.5dp"
android:paddingRight="2.5dp"
android:paddingTop="5dp">
</ImageView>
</GridLayout>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="66dp"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
LogoItems.java
public class LogoItems {
//declare variables(the items displayed in the layout)
private String logo;//, name;
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
}
Main Activity.java
package com.test.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AddBrandPage extends AppCompatActivity implements OnClickListener {
//declare variables
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
private DatabaseReference myRef;
private Button btn_skip;
private Button btn_submit;
//private String name;
//private String url;
List<LogoItems> brandLogo = new ArrayList<>();
//HashMap<String, String> dataSet = new HashMap<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_brand_page);
//initialize variables
btn_skip = (Button) findViewById(R.id.btn_skip);
btn_submit = (Button) findViewById(R.id.btn_submit);
myRef = FirebaseDatabase.getInstance().getReference().child("/brands");
// set the main recyclerview view in the layout
recyclerView = (RecyclerView) findViewById(R.id.recylView);
recyclerView.setHasFixedSize(true);
// set the main layoutManager of the recyclerview
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
loadLogoImgData();
// set the recycler view adapter
adapter = new LogoAdapter(brandLogo, getBaseContext());
recyclerView.setAdapter(adapter);
//set the listener for the buttons click event
btn_skip.setOnClickListener(this);
btn_submit.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view == btn_skip) {
//if skip button clicked close current window and go to user main page
finish();
startActivity(new Intent(getApplicationContext(), UserMainPage.class));
}
}
public void loadLogoImgData(){
brandLogo.clear();
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot brandSnapshot : dataSnapshot.getChildren()){
LogoItems value = brandSnapshot.getValue(LogoItems.class);
LogoItems brandDetails = new LogoItems();
// String name = value.getName();
String logos = value.getLogo();
//brandDetails.setName(name);
brandDetails.setLogo(logos);
brandLogo.add(brandDetails);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
LogoItemsAdapter.java
package com.test.test;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class LogoAdapter extends RecyclerView.Adapter<LogoAdapter.LogoViewHolder> {
List<LogoItems> brandLogo = new ArrayList<>();
// private AddBrandPage addBrandPage;
private Context context;
public LogoAdapter(List <LogoItems> brandLogo, Context context){
this.brandLogo = brandLogo;
this.context = context;
//addBrandPage = (AddBrandPage)context;
}
#Override
public LogoAdapter.LogoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.logo_items,parent,false);
LogoViewHolder logoViewHolder = new LogoViewHolder(view);
return logoViewHolder;
}
#Override
public void onBindViewHolder(LogoAdapter.LogoViewHolder holder, int position) {
//holder.logo.setImageURI(Uri.parse(brandLogo.get(position).getLogo()));
Picasso.with(context).load(brandLogo.get(position).getLogo()).into(holder.logo);
}
#Override
public int getItemCount() {
return brandLogo.size();
}
public static class LogoViewHolder extends RecyclerView.ViewHolder{
//declare variables
public ImageView logo;
CheckBox checkbox;
//private View itemView;
public LogoViewHolder(View itemView){
super(itemView);
//initialize variables inside the constructor
logo = (ImageView)itemView.findViewById(R.id.img_logo);
checkbox = (CheckBox)itemView.findViewById(R.id.checkbox);
// this.itemView = itemView;
}
}
}
your code seems a bit vague like here :
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot brandSnapshot : dataSnapshot.getChildren()){
LogoItems value =
brandSnapshot.getValue(LogoItems.class);
LogoItems brandDetails =
new LogoItems();
// String name = value.getName();
String logos = value.getLogo();
//brandDetails.setName(name);
brandDetails.setLogo(logos);
brandLogo.add(brandDetails); } }
Change this code to :
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot brandSnapshot : dataSnapshot.getChildren()){
LogoItems value =
brandSnapshot.getValue(LogoItems.class);
brandLogo.add(value); }
startRecyclerView();
}
Firebase methods are asynchronous, your present code inflates the recylcerview synchronously which means it is loaded before the data has been set and received.
The changed code starts recyclerview asynchronously after the data has been loaded from the database. You can add a progress bar to know the status. You can set it like this (Initialize it before this method and it will be visible by default) :
mProgressBar.setVisibility(View.INVISIBLE);
startRecyclerView();
This way the code looks organised.

while running robotium error messge has been displayed "does not have a signature matching the target com.android.calculator2"

Here is my code
package com.test.android.calculator2;
import android.app.Activity;
import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
#SuppressWarnings("unchecked")
public class TestApk extends ActivityInstrumentationTestCase2 {
private static final String TARGET_PACKAGE_ID="com.android.calculator2";
private static final String
LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.android.calculator2.Calculator";
private static Class <?> launcherActivityClass;
static {
try {
Activity act = new Activity();
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
// #SuppressWarnings("unchecked")
public TestApk()throws ClassNotFoundException {
super(TARGET_PACKAGE_ID,launcherActivityClass);
}
private Solo solo;
#Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation());
}
public void testCanOpenSettings() {
solo.getActivityMonitor();
getActivity();
solo.sendKey(Solo.DOWN);
solo.goBack();
}
#Override
public void tearDown() throws Exception {
try {
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
Manifest file like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.android.calculator2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="19" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.android.calculator2" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
It looks like your defined target package com.android.calculator2 could not be found.
I see that the class you posted is located in the package com.test.android.calculator2
You either have to create a new package com.android.calculator2 or set your target package to a existing package (e.g. com.test.android.calculator2).

Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException

I created a simple project to launch facebook app on emulator. When the program is run the following error is displayed
Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException
The code is fb.java file
package com.facebook.katana;
import junit.framework.Assert;
import com.jayway.android.robotium.solo.Solo;
//import junit.framework.Assert;
import android.test.ActivityInstrumentationTestCase2;
#SuppressWarnings("rawtypes")
public class fb extends ActivityInstrumentationTestCase2{
private Solo solo;
private static final String TARGET_PACKAGE_ID ="com.facebook.katana";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.facebook.katana.LoginActivity";
private static Class<?> launcherActivityClass;
static{
try{
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch(ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
#SuppressWarnings({ "unchecked", "deprecation" })
public fb() {
super(TARGET_PACKAGE_ID, launcherActivityClass);
// TODO Auto-generated constructor stub
}
#Override
protected void setUp() throws Exception{
solo = new Solo(getInstrumentation(), getActivity());
}
public void testCanOpenSettings() throws InterruptedException{
// wait for the specified time
solo.sleep(3000);
solo.clearEditText(0);
solo.enterText(0, "abc#abc");
// wait for the specified time
solo.sleep(3000);
solo.enterText(1, "acd");
solo.sleep(3000);
}
private boolean assertTrue(Object clickOnImage) {
// TODO Auto-generated method stub
return false;
}
#Override
public void tearDown() throws Exception{
try{
solo.finalize();
} catch(Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
The manifest. xml file (FB2 Manifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.facebook.katana.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="18" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.facebook.katana" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Please help in resolving this issue
The LAUNCHER ACTIVITY FULL CLASS NAME specified was incorrect.
Once this was set to the correct activity name, the program execution was successful.