Im new to android programming and i have searched my eyes out for a solution to my problem but i cant find it anywhere. Im building an application with 3 screen layouts but i have a problem with the last screen:
The problem:
The application starts and the 2 first screens works good but when i go to the last screen(activity_katag.xml & katagActivity.java) and press "Deal1" the application stops and gives me the error "Unfortunately, "AppName" has stopped."
LogCat Error File:
13:32:12.270: W/dalvikvm(614): threadid=1: thread exiting with uncaught exception (group=0x40a122a0)
03-12 13:32:12.290: E/AndroidRuntime(614): FATAL EXCEPTION: main
03-12 13:32:12.290: E/AndroidRuntime(614): java.lang.NullPointerException
03-12 13:32:12.290: E/AndroidRuntime(614): at com.appname.fundeals.DealsAdapter.getChildrenCount(DealsAdapter.java:42)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:562)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.widget.AbsListView$1.run(AbsListView.java:3533)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.os.Handler.handleCallback(Handler.java:615)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.os.Looper.loop(Looper.java:137)
03-12 13:32:12.290: E/AndroidRuntime(614): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-12 13:32:12.290: E/AndroidRuntime(614): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 13:32:12.290: E/AndroidRuntime(614): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 13:32:12.290: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-12 13:32:12.290: E/AndroidRuntime(614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 13:32:12.290: E/AndroidRuntime(614): at dalvik.system.NativeStart.main(Native Method)
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appname.fundeals"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
<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=".inloggActivity" />
<activity android:name=".katagActivity" />
</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="#drawable/bakgrund"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.appname.fundeals.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Random Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="75dp"
android:layout_marginLeft="10dp"
android:text="Username:"
android:textAppearance="?android:attr/textAppearanceMedium" />
/>
<EditText
android:id="#+id/usernameET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_marginLeft="35dp"
android:layout_toRightOf="#+id/textView2"
android:hint="Användarnamn" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="40dp"
android:text="Password:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/passwordET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/usernameET"
android:layout_alignRight="#+id/usernameET"
android:hint="lösenord"
android:inputType="textPassword" />
<TextView
android:id="#+id/attemptsLeftTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:layout_marginLeft="30dp"
android:layout_marginTop="48dp"
android:text="Attempts Left:"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/numberOfRemainingLoginAttemptsTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_alignTop="#+id/attemptsLeftTV"
android:visibility="invisible" />
<TextView
android:id="#+id/loginLockedTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/loginBtn"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<Button
android:id="#+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberOfRemainingLoginAttemptsTV"
android:layout_centerHorizontal="true"
android:onClick="authenticateLogin"
android:text="Login" />
<Button
android:id="#+id/FacebookBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/loginBtn"
android:layout_toLeftOf="#+id/usernameET"
android:onClick="authenticateLogin"
android:text="Facebook" />
MainActivity.java:
package com.appname.fundeals;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import com.appname.fundeals.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
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;
#SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {
public class onClick implements OnClickListener {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, inloggActivity.class));
}
}
private EditText username;
private EditText password;
private Button login;
private TextView loginLockedTV;
private Button facebookBtn;
private TextView attemptsLeftTV;
private TextView numberOfRemainingLoginAttemptsTV;
int numberOfRemainingLoginAttempts = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebookBtn = (Button) findViewById(R.id.FacebookBtn);
facebookBtn.setOnClickListener(new onClick());
setupVariables();
}
public void authenticateLogin(View view) {
if (username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Hello admin!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Seems like you 're not admin!",
Toast.LENGTH_SHORT).show();
numberOfRemainingLoginAttempts--;
attemptsLeftTV.setVisibility(View.VISIBLE);
numberOfRemainingLoginAttemptsTV.setVisibility(View.VISIBLE);
numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));
if (numberOfRemainingLoginAttempts == 0) {
login.setEnabled(false);
loginLockedTV.setVisibility(View.VISIBLE);
loginLockedTV.setBackgroundColor(Color.RED);
loginLockedTV.setText("LOGIN LOCKED!!!");
}
}
}
private void setupVariables() {
username = (EditText) findViewById(R.id.usernameET);
password = (EditText) findViewById(R.id.passwordET);
login = (Button) findViewById(R.id.loginBtn);
loginLockedTV = (TextView) findViewById(R.id.loginLockedTV);
attemptsLeftTV = (TextView) findViewById(R.id.attemptsLeftTV);
numberOfRemainingLoginAttemptsTV = (TextView) findViewById(R.id.numberOfRemainingLoginAttemptsTV);
numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));
}
}
activity_inlogg.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bakgrund" >
<TextView
android:id="#+id/textViewFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="20dp"
android:text="Random Text."
android:textStyle="bold" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewFirst"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:background="#00000000"
android:src="#drawable/rkeepklar" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="6dp"
android:background="#00000000"
android:src="#drawable/stklarmarkerad" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="4dp"
android:background="#00000000"
android:src="#drawable/ndklar" />
</RelativeLayout>
inloggActivity.java (screen2):
package com.appname.fundeals;
import com.appname.fundeals.inloggActivity.OnClick;
import com.appname.fundeals.R;
import android.app.Activity;
import android.graphics.Color;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class inloggActivity extends Activity {
public class OnClick implements OnClickListener {
#Override
public void onClick(View v) {
startActivity(new Intent(inloggActivity.this, katagActivity.class));
}
}
private Button imageButton1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inlogg);
Button imageButton1 = (Button) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(new OnClick());
}
}
activity_katag.xml (screen3):
<?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="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/bakgrund"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
>
<ExpandableListView
android:id="#+id/exp_list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#A4C739"
android:dividerHeight="0.5dp"
></ExpandableListView>
</RelativeLayout>
katagActivity.java:
package com.appname.fundeals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class katagActivity extends Activity {
private static final HashMap<String, List<String>> Deals_Catagory = null;
HashMap<String, List<String>> Deals_category;
List<String> Deals_list;
ExpandableListView Exp_list;
DealsAdapter adapter;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_katag);
Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
Deals_category = DataProvider.getInfo();
Deals_list = new ArrayList<String>(Deals_category.keySet());
adapter = new DealsAdapter(this, Deals_Catagory, Deals_list);
Exp_list.setAdapter(adapter);
}
}
DealsAdapter.java:
package com.appname.fundeals;
import java.util.HashMap;
import java.util.List;
import com.appname.fundeals.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class DealsAdapter extends BaseExpandableListAdapter {
private Context ctx;
private HashMap<String, List<String>> Deals_Catagory;
private List<String> Deals_List;
public DealsAdapter(Context ctx, HashMap<String, List<String>> Deals_Catagory, List<String> Deals_List) {
this.ctx = ctx;
this.Deals_Catagory = Deals_Catagory;
this.Deals_List = Deals_List;
// TODO Auto-generated constructor stub
}
#Override
public int getGroupCount() {
return Deals_List.size();
}
#Override
public int getChildrenCount(int arg0) {
return Deals_Catagory.get(Deals_List.get(arg0)).size();
}
#Override
public Object getGroup(int arg0) {
return Deals_List.get(arg0);
}
#Override
public Object getChild(int parent, int child) {
// TODO Auto-generated method stub
return Deals_Catagory.get(Deals_List.get(parent)).get(child);
}
#Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getChildId(int parent, int child) {
// TODO Auto-generated method stub
return child;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentView) {
// TODO Auto-generated method stub
String group_title = (String) getGroup(parent);
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.parent_deal1, parentView, false);
}
TextView parent_textView = (TextView) convertView.findViewById(R.id.parent_deal1);
parent_textView.setTypeface(null, Typeface.BOLD);
parent_textView.setText(group_title);
return convertView;
}
#Override
public View getChildView(int parent, int child, boolean lastChild, View convertView,
ViewGroup parentview) {
String child_title = (String) getChild(parent, child);
if (convertView == null)
{
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.child_deal1, parentview, false);
}
// TODO Auto-generated method stub
TextView child_textView = (TextView) convertView.findViewById(R.id.child_deal1);
child_textView.setText(child_title);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
DataProvider.java:
package com.appname.fundeals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DataProvider {
public static HashMap<String, List<String>> getInfo()
{
HashMap<String, List<String>> DealsDetails = new HashMap<String, List<String>>();
List<String> Deal_1 = new ArrayList<String>();
Deal_1.add("Text");
List<String> Deal_2 = new ArrayList<String>();
Deal_2.add("Text");
List<String> Deal_3 = new ArrayList<String>();
Deal_3.add("Text");
List<String> Deal_4 = new ArrayList<String>();
Deal_4.add("Text");
DealsDetails.put("Deal 1", Deal_1);
DealsDetails.put("Deal 2", Deal_2);
DealsDetails.put("Deal 3", Deal_3);
DealsDetails.put("Deal 4", Deal_4);
return DealsDetails;
}
}
You try to convert ImageButton to Button so Error come.
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewFirst"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:background="#00000000"
android:src="#drawable/rkeepklar" />
Button imageButton1 = (Button) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(new OnClick());
Do same in other Button also
Updated code
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
imageButton1.setOnClickListener(new OnClick());
Caused by: java.lang.ClassCastException: android.widget.ImageButton
cannot be cast to android.widget.Button
A ClassCastException is an Exception that can occur in a Java program when you try to improperly convert a class from one type to another.
Don't
Button imageButton1 = (Button) findViewById(R.id.imageButton1);
Do
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
Because your XML
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewFirst"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:background="#00000000"
android:src="#drawable/rkeepklar" />
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
$ error in androidmonitor
02-02 14:22:48.870 3269-3269/com.example.applincatio.t E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.applincatio.t, PID: 3269
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applincatio.t/com.example.applincatio.t.moddle}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebChromeClient(android.webkit.WebChromeClient)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebChromeClient(android.webkit.WebChromeClient)' on a null object reference
at com.example.applincatio.t.moddle.onCreate(moddle.java:20)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
$main activity.java
package com.example.applincatio.t;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import static com.example.applincatio.t.R.*;
import static com.example.applincatio.t.R.id.moddle1;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView moddle;
ImageButton outlook;
ImageButton contactus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
moddle = (ImageView) findViewById(moddle1);
ImageView moddle = (ImageView) findViewById(moddle1);
moddle.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case moddle1:
Intent imprintIntent = new Intent(MainActivity.this, moddle.class);
imprintIntent.putExtra("webivew", moddle1);
this.startActivity(imprintIntent);
break;
case id.outlook:
Intent contactIntent = new Intent(MainActivity.this, moddle.class);
// contactIntent.putExtra("webivewContact", outlook);
this.startActivity(contactIntent);
break;
case id.contactus:
Intent aboutIntent = new Intent(MainActivity.this, moddle.class);
//aboutIntent.putExtra("webivewAbout", contactus);
this.startActivity(aboutIntent);
break;
}
}
}
$moddle.java
package com.example.applincatio.t;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class moddle extends AppCompatActivity {
String url;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
Intent intent = this.getIntent();
if (intent != null) {
Bundle data = getIntent().getExtras();
if (data.containsKey("webivew")) { //i have changed this param to match the intent passed
url = data.getString("webivew");
}
if (data.containsKey("webivewContact")) {
url = data.getString("webivewContact");
}
if (data.containsKey("webivewAbout")) {
url = data.getString("webivewAbout");
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView wView, int progress) {
// webViewActivity.setTitle("Loading...");
// webViewActivity.requestWindowFeature(progress * 100);
if(progress == 100) {
// webViewActivity.setTitle(R.string.app_name);
}
}
});
webView.loadUrl(url);
}
}
}
$activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:background="#drawable/p"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.applincatio.t.MainActivity"
android:weightSum="1">
<GridLayout
android:layout_width="match_parent"
android:rowCount="4"
android:columnCount="2"
android:layout_height="530dp"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:id="#+id/gridview"
android:padding="14dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/moddle1"
android:src="#drawable/moddle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MODDLE"
android:textColor="#fff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="96dp"
android:layout_height="139dp"
android:id="#+id/outlook"
android:layout_gravity="center"
android:src="#drawable/ou"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OUTLOOK"
android:gravity="bottom"
android:textAlignment="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="122dp"
android:layout_height="141dp"
android:layout_gravity="center"
android:id="#+id/contactus"
android:src="#drawable/cont"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONTACT US"
android:textSize="20dp"
android:textColor="#ffff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="123dp"
android:layout_height="128dp"
android:onClick="onClick"
android:layout_gravity="center"
android:src="#drawable/vi"
android:id="#+id/visitus" />
<TextView
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="VISIT US"
android:gravity="bottom"
android:textAlignment="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
</LinearLayout>
</GridLayout>
</LinearLayout>
when i run the app. it is running but when i click on any imagebutton the app keeps stopping. i cant understand the mistake i did .in image you can see my layout its running but imagebuttons are not working
please any one help me
thanks in advance
Try the below one,
package com.example.applincatio.t;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class moddle extends AppCompatActivity {
String url;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web_link);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setJavaScriptEnabled(true);
Intent intent = this.getIntent();
if (intent != null) {
Bundle data = getIntent().getExtras();
if (data.containsKey("webview")) {
url = data.getString("webview");
}
if (data.containsKey("webivewContact")) {
url = data.getString("webivewContact");
}
if (data.containsKey("webivewAbout")) {
url = data.getString("webivewAbout");
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView wView, int progress) {
// webViewActivity.setTitle("Loading...");
// webViewActivity.requestWindowFeature(progress * 100);
if(progress == 100) {
// webViewActivity.setTitle(R.string.app_name);
}
}
});
url = "https://moodle.kluniversity.in/login/index.php";
webView.loadUrl(url);
}
}
}
Thanks!
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
In a class i am taking we have been learning some android app development and such, and i have managed to run into a problem that I, for the life of me, cannot figure out...
Basic background...
I get a nullpointerexception when I rotate the device... This error is located at Line 100 of QuizActivity.....
this Line is : mCheatButton.setOnClickListener(new View.OnClickListener() {//....
I suspect this has something to do with storing or retrieving information from the bundle...
Anyways here is the rest of The code....
Here is my quiz activity... Sorry for all the code blocks, I dont really know a better way to give you this information, or if you even need all of it.... But right now I just can figure out what is going on... so i have printed the error (stack trace? i think that is the right term...).. here as well...
03-02 09:49:28.858 7921-7921/com.example.ryan.ryans_quiz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ryan.ryans_quiz, PID: 7921
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ryan.ryans_quiz/com.example.ryan.ryans_quiz.QuizActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.ryan.ryans_quiz.QuizActivity.onCreate(QuizActivity.java:100)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
QuizActivity
package com.example.ryan.ryans_quiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private static final int REQUEST_CODE_CHEAT = 0;
private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private Button mCheatButton;
private TextView mQuestionTextView;
private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true)
};
private int mCurrentIndex = 0;
private boolean mIsCheater;
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId = 0;
if (mIsCheater) {
messageResId = R.string.judgment_toast;
} else {
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
.show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
}
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mTrueButton = (Button) findViewById(R.id.trueButton);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.falseButton);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton = (ImageButton) findViewById(R.id.nextButton);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
}
});
mCheatButton = (Button)findViewById(R.id.cheatButton);
mCheatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
Intent i = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
startActivityForResult(i, REQUEST_CODE_CHEAT);
}
});
updateQuestion();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_INDEX, mCurrentIndex);
}
#Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause() called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_CODE_CHEAT) {
if (data == null) {
return;
}
mIsCheater = CheatActivity.wasAnswerShown(data);
}
}
}
My CheatActivity class
package com.example.ryan.ryans_quiz;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class
CheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE =
"com.bignerdranch.android.geoquiz.answer_is_true";
private static final String EXTRA_ANSWER_SHOWN =
"com.bignerdranch.android.geoquiz.answer_shown";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
public static Intent newIntent(Context packageContext, boolean answerIsTrue) {
Intent i = new Intent(packageContext, CheatActivity.class);
i.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
return i;
}
public static boolean wasAnswerShown(Intent result) {
return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
}
private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mAnswerTextView = (TextView)findViewById(R.id.answer_text_view);
mShowAnswer = (Button)findViewById(R.id.show_answer_button);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.trueButton);
} else {
mAnswerTextView.setText(R.string.falseButton);
}
setAnswerShownResult(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int cx = mShowAnswer.getWidth() / 2;
int cy = mShowAnswer.getHeight() / 2;
float radius = mShowAnswer.getWidth();
Animator anim = ViewAnimationUtils
.createCircularReveal(mShowAnswer, cx, cy, radius, 0);
anim.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mAnswerTextView.setVisibility(View.VISIBLE);
mShowAnswer.setVisibility(View.INVISIBLE);
}
});
anim.start();
} else {
mAnswerTextView.setVisibility(View.VISIBLE);
mShowAnswer.setVisibility(View.INVISIBLE);
}
}
});
}
my Question Class
package com.example.ryan.ryans_quiz;
/**
* Created by Ryan on 1/20/2016.
*/
public class Question {
private int mTextResId;
private boolean mAnswerTrue;
public Question(int _textResId, boolean _answerTrue) {
mTextResId = _textResId;
mAnswerTrue = _answerTrue;
}
public int getTextResId() {
return mTextResId;
}
public void setTextResId(int textResId) {
mTextResId = textResId;
}
public boolean isAnswerTrue() {
return mAnswerTrue;
}
public void setAnswerTrue(boolean answerTrue) {
mAnswerTrue = answerTrue;
}
}
And finally my xml Files....
activity_cheat
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
tools:context="com.example.ryan.ryans_quiz.CheatActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="24dp"
android:text="#string/warning_text"/>
<TextView
android:id="#+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="24dp"
tools:text="Answer"/>
<Button
android:id="#+id/show_answer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/show_answer_button"/>
</LinearLayout>
LandLayout - activity_quiz
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="24dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="horizontal">
<Button
android:id="#+id/trueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/trueButton"/>
<Button
android:id="#+id/falseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/falseButton"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="#string/cheat_button"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="horizontal">
<ImageButton
android:id="#+id/previousButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/previousButton"
android:src="#drawable/arrow_left"/>
<ImageButton
android:id="#+id/nextButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:contentDescription="#string/nextButton"
android:src="#drawable/arrow_right"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
vertical layout- activity_quiz
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/trueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/trueButton"/>
<Button
android:id="#+id/falseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/falseButton"/>
</LinearLayout>
<Button
android:id="#+id/cheatButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cheat_button"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/previousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/previousButton"
android:src="#drawable/arrow_left"/>
<ImageButton
android:id="#+id/nextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/nextButton"
android:src="#drawable/arrow_right"/>
</LinearLayout>
</LinearLayout>
Strings..
<resources>
<string name="app_name">RyanHull_Quiz</string>
<string name="action_settings">Settings</string>
<string name="trueButton">True</string>
<string name="falseButton">False</string>
<string name="nextButton">Next</string>
<string name="previousButton">Previous</string>
<string name="cheat_button">Cheat!</string>
<string name="correct_toast">Correct! :)</string>
<string name="incorrect_toast">Incorrect! :(</string>"
<string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longest river in the Americas.</string>
<string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
<string name="warning_text">Are you sure you want to do this??</string>
<string name="show_answer_button">Show Answer</string>
<string name="judgment_toast">Cheating is Wrong 8/</string>
<string name="title_activity_cheat">Cheat</string>
and finally manifest....
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.ryan.ryans_quiz"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".QuizActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name=".CheatActivity"
android:label="#string/title_activity_cheat"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
EDIT: its not a matter of me know what the exception is.. More of the matter of find where and why...
The lookup is failing for mCheatButton because you forgot to assign it an id in the landscape version of the activity_quiz.xml layout file.
When you then try to set a ClickListener on a null object that is where the NullPointerException is coming from.
when you rotate the device,the QuizActivity will destroy and then call onCreate,but your LandLayout file has no Button called 'cheatButton',so findViewById(R.id.cheatButton) will return null to mCheatButton, it can not call any method.
You haven't assigned any id to cheat button in LandLayout - activity_quiz
Change
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="#string/cheat_button"/>
to
<Button
android:id="#+id/cheatButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="#string/cheat_button"/>
As I press the "Start Activity" button in my app it won't working and forced to stop its working.
Here is 1st xml file (get.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/etget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Enter Your Gender" />
<Button
android:id="#+id/bget1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/etget"
android:text="Start Activity" />
<Button
android:id="#+id/bget2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/etget"
android:layout_toLeftOf="#id/bget1"
android:text="Start Activity for Result " />
<TextView
android:id="#+id/tvget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bget2"
android:text="TextView" />
</RelativeLayout>
Here is java file, Get.java:
package example.katta;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Get extends Activity implements OnClickListener {
Button bg1, bg2;
TextView tv;
TextView etg;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
etg = (TextView) findViewById(R.id.etget);
bg1 = (Button) findViewById(R.id.bget1);
bg2 = (Button) findViewById(R.id.bget2);
tv = (TextView) findViewById(R.id.tvget);
bg1.setOnClickListener(this);
bg2.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bget1:
String bread = etg.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(Get.this, Send.class);
a.putExtras(basket);
startActivity(a);
break;
case R.id.bget2:
break;
}
}
}
Here is 2nd xml file(send.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/etsend"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
<RadioGroup
android:id="#+id/rgsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb1send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="#+id/rb2send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<TextView
android:id="#+id/tvsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/buttonsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
Here is java file(Send.java) :
package example.katta;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class Send extends Activity implements OnClickListener, OnCheckedChangeListener {
Button bts;
TextView tvs,ets;
RadioGroup selection;
String gotbread;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
Initialize();
Bundle gotbasket = getIntent().getExtras();
gotbread = gotbasket.getString("key");
tvs.setText(gotbread);
}
private void Initialize() {
// TODO Auto-generated method stub
ets = (TextView) findViewById(R.id.etsend);
bts = (Button) findViewById(R.id.buttonsend);
tvs = (TextView) findViewById(R.id.tvsend);
selection = (RadioGroup) findViewById(R.id.rgsend);
bts.setOnClickListener(this);
selection.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rb1send:
break;
case R.id.rb2send:
break;
}
}
}
The error is:
android.content.ActivityNotFoundException: Unable to find explicit activity class {example.katta/example.katta.Send}; have you declared this activity in your AndroidManifest.xml?
The solution is to add Send Activity in your AndroidManifest.xml:
<activity
android:name=".Send" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should declarate every activities in your manifest, also I think you can create a better code.
I'll give some recommendations to help you.
1- You can explicit in naming their views.
<TextView
android:id="#+id/txt_view_send"
... >
</TextView>
2- Use camelNotation java code
Bundle gotBasket ...
Future large projects is of great help!
3- Use match_parent un your views
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent.
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
4- Bundle is used only when you have many arguments. You can do something like this in your example.
//you can add a variable to get editText value or pass direct the value in to putExtras()
String bread = etg.getText().toString();
Intent a = new Intent(Get.this, Send.class);
a.putExtras("key", bread);
startActivity(a);
http://developer.android.com/reference/android/content/Intent.html
4-validating can help avoid exceptions.
//example
if(getIntent().getExtras() != null){
tvs.setText(getIntent().getExtras().getString("key"));
}
//your example
Bundle gotbasket = getIntent().getExtras();
if(gotbasket != null){
gotbread = gotbasket.getString("key");
if(gotbread != null{
tvs.setText(gotbread);
}
It can produce NullPointerException.
I hope it helps you!!
Cheers
programming continues!
My problem is when I click bSA button I encounter error and the activity closes.
java.lang.runtimeexception unable to start activity componentinfo ...
here is my code
Data.java :
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Data extends Activity implements View.OnClickListener {
Button start, startFor;
EditText sendET;
TextView gotAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
initialize();
}
private void initialize() {
start = (Button) findViewById(R.id.bSA);
startFor = (Button) findViewById(R.id.bSAFR);
sendET = (EditText) findViewById(R.id.etSend);
gotAnswer = (TextView) findViewById(R.id.tvGot);
start.setOnClickListener(this);
startFor.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bSA:
String bread = sendET.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
startActivity(a);
break;
case R.id.bSAFR:
break;
}
}
}
get.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etSend" android:layout_gravity="center_horizontal"/>
<Button
android:layout_below="#+id/etSend"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity"
android:id="#+id/bSA"/>
<Button
android:layout_toLeftOf="#+id/bSA"
android:layout_alignTop="#+id/bSA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity for Results"
android:id="#+id/bSAFR"/>
<TextView
android:layout_below="#+id/bSAFR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:id="#+id/tvGot"/>
</RelativeLayout>
=============================================================
OpenedClass.java
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
public class OpenedClass extends Activity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
TextView question, test;
Button returnData;
RadioGroup selectionList;
String gotBread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
initialize();
Bundle gotBasket = getIntent().getExtras();
gotBread = gotBasket.getString("key");
question.setText(gotBread);
}
private void initialize() {
question = (TextView) findViewById(R.id.tvQuestion);
test = (TextView) findViewById(R.id.tvText);
returnData = (Button) findViewById(R.id.bReturn);
returnData.setOnClickListener(this);
selectionList = (RadioGroup) findViewById(R.id.rgAnswers);
selectionList.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View view) {
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rCrazy:
break;
case R.id.rFun:
break;
case R.id.rBoth:
break;
}
}
}
send.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Hosein is ..."
android:id="#+id/tvQuestion"/>
<RadioGroup
android:id="#+id/rgAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crazy"
android:id="#+id/rCrazy"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Super Fun"
android:id="#+id/rFun"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Both"
android:id="#+id/rBoth"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return"
android:id="#+id/bReturn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tvText"/>
</LinearLayout>
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
You cannot put a inside itself. This causes infinite recursion and stack overflow.
You probably wanted
a.putExtras(basket);
I have some XML which should be displaying the hour from TimePicker data but every time I try to view the data it force closes. I've narrowed the issue down to an incorrect setup in my XML file(s) but I can't seem to correct the issue and keep the app from force closing.
view_country xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:layout_margin="5dp">
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="#string/name_lbl"/>
<TextView
android:id="#+id/nameText"
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="#string/cap_lbl"/>
<TextView
android:id="#+id/capText"
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="#string/code_lbl"/>
<TextView
android:id="#+id/codeText"
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="Linked Users"/>
<TextView
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="Time Limit"/>
<TextView
android:id="#+id/timeEdit"
style="#style/StyleText"/>
</TableRow>
</TableLayout>
ViewCountry java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.TimePicker;
public class ViewCountry extends Activity {
private long rowID;
private TextView nameTv;
private TextView capTv;
private TextView codeTv;
private TextView timeTv;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_country);
setUpViews();
Bundle extras = getIntent().getExtras();
rowID = extras.getLong(CountryList.ROW_ID);
}
private void setUpViews() {
nameTv = (TextView) findViewById(R.id.nameText);
capTv = (TextView) findViewById(R.id.capText);
timeTv = (TextView) findViewById(R.id.timeEdit);
codeTv = (TextView) findViewById(R.id.codeText);
}
#Override
protected void onResume()
{
super.onResume();
new LoadContacts().execute(rowID);
}
private class LoadContacts extends AsyncTask<Long, Object, Cursor>
{
DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);
#Override
protected Cursor doInBackground(Long... params)
{
dbConnector.open();
return dbConnector.getOneContact(params[0]);
}
#Override
protected void onPostExecute(Cursor result)
{
super.onPostExecute(result);
result.moveToFirst();
// get the column index for each data item
int nameIndex = result.getColumnIndex("name");
int capIndex = result.getColumnIndex("cap");
int codeIndex = result.getColumnIndex("code");
int timeIndex = result.getColumnIndex("time");
nameTv.setText(result.getString(nameIndex));
capTv.setText(result.getString(capIndex));
timeTv.setText(result.getInt(timeIndex));
codeTv.setText(result.getString(codeIndex));
result.close();
dbConnector.close();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.view_country_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.editItem:
Intent addEditContact =
new Intent(this, AddEditCountry.class);
addEditContact.putExtra(CountryList.ROW_ID, rowID);
addEditContact.putExtra("name", nameTv.getText());
addEditContact.putExtra("cap", capTv.getText());
addEditContact.putExtra("code", codeTv.getText());
startActivity(addEditContact);
return true;
case R.id.deleteItem:
deleteContact();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void deleteContact()
{
AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);
alert.setTitle(R.string.confirmTitle);
alert.setMessage(R.string.confirmMessage);
alert.setPositiveButton(R.string.delete_btn,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int button)
{
final DatabaseConnector dbConnector =
new DatabaseConnector(ViewCountry.this);
AsyncTask<Long, Object, Object> deleteTask =
new AsyncTask<Long, Object, Object>()
{
#Override
protected Object doInBackground(Long... params)
{
dbConnector.deleteContact(params[0]);
return null;
}
#Override
protected void onPostExecute(Object result)
{
finish();
}
};
deleteTask.execute(new Long[] { rowID });
}
}
);
alert.setNegativeButton(R.string.cancel_btn, null).show();
}
}
Database Connector
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseConnector {
private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;
public DatabaseConnector(Context context) {
dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}
public void open() throws SQLException
{
//open database in reading/writing mode
database = dbOpenHelper.getWritableDatabase();
}
public void close()
{
if (database != null)
database.close();
}
public void insertContact(String name, String cap, String code, String time)
{
ContentValues newCon = new ContentValues();
newCon.put("name", name);
newCon.put("cap", cap);
newCon.put("time", time);
newCon.put("code", code);
open();
database.insert("country", null, newCon);
close();
}
public void updateContact(long id, String name, String cap,String code, String time)
{
ContentValues editCon = new ContentValues();
editCon.put("name", name);
editCon.put("cap", cap);
editCon.put("time", time);
editCon.put("code", code);
open();
database.update("country", editCon, "_id=" + id, null);
close();
}
public Cursor getAllContacts()
{
return database.query("country", new String[] {"_id", "name"},
null, null, null, null, "name");
}
public Cursor getOneContact(long id)
{
return database.query("country", null, "_id=" + id, null, null, null, null);
}
public void deleteContact(long id)
{
open();
database.delete("country", "_id=" + id, null);
close();
}
}
AddEditCountry java
import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;
public class AddEditCountry extends Activity {
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
timeEt.setCurrentHour(extras.getInt("time"));
}
Button saveButton =(Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (nameEt.getText().length() != 0)
{
AsyncTask<Object, Object, Object> saveContactTask =
new AsyncTask<Object, Object, Object>()
{
#Override
protected Object doInBackground(Object... params)
{
saveContact();
return null;
}
#Override
protected void onPostExecute(Object result)
{
finish();
}
};
saveContactTask.execute((Object[]) null);
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
private void saveContact()
{
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
codeEt.getText().toString());
}
else
{
dbConnector.updateContact(rowID,
nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
codeEt.getText().toString());
}
}
}
add_country xml (where TimePicker data which is causing the crash is first entered into the system)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<EditText android:id="#+id/nameEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:hint="#string/name_hint"
android:inputType="textPersonName|textCapWords"/>
<EditText android:id="#+id/capEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:hint="#string/cap_hint"
android:inputType="textPersonName|textCapWords"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:textColor="#ffffff"
android:text="10MB" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:textColor="#ffffff"
android:text="Unlimited Data" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bandwidth Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:textColor="#ffffff"
android:text="10kbs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#ffffff"
android:gravity="right"
android:text="Unlimited Bandwidth" />
</LinearLayout>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiFi Time Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<TimePicker
android:id="#+id/timeEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1.0" />
<EditText
android:id="#+id/codeEdit"
android:inputType="textUri"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:lines="1"
android:hint="#string/code_hint"
android:imeOptions="actionNext" />
<Button android:id="#+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:text="#string/save_btn"/>
</LinearLayout>
</ScrollView>
(UPDATED) LOGCAT:
03-22 06:14:34.855: D/Activity(4860): Activity.onPause(), editTextTapSensorList size: 0
03-22 06:14:34.915: I/Adreno200-EGLSUB(4860): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 06:14:34.915: D/memalloc(4860): ion: Mapped buffer base:0x5d4c3000 size:614400 offset:0 fd:60
03-22 06:14:34.965: D/memalloc(4860): ion: Mapped buffer base:0x5d8f7000 size:614400 offset:0 fd:64
03-22 06:14:34.985: D/memalloc(4860): ion: Unmapping buffer base:0x5ca41000 size:614400
03-22 06:14:34.985: D/memalloc(4860): ion: Unmapping buffer base:0x5dc05000 size:614400
03-22 06:14:34.985: D/memalloc(4860): ion: Unmapping buffer base:0x5dc9b000 size:614400
03-22 06:14:35.035: D/memalloc(4860): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:56
03-22 06:14:37.748: D/Activity(4860): Activity.onPause(), editTextTapSensorList size: 0
03-22 06:14:37.828: I/Adreno200-EGLSUB(4860): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 06:14:37.828: D/memalloc(4860): ion: Mapped buffer base:0x5e131000 size:614400 offset:0 fd:59
03-22 06:14:37.828: W/ResourceType(4860): No package identifier when getting value for resource number 0x00000000
03-22 06:14:37.828: W/dalvikvm(4860): threadid=1: thread exiting with uncaught exception (group=0x410889d8)
03-22 06:14:37.838: E/AndroidRuntime(4860): FATAL EXCEPTION: main
03-22 06:14:37.838: E/AndroidRuntime(4860): android.content.res.Resources$NotFoundException: String resource ID #0x0
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.content.res.Resources.getText(Resources.java:247)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.widget.TextView.setText(TextView.java:3622)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.nfc.linkingmanager.ViewCountry$LoadContacts.onPostExecute(ViewCountry.java:74)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.nfc.linkingmanager.ViewCountry$LoadContacts.onPostExecute(ViewCountry.java:1)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.AsyncTask.finish(AsyncTask.java:602)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.AsyncTask.access$600(AsyncTask.java:156)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.Looper.loop(Looper.java:137)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.app.ActivityThread.main(ActivityThread.java:4477)
03-22 06:14:37.838: E/AndroidRuntime(4860): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 06:14:37.838: E/AndroidRuntime(4860): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-22 06:14:37.838: E/AndroidRuntime(4860): at dalvik.system.NativeStart.main(Native Method)
UI element "#+id/timeEdit" defined in layout view_country.xml is a TextView. You are tring to cast it to android.widget.TimePicker in your activity ViewCountry.
There is another UI element with same id ("#+id/timeEdit") which indeed IS TimePicker but it's placed in layout for AddEditCountry activity.
Check your ViewCountry layout and code, probably element #+id/timeEdit shouldn't be a TextView, or you should cast it to TextView in your onCreate method.