Android Application crahes when passing strings between classes - java

I'm trying to pass a string between 2 classes in Android . The first class is "MainActivity" and the second one is "abc" .
Here's my MainActivity code :
package com.example.passstrings;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
String pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass = "abcabc";
Intent abc = new Intent(this , abc.class);
abc.putExtra("key", pass);
startActivity(abc);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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;
}
#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.fragment_main, container, false);
return rootView;
}
}
}
and here's my abc class code :
package com.example.passstrings;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}
}
Here's my fragment_main.xml code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.passstrings.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginLeft="69dp"
android:layout_marginTop="72dp"
android:layout_toRightOf="#+id/textView2"
android:text="TextView" />
</RelativeLayout>
and here's my LogCat :
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x008b
04-04 05:34:38.545: I/dalvikvm(734): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:34:38.545: W/dalvikvm(734): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:34:38.545: D/dalvikvm(734): VFY: replacing opcode 0x6e at 0x0099
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.555: D/dalvikvm(734): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:34:38.675: D/AndroidRuntime(734): Shutting down VM
04-04 05:34:38.675: W/dalvikvm(734): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:34:38.685: E/AndroidRuntime(734): FATAL EXCEPTION: main
04-04 05:34:38.685: E/AndroidRuntime(734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.os.Looper.loop(Looper.java:123)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:34:38.685: E/AndroidRuntime(734): at dalvik.system.NativeStart.main(Native Method)
04-04 05:34:38.685: E/AndroidRuntime(734): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.passstrings/com.example.passstrings.abc}; have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivityForResult(Activity.java:2817)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Activity.startActivity(Activity.java:2923)
04-04 05:34:38.685: E/AndroidRuntime(734): at com.example.passstrings.MainActivity.onCreate(MainActivity.java:25)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:34:38.685: E/AndroidRuntime(734): ... 11 more
and here's my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.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>
If I remove "startActivity(abc);" the application doesn't crash, also if use try&catch the application doesn't crash but the same time it doesn't perform the action . So what could be the reason ? and thanks in advance
My LogCat after adding the activity to manifest :
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.555: W/dalvikvm(883): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.555: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x008b
04-04 05:47:16.555: I/dalvikvm(883): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-04 05:47:16.565: W/dalvikvm(883): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-04 05:47:16.565: D/dalvikvm(883): VFY: replacing opcode 0x6e at 0x0099
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.565: D/dalvikvm(883): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-04 05:47:16.775: D/AndroidRuntime(883): Shutting down VM
04-04 05:47:16.775: W/dalvikvm(883): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-04 05:47:16.786: E/AndroidRuntime(883): FATAL EXCEPTION: main
04-04 05:47:16.786: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passstrings/com.example.passstrings.abc}: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:123)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:521)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 05:47:16.786: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 05:47:16.786: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
04-04 05:47:16.786: E/AndroidRuntime(883): Caused by: java.lang.NullPointerException
04-04 05:47:16.786: E/AndroidRuntime(883): at com.example.passstrings.abc.onCreate(abc.java:20)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 05:47:16.786: E/AndroidRuntime(883): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 05:47:16.786: E/AndroidRuntime(883): ... 11 more
and my Manifest after the update :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passstrings"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.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.passstrings.abc" />
</application>
</manifest>

You need to add your second Activity to your Androidmanifest.xml file.
<activity android:name="com.example.passstrings.abc" />

have you declared this activity in your AndroidManifest.xml?
04-04 05:34:38.685: E/AndroidRuntime(734): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
it clearly say's that you have not declared your abc activity in manifest file. have you declared this activity in your AndroidManifest.xml?
so just decalre your abc activity in manifest file.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.passstrings.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=".abc"
</activity>
</application>
add this line
<activity
android:name=".abc"
</activity>
and also change this in your abc.java
setContentView(R.layout.activity_main);
you have reference of activity_main only you have to set abc.XML or whatever your XML filename for abc. So change it with
setContentView(R.layout.abc);
Edit
public class abc extends Activity{
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tv1=(TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String pass= intent.getExtras().getString("key");
if(pass!= null){
tv1.setText(pass);
}
}

It is simply ActivityNotFoundException which mean you forgot to declare abc activity in your AndroidManifest.xml file.
Just add following line in your manifest.xml in application tag.
<activity android:name="com.example.passstrings.abc" />
For your new NullPointerException here is the solution
You have defined following .xml in your Activity file activity_main while you have declare TextViews in fragment_main.xml, that's why it is giving NullPointerException.
I suggest you to change the xml from following line
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main.xml);

