App was unable to start Android - android

Getting the error "Unable To start myWeb*(my app's name)*"
Can anybody Help me ?
I am a newbie and just started coding android.
I ran this on eclipse and when the app is launched from ADT it gave error!!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myweb"
android:versionCode="1"
android:versionName="1.0"
android:name="testing.android.application.three.MainActivityThreeActivity"
>
<uses-permission android:name="android.permission.INTERNET"
/>
<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/AppTheme" >
<activity
android:name="com.example.myweb.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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="#style/AppTheme"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="#+id/mainLayout"
>
<EditText
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp"
android:ems="10"
android:textColor="#00FF00"
android:typeface="monospace"
android:hint="Email Address or Phone Number" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/password"
android:layout_centerVertical="true"
android:ems="10"
android:textColor="#00FF00"
android:typeface="monospace"
android:hint="Password"
android:inputType="textPassword"/>
<Button
android:id="#+id/login1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/password"
android:layout_below="#+id/password"
android:layout_marginTop="18dp"
android:minWidth="180dip"
android:text="Log In" />
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/login1"
android:layout_toRightOf="#+id/login1"
android:visibility="invisible" />
</RelativeLayout>
MainActivity.java
package com.example.myweb;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton button = (ImageButton) findViewById(R.id.login1);
EditText emailBox = (EditText)findViewById(R.id.email);
EditText passwordBox = (EditText)findViewById(R.id.password);
final String emailId = emailBox.getText().toString();
final String passwordId = passwordBox.getText().toString();
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
getUserLoggedIn(emailId,passwordId);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
#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;
}
public void getUserLoggedIn(String email,String password) throws ClientProtocolException, IOException{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("localhost/testand.php");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("email", email));
pairs.add(new BasicNameValuePair("password", password));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
}
}

