i have developed my first android application with a splash screen, login
page. after providing the login code user will redirected to the list page
of the first TAB of 3 TABs. i m using listviewfragment for the TAB display.
can any one help me to resolve the issue. the issue was after login success page redirected to first tab of the fragment and after few seconds app has been closed with unfortunately app has been closed error.
Need urgent reply.
here is my code.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.example.malverninstitute"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.example.malverninstitute.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name=".MainActivity1"
android:label="@string/app_name" >
</activity>
<activity android:name=".tabslist"
android:label="@string/app_name" />
</application>
</manifest>
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
style="@color/titlebackgroundcolor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/titlebackgroundcolor"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="230dp"
android:text="@string/splash_malvern"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/White"
android:textSize="40sp"
android:typeface="sans" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="@string/splash_subtext"
android:textColor="@color/White"
android:textSize="12sp"
android:typeface="sans" />
</LinearLayout>
activity_main_activiy1.xml Main p[age for LOGIN
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
style="@color/Title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:background="@color/Top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:tag="@string/main_name"
android:textColor="@color/White"
tools:context="com.example.malverninstitute.MainActivity1" >
<EditText
android:id="@+id/logid"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="text"
android:text="@string/loginid" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_above="@+id/logid"
android:layout_alignRight="@+id/logid"
android:layout_marginBottom="14dp"
android:text="@string/logintext2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/Tit"
android:textColorLink="@color/Tit"
android:textSize="28sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:text="@string/logintext1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/Tit"
android:textSize="@dimen/logtext1size"
android:textStyle="bold"
android:typeface="normal" />
<Button
android:id="@+id/button1"
style="@layout/activity_main_activity1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_below="@+id/logid"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:background="@color/Tit"
android:text="@string/logbuttonname"
android:textAppearance="@style/MyActionBar"
android:textColor="@color/White"
android:textColorHint="@color/Title"
android:textColorLink="@color/White"
android:textSize="@dimen/logbuttonsize"
android:textStyle="bold" />
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
MainActivity1.java
package com.example.malverninstitute;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity1 extends Activity {
Button b;
EditText et;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
b = (Button)findViewById(R.id.button1);
et = (EditText)findViewById(R.id.logid);
tv = (TextView)findViewById(R.id.tv);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity1.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}
void login(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://ift.tt/1wl4umm");
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("logid",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
// tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity1.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});
// startActivity(new Intent(MainActivity1.this, tabslist.class));
Intent i = new Intent(MainActivity1.this, tabslist.class);
startActivity(i);
}else{
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
MainActivity1.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity1.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
@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_activity1, 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);
}
}
tabslist.xml TABS page after login success
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
xmlns:android="http://ift.tt/nIICcg"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</android.support.v4.app.FragmentTabHost>
tabslist.java
package com.example.malverninstitute;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
/**
* This demonstrates how you can implement switching between the tabs of a
* TabHost through fragments, using FragmentTabHost.
*/
public class tabslist extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabslist);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("content").setIndicator("CONTENT"),
ContentFragment.class, null);
// mTabHost.getResources().getDrawable(R.drawable.content_blue);
mTabHost.addTab(mTabHost.newTabSpec("links").setIndicator("LINKS"),
LinksFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("chat").setIndicator("CHAT"),
ChatFragment.class, null);
}
}
fragment_content.xml First tab for content list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
ContetnFragment.java
package com.example.malverninstitute;
import com.example.malverninstitute.R;
import android.os.Bundle;
import android.support.v4.app.ListFragment ;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.example.malverninstitute.JSONParser;
@SuppressWarnings("unused")
public class ContentFragment extends ListFragment {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// products JSONArray
JSONArray products = null;
// url to get all products list
private static String url_all_products = "http://ift.tt/1wl4wum";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_MEALS = "products";
private static final String TAG_MID = "TempTitle";
private static final String TAG_NAME = "TempDescp";
private static final String TAG_PRICE = "TempQ";
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_content, container, false);
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
/*// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading meals in Background Thread
new LoadAllMeals().execute();
// Get listview
lv = getListView();
lv.setItemsCanFocus(false);*/
productsList = new ArrayList<HashMap<String, String>>();
lv = (ListView) view.findViewById(R.id.TempTitle);
lv = getListView();
new LoadAllMeals().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllMeals extends AsyncTask<String, String, String> {
/**
* getting All Meals from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON response
Log.d("All Articles: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Meals found
// Getting Array of Meals
products = json.getJSONArray(TAG_MEALS);
// looping through All meals
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String TempTitle = c.getString(TAG_MID);
String TempDescp = c.getString(TAG_NAME);
String TempQ = c.getString(TAG_PRICE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_MID, TempTitle);
map.put(TAG_NAME, TempDescp);
map.put(TAG_PRICE, TempQ);
// adding HashList to ArrayList
productsList.add(map);
}
} /*else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
*/
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all meals
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
String[] from = new String[] {TAG_MID,TAG_NAME,TAG_PRICE};
int[] to = new int[] { R.id.TempTitle, R.id.TempDescp, R.id.TempQ};
ListView listView = (ListView)getActivity().findViewById(android.R.id.list);
ListAdapter adapter = new SimpleAdapter (
getActivity(), productsList,
R.layout.meals_list_item, from,
to);
// updating listview
listView.setAdapter(adapter);
}
});
}
}
}
Thanks.
No comments:
Post a Comment