Related

Class not found Android

I have a custom View class in my application. I want to extend it and add it to my xml file however when I do that I get class not found exception..
Here is the xml file:
floorplan.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/floorplannextbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="47dp"
android:layout_marginTop="22dp"
android:text="Next" />
<com.example.helloworld3.FloorPlan.DrawView2
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
And this is my DrawView2 class. It is inside another class called FloorPlan but the entire code would be too long to post
DrawView2.java
public class DrawView2 extends RelativeLayout {
Paint paint = new Paint();
public DrawView2(Context context) {
super(context);
paint.setColor(Color.RED);
}
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
for(int i =0,j=i+1 ; i<subpath.size()&& j<subpath.size(); i++, j++){
ux = subpath.get(i).v.x;
dx =subpath.get(i).v.y;
rx= subpath.get(j).v.x;
lx = subpath.get(j).v.y;
canvas.drawLine(ux, dx , rx, lx, paint);
}
}
This is what I get in the LogCat:
04-04 20:37:59.959: D/(884): HostConnection::get() New Host Connection established 0x8b92258, tid 884
04-04 20:38:07.047: W/KeyCharacterMap(884): No keyboard for id 0
04-04 20:38:07.047: W/KeyCharacterMap(884): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-04 20:38:16.599: D/AndroidRuntime(884): Shutting down VM
04-04 20:38:16.599: W/dalvikvm(884): threadid=1: thread exiting with uncaught exception (group=0xb5ff94f0)
04-04 20:38:16.607: E/AndroidRuntime(884): FATAL EXCEPTION: main
04-04 20:38:16.607: E/AndroidRuntime(884): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld3/com.example.helloworld3.FloorPlan}: android.view.InflateException: Binary XML file line #18: Error inflating class com.example.helloworld3.FloorPlan.DrawView2
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.os.Looper.loop(Looper.java:130)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 20:38:16.607: E/AndroidRuntime(884): at dalvik.system.NativeStart.main(Native Method)
04-04 20:38:16.607: E/AndroidRuntime(884): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.example.helloworld3.FloorPlan.DrawView2
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.Activity.setContentView(Activity.java:1657)
04-04 20:38:16.607: E/AndroidRuntime(884): at com.example.helloworld3.FloorPlan.onCreate(FloorPlan.java:164)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-04 20:38:16.607: E/AndroidRuntime(884): ... 11 more
04-04 20:38:16.607: E/AndroidRuntime(884): Caused by: java.lang.ClassNotFoundException: com.example.helloworld3.FloorPlan.DrawView2 in loader dalvik.system.PathClassLoader[/data/app/com.example.helloworld3-1.apk]
04-04 20:38:16.607: E/AndroidRuntime(884): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-04 20:38:16.607: E/AndroidRuntime(884): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-04 20:38:16.607: E/AndroidRuntime(884): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
04-04 20:38:16.607: E/AndroidRuntime(884): ... 20 more
And is my Manifest file:
AndroidManifes. xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld3"
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/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=".FloorPlan"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld3.FLOORPLAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".FloorPlanTwo"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld3.FLOORPLANTWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".FloorPlanThree"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld3.FLOORPLANTHREE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you for all you help!
If DrawView2 is an inner class it should be referred to in XML as:
...
<com.example.helloworld3.FloorPlan$DrawView2
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...
I had a problem similar to yours. Try these two things:
Extends from View, not from RelativeLayout
Add these constructors to your DrawView2 class
public DrawView2(Context context)
{
super(context);
}
public DrawView2(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public DrawView2(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}

Why DrawerLayout and ActionBar not working on Tablets?

I'm develop some app with sliding menu.
when i try to run this on my galaxy 4s(api 18+ 4.3.3) its work fine!
but when i run this on xperia arc (api 8 ver 2.3.4) the app crash.
i know its dependent on Support library and i use :
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
and i need to use :
import android.support.v7.app.ActionBar;
so i added to my project the support library and connect the support library to him.
but i dont know how to handle the support and what i need to chage on my code:
my java code (v4){
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
public class MainActivity extends Activity implements OnClickListener, OnLongClickListener , OnTouchListener , OnItemClickListener{
/////////////////
ArrayList<ItemInSetting > listSetting = new ArrayList<ItemInSetting>();
CustomAdapterSetting adapterSetting;
DrawerLayout drawer;
ActionBarDrawerToggle toggle;
//////////////////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingItems();
}
public void settingItems(){
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawer = (DrawerLayout) findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this, drawer, R.drawable.ic_drawer, 0, 0){
#Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("menu");
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
getActionBar().setTitle("app title");
invalidateOptionsMenu();
}
};drawer.setDrawerListener(toggle);
listSetting.add(new ItemInSetting("ist1", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist2", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist3", R.drawable.calende_bigg));
listSetting.add(new ItemInSetting("ist4", R.drawable.calende_bigg));
lvSetting = (ListView) findViewById(R.id.listSetting);
lvSetting.setOnItemClickListener(this);
adapterSetting = new CustomAdapterSetting(listSetting, this);
lvSetting.setAdapter(adapterSetting);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
date = new Date();
switch (position) {
case 0:
Intent intent = new Intent (MainActivity.this,ListOfHours.class);
startActivity(intent);
break;
case 1:
getSetting();
if(sickIsOn){
cancelingFreeday(false);
}else{
addingManualMenu();
}
break;
case 2:
changingSetting(" ");
break;
case 3:
sendContact();
break;
default:
break;
}
drawer.closeDrawers();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (toggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
toggle.onConfigurationChanged(newConfig);
}
my xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="#drawable/new_back"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listSetting"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#android:color/darker_gray"
android:choiceMode="singleChoice"
android:listSelector="#drawable/list_selector" >
</ListView>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bib.workclock"
android:versionCode="52"
android:versionName="3.0.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/icon_new3"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity
android:name="com.bibas.workclock.MainActivity"
android:label="#string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ListOfHours" ></activity>
<activity android:name="HelpPage"></activity>
<activity android:name="info" android:screenOrientation="portrait"></activity>
</application>
</manifest>
this the locat error when i try to run on xperia :
05-04 01:59:05.058: E/AndroidRuntime(15484): FATAL EXCEPTION: main
05-04 01:59:05.058: E/AndroidRuntime(15484): java.lang.NoSuchMethodError: com.bba.workclock.MainActivity.getActionBar
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.bba.workclock.MainActivity.settingItems(MainActivity.java:1657)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.bba.workclock.MainActivity.onCreate(MainActivity.java:124)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.os.Looper.loop(Looper.java:130)
05-04 01:59:05.058: E/AndroidRuntime(15484): at android.app.ActivityThread.main(ActivityThread.java:3701)
05-04 01:59:05.058: E/AndroidRuntime(15484): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 01:59:05.058: E/AndroidRuntime(15484): at java.lang.reflect.Method.invoke(Method.java:507)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
05-04 01:59:05.058: E/AndroidRuntime(15484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
05-04 01:59:05.058: E/AndroidRuntime(15484): at dalvik.system.NativeStart.main(Native Method)
here is project like mine:
http://speedy.sh/UKsrV/SlidingMenu.zip
if any one can fix that code and give it to me back i appreciate alot
How can i fix this?!!
please help me :(((
I FIX THAT PROBLEM BUT KNOW ON MY TABLET IT'S CRASH .
here the locat error:
05-11 23:10:22.127: E/AndroidRuntime(31723): FATAL EXCEPTION: main
05-11 23:10:22.127: E/AndroidRuntime(31723): java.lang.IllegalStateException: Child android.widget.FrameLayout#41f91e40 at index 0 does not have a valid layout_gravity - must be Gravity.LEFT, Gravity.RIGHT or Gravity.NO_GRAVITY
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:721)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.View.measure(View.java:15479)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer.doFrame(Choreographer.java:525)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Handler.handleCallback(Handler.java:615)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.os.Looper.loop(Looper.java:137)
05-11 23:10:22.127: E/AndroidRuntime(31723): at android.app.ActivityThread.main(ActivityThread.java:4895)
05-11 23:10:22.127: E/AndroidRuntime(31723): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 23:10:22.127: E/AndroidRuntime(31723): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-11 23:10:22.127: E/AndroidRuntime(31723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-11 23:10:22.127: E/AndroidRuntime(31723): at dalvik.system.NativeStart.main(Native Method)
ActionBar is available from api level 11. To support below api level 11 you need to use AppCompat from the support library. Reference AppCompat in your android project.
You need to extend ActionBarActivity. Use theme derived from Theme.AppCompat in manifest and use getSupportActionBar().
Complete example #
Navigation Drawer with backword compatibility android
Check Adding libraries with resources
https://developer.android.com/tools/support-library/setup.html
And read
http://developer.android.com/guide/topics/ui/actionbar.html

unfortunately android appname has stopped [duplicate]

This question already has answers here:
After Google Play Service update to version 13 I got an error
(8 answers)
Closed 9 years ago.
Yesterday I updated my eclipse. Before I update the eclipse, my application ran correctly but after I updated the eclipse when I run the application,I have an error,
MY LOGCAT
11-02 04:50:32.548: D/AndroidRuntime(2161): Shutting down VM 11-02 04:50:32.548: W/dalvikvm(2161): threadid=1: thread exiting with
uncaught exception (group=0x40a71930) 11-02 04:50:32.609:
E/AndroidRuntime(2161): FATAL EXCEPTION: main 11-02 04:50:32.609:
E/AndroidRuntime(2161): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.vindypratama.tempatwisata/com.vindypratama.tempatwisata.MainMapActivity}:
android.view.InflateException: Binary XML file line #7: Error
inflating class fragment 11-02 04:50:32.609: E/AndroidRuntime(2161):
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.app.ActivityThread.access$600(ActivityThread.java:141) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.os.Handler.dispatchMessage(Handler.java:99) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
android.os.Looper.loop(Looper.java:137) 11-02 04:50:32.609:
E/AndroidRuntime(2161): at
android.app.ActivityThread.main(ActivityThread.java:5041) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
java.lang.reflect.Method.invokeNative(Native Method) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
java.lang.reflect.Method.invoke(Method.java:511) 11-02 04:50:32.609:
E/AndroidRuntime(2161): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
dalvik.system.NativeStart.main(Native Method) 11-02 04:50:32.609:
E/AndroidRuntime(2161): Caused by: android.view.InflateException:
Binary XML file line #7: Error inflating class fragment 11-02
04:50:32.609: E/AndroidRuntime(2161): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
android.view.LayoutInflater.inflate(LayoutInflater.java:489) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.app.Activity.setContentView(Activity.java:1881) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
com.vindypratama.tempatwisata.MainMapActivity.onCreate(MainMapActivity.java:25)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.app.Activity.performCreate(Activity.java:5104) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-02 04:50:32.609: E/AndroidRuntime(2161): ... 11 more 11-02
04:50:32.609: E/AndroidRuntime(2161): Caused by:
java.lang.IllegalStateException: The meta-data tag in your app's
AndroidManifest.xml does not have the right value. Expected 4030500
but found 0. You must have the following declaration within the
element: 11-02
04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown
Source) 11-02 04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.maps.internal.q.v(Unknown Source) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.maps.internal.q.u(Unknown Source) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.maps.SupportMapFragment$b.cE(Unknown Source)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.dynamic.a.a(Unknown Source) 11-02 04:50:32.609:
E/AndroidRuntime(2161): at
com.google.android.gms.dynamic.a.onInflate(Unknown Source) 11-02
04:50:32.609: E/AndroidRuntime(2161): at
com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown
Source) 11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
11-02 04:50:32.609: E/AndroidRuntime(2161): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-02 04:50:32.609: E/AndroidRuntime(2161): ... 21 more 11-02
04:50:45.858: I/Process(2161): Sending signal. PID: 2161 SIG: 9
My activity.java
package org.lucasr.twowayview;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
public class MainMapActivity extends FragmentActivity
{
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_map);
setupMapIfNeeded();
}
private void setupMapIfNeeded()
{
if(map == null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.maps);
map = supportMapFragment.getMap();
if(map != null)
{
setupMap();
}
}
}
private void setupMap()
{
map.setMyLocationEnabled(true);
moveToMyLocation();
}
private void moveToMyLocation()
{
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if ( location != null)
{
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));
}
}
#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_map, menu);
return true;
}
protected void onResume()
{
super.onResume();
int resCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if( resCode != ConnectionResult.SUCCESS)
{
GooglePlayServicesUtil.getErrorDialog(resCode, this, 1);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vindypratama.tempatwisata"
android:versionCode="1"
android:versionName="1.0" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.vindypratama.tempatwisata.permission.MAPS_RECEIVE" />
<permission
android:name="com.vindypratama.tempatwisata.permission.MAPS_RECEIVE"
android:protectionLevel="signature" >
</permission><uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDcosD0fQsQcYCEiDDkRxP6ZRACPf-svaw" />
<activity
android:name="com.vindypratama.tempatwisata.MainMapActivity"
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>
</manifest>
xml file:
<fragment
android:id="#+id/maps"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
Try to use Project -> Clean. That actually helped me.
EDIT: If that doesn't help, try to relaunch Eclipse after Clean.
you are using a custom class - twowayview. fix you build path. check if you have checked the added jars in your build path. clean the project. delete bin folder.clear/refresh the project. it will work

The application has stopped unexpectedly . Error-caused by: Java.lang.NullPointerException

main.java
package com.learnactivities;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
activity.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=".Main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
</RelativeLayout>
Second.java
package com.learnactivities;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Second" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="#string/nd" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnactivities"
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.learnactivities.Main"
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=".second" />
</application>
</manifest>
I m getting following errors. I can see that error caused by java.lang.NullPointerException. I did my best searching here and there but could not get to the solution.Plz, anyone help me sort this problem out?
08-25 19:08:44.442: D/AndroidRuntime(276): Shutting down VM
08-25 19:08:44.442: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-25 19:08:44.472: E/AndroidRuntime(276): FATAL EXCEPTION: main
08-25 19:08:44.472: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learnactivities/com.learnactivities.Main}: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-25 19:08:44.472: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at com.learnactivities.Main.onCreate(Main.java:19)
08-25 19:08:44.472: E/AndroidRuntime(276): at `android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)`
`08-25 19:08:44.472: E/AndroidRuntime(276): at` `android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)`
08-25 19:08:44.472: E/AndroidRuntime(276): ... 11 more
R.id.button1 is declared inside second.xml, but you are looking for it inside activity.xml. So in Main, when you do
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
findViewById is returning null
Try this in your main class..
TextView b = (TextView ) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
and also in manifest change like below line
<activity android:name=".Second" />

Android Java: Global variables through subclass; can't launch app?

I've been reading up examples on how to do this, trying to piece together a functioning example from posts on this site and others. I'm sure I'm missing something small, but as a novice to Java, I could use some assistance.
I'm merely trying to create a small example, derived from the automated hello world generated when creating a new project in Eclipse. All I want to do is be able to store a few global variables in a subclass, then reference those values in my main activity. Unfortunately, every time I try to run the app, it crashes "Unfortunately, GeneralTest1 has stopped", and the log cat error is not very helpful.
Quick overview:
GlobalVars class extends Application
In the manifest, android:name has been added to reference the additional GlobalVars class
Within my main activity, I'm initializing the global vars class with getApplicationContext()
Here is everything I've got; any help would be much appreciated!
MainActivity.java
package com.example.generaltest1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
GlobalVars.java
package com.example.generaltest1;
import android.app.Application;
public class GlobalVars extends Application {
String myString = "Some Text";
public String getMyString() {
return myString;
}
public String setMyString(String string) {
this.myString = string;
return myString;
}
}
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" >
<TextView
android:id="#+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">GeneralTest1</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
GeneralTest1 Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.generaltest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:name="GlobalVars">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Log Cat
08-14 09:53:05.546: E/Trace(620): error opening trace file: No such file or directory (2)
08-14 09:53:05.656: D/AndroidRuntime(620): Shutting down VM
08-14 09:53:05.656: W/dalvikvm(620): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-14 09:53:05.676: E/AndroidRuntime(620): FATAL EXCEPTION: main
08-14 09:53:05.676: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.generaltest1/com.example.generaltest1.MainActivity}: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.os.Looper.loop(Looper.java:137)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 09:53:05.676: E/AndroidRuntime(620): at dalvik.system.NativeStart.main(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): Caused by: java.lang.NullPointerException
08-14 09:53:05.676: E/AndroidRuntime(620): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-14 09:53:05.676: E/AndroidRuntime(620): at com.example.generaltest1.MainActivity.<init>(MainActivity.java:10)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstanceImpl(Native Method)
08-14 09:53:05.676: E/AndroidRuntime(620): at java.lang.Class.newInstance(Class.java:1319)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-14 09:53:05.676: E/AndroidRuntime(620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-14 09:53:05.676: E/AndroidRuntime(620): ... 11 more
You are calling getApplicationContext() in your class definition, which is too early in the activity lifetime and results in a nullpointer exception. Move the assignments to your onCreate() function and it should work as expected.
Please check this article about activities and their lifecycle :
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Your activity should look like this:
public class MainActivity extends Activity {
GlobalVars myVars;
TextView myText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVars = ((GlobalVars)getApplication());
myText = (TextView) findViewById(R.id.myText);
myText.setText(myVars.getMyString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Add these lines in oncreate()..
GlobalVars myVars = ((GlobalVars)getApplicationContext());
TextView myText = (TextView) findViewById(R.id.myText);

Categories