Use AsyncTask like below and call it in onCreate
private class DownloadFilesTask extends AsyncTask<emailId, passwordId> {
protected Long doInBackground(URL... urls) {
getUserLoggedIn(emailId,passwordId);
return totalSize;
}
protected void onPostExecute(Long result) {
Log.v("Webpage","Done");
}
}
and call in onCreate
try {
new DownloadFilesTask().execute(emailId,passwordId);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Solved It!![Solution Here]
Actually this was solved....
In activity_main.xml i used <button> tag but in .java i used ImageButton to call it by id.
Though silly i solved it by using Button class instead of ImageButton in java file
Changed
ImageButton button = (ImageButton) findViewById(R.id.login1);
to
Button button = (Button) findViewById(R.id.login1);
This fix was only specific to my code. So if anybody gets around error like this its time to recheck code rather than hanging around with Questions.

Related

Testing a transition between two activities and going back to the first activity

I am developing an app that requires the user to press a button and it goes to another activity. The problem is that I cannot continue from there. I want to be able to simulate a button press and go back to the previous activity it was on. Here is my code.
package com.example.guy.smsclassproject;
import android.os.Looper;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
/**
* Created by ksl130230 on 11/5/2015.
*/
public class DraftsActivityTest extends ActivityInstrumentationTestCase2<DraftsActivity> {
private DraftsActivity tester;
private EditText searchText;
private Button searchButton;
private DraftsDatabase draftsDatabase;
private MessageObject messageObject1;
private MessageObject messageObject2;
private MessageObject messageObject3;
Button[] draftButtons;
ArrayList<MessageObject> messagesToBeDisplayed;
public DraftsActivityTest() {
super(DraftsActivity.class);
}
#Override
#UiThreadTest
public void setUp() throws Exception {
super.setUp();
if (Looper.myLooper() == null)
{
Looper.prepare();
}
draftsDatabase = new DraftsDatabase();
draftsDatabase.clearData();
messageObject1 = new MessageObject("hi", "5554",null, true);
messageObject2 = new MessageObject("hi hi", "5555554",null, true);
messageObject3 = new MessageObject("sup", "5435555554",null, true);
draftsDatabase.addMessage(messageObject1);
draftsDatabase.addMessage(messageObject2);
draftsDatabase.addMessage(messageObject3);
tester = getActivity();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
searchText = (EditText) tester.findViewById(R.id.searchText);
searchButton = (Button) tester.findViewById(R.id.searchButton);
}
#SmallTest
#UiThreadTest
public void testSearch() {
searchText.setText("hij");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word hi", 0, messagesToBeDisplayed.size());
searchText.setText("sup");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word sup", 1, messagesToBeDisplayed.size());
searchText.setText("yo");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word yo", 0, messagesToBeDisplayed.size());
searchText.setText("i");
searchButton.performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Messages with the word i", 2, messagesToBeDisplayed.size());
}
#SmallTest
#UiThreadTest
public void testRedisplay()
{
assertNotNull(tester.draftButtons[0]);
tester.draftButtons[0].performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("Size of the list after deletion is 2", 2, messagesToBeDisplayed.size()); //presses the first button, which deletes it from the drafts
getActivity();
String buttonText0 = tester.draftButtons[0].getText().toString();
if(buttonText0.equals("5555554: hi hi")) assertSame("Text redisplayed on the first button", buttonText0, messageObject2.toString()); //gets the text of the current button, since the messageobject1 was in draftsButtons0 before, not it should have messageobject2
assertNotNull(tester.draftButtons[1]);
String buttonText1 = tester.draftButtons[1].getText().toString();
if(buttonText1.equals("5435555554: sup"))
assertSame("Text redisplayed on the second button", buttonText1, messageObject3.toString());
}
#SmallTest
#UiThreadTest
public void testMessageButtons()
{
assertNotNull(tester.draftButtons[0]);
tester.draftButtons[0].performClick();
//THE PROBLEM IS LOCATED HERE.
//As soon as I press the button, the app goes to another activity.
//I want it to go back from the activity.
assertNotNull(tester.draftButtons[1]);
tester.draftButtons[1].performClick();
messagesToBeDisplayed = tester.messagesToBeDisplayed;
assertEquals("The draftsDatabase now only contains 1 message", 1, messagesToBeDisplayed.size());
assertNotNull(tester.draftButtons[0]);
tester.draftButtons[0].performClick();
assertNull(draftsDatabase); //after you press all the buttons, the draftsDatabase should be empty because all the messages have been deleted
}
}
This is my code of application which I recently made. If user press a button it goes to another activity from this you will be to simulate a button press and go back to the previous activity and I also have made some modification that you can pass data from text view to second activity
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Intent i = new Intent(this, Main2Activity.class);
final EditText inputText = (EditText) findViewById(R.id.inputText);
String mesg = inputText.getText().toString();
i.putExtra("mm",mesg );
startActivity(i);
}
}
Main2Activity
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Transition;
import android.transition.TransitionManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
ViewGroup RLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle data = getIntent().getExtras();
if(data == null){
return;
}
String mm = data.getString("mm");
final TextView textVieww = (TextView) findViewById(R.id.textVieww);
textVieww.setText(mm);
RLayout = (ViewGroup) findViewById(R.id.RLayout);
RLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
moveButton();
return true;
}
});
}
public void onClickk(View view) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.user.razaali.third" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="#string/title_activity_main2" >
</activity>
</application>
</manifest>
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=".MainActivity"
android:background="#53ff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="First Screen"
android:id="#+id/textView"
android:layout_above="#+id/inputText"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClick" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inputText"
android:width="250dp"
android:layout_above="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp" />
</RelativeLayout>
activity_main2.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.example.razaali.third.Main2Activity"
android:background="#fff306"
android:id="#+id/RLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Secound Screen"
android:id="#+id/textVieww"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change"
android:id="#+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClickk" />
</RelativeLayout>

How do you fully setup an edittext into a ScrollView?

Before I go into detail about the problem I have been having, I am quite new to android development so let me just explain what I would like to achieve and what I have tried to do.
I am creating an android app in eclipse IDE using the android 4.0 operating system and what I would like to create in this app is a windowed scrollview which contains various different widgets such as check boxes, text views and edit texts. These edit texts are required so that the user can input some data about an image that is displayed in an image view on the app page but is not inside the scroll view. The user would then press a button and that data would then be stored somewhere (i know what I want to do with that) and a new image would appear, then the process repeats.
Now in order to create a windowed scrollview, I understand that you need to define the widgets inside a scrollview and to be able to scroll down to them properly is by defining them inside a linear layout and then messing around with the margin top values in each widget in the activity xml file.
However, once i created these widgets tried testing, this is where I am running into some issues.
Firstly, if a widget is placed in between the header and footer ( the region displayed on the app page and used as part of the windowed scrollview) and i try and test out the scrolling, the scrolling works but it creates a random duplicate of that widget which I cannot get rid of e.g http://community.dur.ac.uk/cs.seg05/androidapp/exhibit_A.JPG (I can't display the image to you directly as i am not a high enough rank to display an image on StackOverflow)
In an attempt to try and fix this issue, I decided that I would move everything below the footer of the scrollview and then just scroll up from there and this seemed to have worked.
Unfortunately, when trying to test out inputs into my edit texts, I ran to another annoying problems.
As soon as I finish typing into an edit text and click the next button on the soft keyboard, the app directed me to another edit text which I had not defined and displayed another soft keyboard and this in turn created a duplicate of every widget (including that edit text) that was currently displayed inside the scrollview window. (i had scrolled up in order to get to this edit text) eg
https://community.dur.ac.uk/cs.seg05/androidapp/test.JPG
I then looked on questions such as Scrolling EditText on ScrollView programmatically , How to prevent auto scrolling of EditText when keyboard appears on Android? , Enable Scrolling within EditText that is in a ScrollView
but none of these are really helping me.
I have also tried using AutoTextCompleteView widgets as the widgets that the user woudl use as inputs but I do not fully understand how they work and I have not been able to get any data from them as when i type something into them, the program is returning that nothing has been typed.
I would really appreciate some help on this as I don't know if I am missing something, or if I have not set the widgets up correctly or if I have not set the scrollview correctly so if any of you have any solutions to these problems, please post them (code or a step by step would be very helpful).
Here is my java code in my activity (note that this a second activity that is launched once the first activity is completed).
package com.example.hello;
import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.os.Build;
import android.widget.AdapterView.OnItemClickListener;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
public class SecondActivity extends Activity {
private String userId; //global variable as it will be used throughout this activity
private ArrayList <String> imageNames;
private String directory="http://community.dur.ac.uk/cs.seg05/Wild/CameraImages/";
private int imageNumber=0;
private CheckBox humanTest;
private ArrayList<String> pictureIds;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle data = getIntent().getExtras(); //obtains information passed from the previous activity
userId= data.getString("userId"); //gets the user id from the main activity
System.err.println("Received User id " +userId);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
imageNames =getImages(userId); //gets all of the image names from the database
System.err.println(imageNames);
getNextImage(imageNumber, imageNames); //method that obtains the next image
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_second,
container, false);
return rootView;
}
}
public ArrayList<String> getImages(String userid) { // method that gets images from a php file
ArrayList<String> images= new ArrayList<String>();
pictureIds= new ArrayList<String> ();
try {
HttpClient httpclient= new DefaultHttpClient();
URI website= new URI("https://community.dur.ac.uk/cs.seg05/androidapp/ImageAccess.php?userid="+userid);
HttpGet request= new HttpGet();
request.setURI(website);
HttpResponse response = httpclient.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb=new StringBuffer("");
String l= "";
String nl= System.getProperty("line.separator");
while ((l = reader.readLine()) != null) { //converts the whole output into a string
sb.append(l+ nl);
}
reader.close();
String result=sb.toString();
JSONArray jArray = new JSONArray(result);
System.err.println(jArray.length());
for(int i=0;i<jArray.length();i++){ //retrieves the JSon
JSONObject json_data = jArray.getJSONObject(i);
System.err.println(json_data);
images.add(json_data.getString("File_Name"));
pictureIds.add(json_data.getString("Picture_ID"));
}
}
catch (Exception e) {
System.err.println(e);
}
return images;
}
public static Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public void getNextImage(int number, ArrayList<String> images) { //method that gets the images
if (number>=images.size()) {
String whitespace="http://www.skettymethodistchurch.org.uk/whitespace.jpg" ; //whitespace for when user has finished the subset of images
Bitmap bm= getBitmapFromURL(whitespace);
ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view
iv.setImageBitmap(bm);
}
else {
String image=images.get(number);
String request=directory+image;
Bitmap bm= getBitmapFromURL(request);
ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view
iv.setImageBitmap(bm);
imageNumber++;
}
}
public void loadNextImage(View view) { //method for when the next button is clicked
try {
// humanTest = (CheckBox) findViewById(R.id.checkBox1);
ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view
iv.setImageBitmap(null);
getNextImage(imageNumber, imageNames);
}
catch (Exception e) {
System.err.println(e);
}
}
public void LoadHelpActivity(View view) {
Intent i = new Intent(getBaseContext(), HelpActivity.class);
startActivity(i);
}
public void getScore(View view) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
}
}
Here is the xml file associated with this activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.hello.SecondActivity"
tools:ignore="MergeRootFrame" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="315dp"
android:layout_height="150dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp" />
<!-- Header aligned to top -->
<RelativeLayout
android:id="#+id/header"
android:layout_width="315dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center" >
<TextView
android:layout_width="355dp"
android:layout_height="25dp"
android:layout_marginTop="0dp"
android:text="Scroll up to classify the contents of the image"
android:textColor="#000"
android:textSize="14sp" />
</RelativeLayout>
<!-- Footer aligned to bottom -->
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
/>
</RelativeLayout>
<ScrollView
android:id="#+id/scrollableContents"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
android:isScrollContainer="false"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Humans in image" />
<EditText
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginTop="30dp"
android:ems="13"
/>
<Button
android:id="#+id/button1"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:onClick="loadNextImage"
android:text="#string/classify" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="LoadHelpActivity"
android:text="Help" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:onClick="getScore"
android:text="Check score" />
</RelativeLayout>
Finally, here is the android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hello.MainActivity"
android:label="Wild Eyes" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.hello.SecondActivity"
android:label="Wild Eyes" >
</activity>
<activity
android:name="com.example.hello.HelpActivity"
android:label="Wild Eyes" >
</activity>
</application>
</manifest>
If there is anything that I have missed and that would be beneficial, do let me know and I will post it.
Thanks
Tom

Treasure hunt has stopped working

When I ran the app the first activity worked but as soon as it went to the second activity it said treasure hunt has stopped working, and there was nothing under logcat or console. I am pretty sure it is a problem with level_1.java but can't figure out what. Please help thanks.
Main Activity:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void Start(View view) {
Log.d("hi", "hello");
Intent intent = new Intent(this, Level_1.class);
startActivity(intent);
finish();
}
}
Mainxml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Treasure Hunt" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:text="Start"
android:onClick="Start" />
</RelativeLayout>
`
level_1java:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class Level_1 extends Activity {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
String hint;
String answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
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.level_1, menu);
return true;
}
public void level1() {
editor.putInt("level", 1); // only needs to be done for level 1
editor.commit();
Clue.setText("Bartholdi was my creator, I carry fire and knowledge, what am I?");
hint = "July IV MDCCLXXVI";
answer = "statue of liberty";
}
public void answer() {
String useranswer = edittext.toString();
if (useranswer.equalsIgnoreCase(answer)) {// if they get the correct answer
int leveltest = settings.getInt("level", 1);
editor.putInt("level", leveltest+1); //adds one level to the current level
editor.commit();
}
}
public void hint() { //function that displays the hint
}
}
level_1 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=".Level_1" >
<TextView
android:id="#+id/Clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Clue" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Clue"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/editText"
android:layout_marginBottom="50dp"
android:text="Submit"
android:onClick="answer"/>
<Button
android:id="#+id/Hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Submit"
android:layout_alignBottom="#+id/Submit"
android:layout_alignLeft="#+id/editText"
android:text="Hint"
android:onClick="Hint"/>
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.treasurehunt"
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/AppTheme" >
<activity
android:name="com.example.treasurehunt.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="com.example.treasurehunt.Level_1"
android:label="#string/title_activity_level_1" >
</activity>
</application>
</manifest>
Seems you're having NullPointerException.
On the field declaration
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
Move the assignments to in onCreate()
public class Level_1 extends Activity
{
// Other members ....
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
// Assignemnts should be here
Clue = (TextView)findViewById(R.id.Clue);
edittext = (EditText)findViewById(R.id.editText);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
}

new to android development- cant start a new activity [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
First- sorry if my question a bit dumb, I'm totally new to Android development ...
I'm trying to start a new activity from my "LAUNCHER" activity and run it on the emulator, but every time I click on the "Next" button (which should start my second activity), I get an error msg saying: "unfortunately, Omis hang man free has stopped", and then closes the app...
also, when I re-launch the same app in the emulator without re-installing it from Eclipse, it doesn't even showing me the first screen...
I don't even know where my problem lies, so I attached 5 codes:
1'st activity layout (named "activity_open")
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".OpenActivity"
android:background="#DCDCDC" >
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<TextView
android:id="#+id/tvEnterName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/stEnterName"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="40" >
<EditText
android:id="#+id/etNameInput"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="#string/stHintNameEnter"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<Button
android:id="#+id/bNext"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/stNext"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
1'st activity class (named "OpenActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OpenActivity extends Activity {
TextView tvEnterYourName;
EditText etName;
Button bContinue;
String myName, tmp;
final String errorStr = "\nYou must enter a name!";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
initialize();
bContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
myName = etName.getText().toString();
if(etName.getText().toString().equals("") == true)
{
tvEnterYourName.setText(tmp + errorStr);
tvEnterYourName.setTextColor(Color.RED);
}
else
{
try {
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
private void initialize() {
// TODO Auto-generated method stub
tvEnterYourName = (TextView)findViewById(R.id.tvEnterName);
etName = (EditText)findViewById(R.id.etNameInput);
bContinue = (Button)findViewById(R.id.bNext);
myName = "~";
tmp = tvEnterYourName.getText().toString();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_open, menu);
return true;
}
}
2'nd activity class (named "GameActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;
import android.widget.TextView;
public class GameActivity extends Activity {
TextView tt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
GridView gvGrid = (GridView)findViewById(R.id.gvHagdara);
tt.setText("_");
gvGrid.addView(tt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_game, menu);
return true;
}
}
2'nd activity layout (named "layout_game")
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<GridView
android:id="#+id/gvHagdara"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/textView1"
android:layout_centerInParent="false"
android:numColumns="auto_fit" >
</GridView>
</RelativeLayout>
5. manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omi.hangmanfree"
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/AppTheme" >
<activity
android:name=".OpenActivity"
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=".GameActivity"
android:label="#string/title_activity_game" >
<intent-filter>
<action android:name="com.omi.hangmanfree.GAMEACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I'm not looking for code improvements (I am only learning the language), so please do not send me suggestions to improve the code (I know there are a lot!) --- I just want this code to work.
The problem may be in the Intent. Try this:
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
Please Remove finish(); from onPause() Method from OpenActivity and remove gvGrid.addView(tt); from GameActivity because gridview is not container layout and write below code
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);
instead of
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
it will solve your problem.
please replace in openActivity
Intent gameIntent = new Intent(getApplicationContext(), GameActivity.class);
startActivity(gameIntent);
Try changing here:
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
to
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);

ProgressDialog in Tabbar android

I am trying to make a tab bar application in android, after searching in google i found some tutorial and i succeeded. I want 3 tabs, when i click on 1st tab some data will be taken from server using asyntask and show the progressdialog in frontend, but the progressdialog is capturing the whole screen(with tabbar) and because of that i can't be able to switch the 2nd or 3rd tab.
Expected Output: Whenever i click on any tab a data will be taken from server in background, as i know data is loaded in background so for that time i want to switch 2nd tab or any other tab.
package com.tabexample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class ArrowsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrowspage);
Button back = (Button) findViewById(R.id.BackButton2);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProgressDialog p = new ProgressDialog(getParent());
p.setMessage("Loading");
p.setCancelable(false);
p.show();
p.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
});
}
}
package com.tabexample;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class EditActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListContent));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
startActivity(new Intent(EditActivity.this, OptionsActivity.class));
}
});
}
private static String[] mListContent={"Item 1", "Item 2", "Item 3"};
}
package com.tabexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class OptionsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.optionspage);
Button back = (Button) findViewById(R.id.BackButton1);
Button next = (Button) findViewById(R.id.NextButton1);
}
}
package com.tabexample;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class TabExmapleActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("OPT")
.setContent(new Intent(this, ArrowsActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("EDIT")
.setContent(new Intent(this, EditActivity.class)));
tabHost.setCurrentTab(0);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Arrows Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton2"
android:text="Back" android:layout_below="#+id/TextView02">
</Button>
<Button android:id="#+id/Button01" android:layout_below="#id/BackButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Indeterminate Dialog"></Button>
<Button android:id="#+id/Button02" android:layout_below="#id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Options Page">
</TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/BackButton1"
android:text="Back" android:layout_below="#+id/TextView01">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/NextButton1"
android:text="Next" android:layout_toRightOf="#+id/BackButton1"
android:layout_below="#+id/TextView01"></Button>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
</RelativeLayout>
</TabHost>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tabexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TabExmapleActivity"
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="ArrowsActivity"></activity>
<activity android:name="EditActivity"></activity>
<activity android:name="OptionsActivity"></activity>
</application>
</manifest>
Just Put Progressbar for the child Activitys you are adding in TabHost .
First it will show progress bar of first activity in onCreate which you set as current activity and if you click on next tab it will show its progress dialog
if i get you correctly, you need to access the below activity components when the progress dialog is active.
for this you need to make non-modal/modeless progress dialog. non-modal dialogs will allow you to accept events by behind ui components
please refer the post below
timed modeless dialog

Categories

Resources