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);
}
Related
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);
I try to implement an application based on rscm (middleware). When I try to run on emulator, I get the errors listed below:
Error :03-27 16:58:20.490: E/Trace(1508): error opening trace file: No
such file or directory (2)
03-27 21:53:21.610: D/AndroidRuntime(3803): Shutting down VM 03-27
21:53:21.610: W/dalvikvm(3803): threadid=1: thread exiting with
uncaught exception (group=0x40a71930) 03-27 21:53:21.760:
E/AndroidRuntime(3803): FATAL EXCEPTION: main 03-27 21:53:21.760:
E/AndroidRuntime(3803): java.lang.RuntimeException: Unable to
instantiate activity
ComponentInfo{com.example.context_application/com.example.context_application.MyContextAwareActivity}:
java.lang.ClassNotFoundException: Didn't find class
"com.example.context_application.MyContextAwareActivity" on path:
/data/app/com.example.context_application-2.apk 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread.access$600(ActivityThread.java:141) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.os.Handler.dispatchMessage(Handler.java:99) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
android.os.Looper.loop(Looper.java:137) 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
android.app.ActivityThread.main(ActivityThread.java:5041) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
java.lang.reflect.Method.invokeNative(Native Method) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
java.lang.reflect.Method.invoke(Method.java:511) 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
dalvik.system.NativeStart.main(Native Method) 03-27 21:53:21.760:
E/AndroidRuntime(3803): Caused by: java.lang.ClassNotFoundException:
Didn't find class
"com.example.context_application.MyContextAwareActivity" on path:
/data/app/com.example.context_application-2.apk 03-27 21:53:21.760:
E/AndroidRuntime(3803): at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
java.lang.ClassLoader.loadClass(ClassLoader.java:501) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
java.lang.ClassLoader.loadClass(ClassLoader.java:461) 03-27
21:53:21.760: E/AndroidRuntime(3803): at
android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-27 21:53:21.760: E/AndroidRuntime(3803): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-27 21:53:21.760: E/AndroidRuntime(3803): ... 11 more
Can anyone help?
package com.example.context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.aspectsense.rscm.ContextValue;
import org.aspectsense.rscm.context.client.ContextListenerActivity;
import org.json.JSONException;
import java.util.Date;
public class MyContextAwareActivity extends ContextListenerActivity
{
#Override public String[] getRequestedScopes()
{
return new String[] { "battery.level" };
}
private TextView messageTextView;
#Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
messageTextView = new TextView(this);
setContentView(messageTextView);
appendMessage("Activity created");
}
private void appendMessage(final String message)
{
final String currentMessage = messageTextView.getText().toString();
messageTextView.setText(currentMessage + "\n" + message);
}
#Override public void onContextValueChanged(ContextValue contextValue)
{
try
{
appendMessage(new Date() + ": The battery level is " + contextValue.getValueAsInteger() + "%");
}
catch (JSONException jsone)
{
Toast.makeText(this, "Error while displaying context event: " + contextValue, Toast.LENGTH_SHORT).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.context"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.context.MyContextAwareActivity"
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>
You are telling it to write to external storage. Have you set up an SD card in eclipse?
Do any of these answers help? error opening trace file: No such file or directory (2)
I made a class and its constructor should take 2 arguments, but Eclipse gives me an InstantiationException and I don't know why. How can I fix this?
Here is the Sms class code:
public class Sms {
String message;
String phonenumber = "";
SharedPreferences sp;
Context context;
public Sms(String m, Context context)
{
message = m;
this.context = context;
}
public void sendSms()
{
sp = PreferenceManager.getDefaultSharedPreferences(context);
phonenumber = sp.getString("PHONE", "");
SmsManager manager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
int length = message.length();
if(length > MAX_SMS_MESSAGE_LENGTH)
{
ArrayList<String> messagelist = manager.divideMessage(message);
manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
}
else
{
manager.sendTextMessage(phonenumber, null, message, piSend, piDelivered);
}
}
}
--------------------------------------------
And the LogCat:
02-16 17:20:26.579: E/AndroidRuntime(474): FATAL EXCEPTION: main
02-16 17:20:26.579: E/AndroidRuntime(474): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{mioc.programing.safetpin/mioc.programing.safetpin.Sms}: java.lang.InstantiationException: mioc.programing.safetpin.Sms
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.os.Looper.loop(Looper.java:123)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.reflect.Method.invoke(Method.java:507)
02-16 17:20:26.579: E/AndroidRuntime(474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-16 17:20:26.579: E/AndroidRuntime(474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-16 17:20:26.579: E/AndroidRuntime(474): at dalvik.system.NativeStart.main(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474): Caused by: java.lang.InstantiationException: mioc.programing.safetpin.Sms
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.Class.newInstanceImpl(Native Method)
02-16 17:20:26.579: E/AndroidRuntime(474): at java.lang.Class.newInstance(Class.java:1409)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-16 17:20:26.579: E/AndroidRuntime(474): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-16 17:20:26.579: E/AndroidRuntime(474): ... 11 more
And this is how I create the object from my Activity:
Intent open_SM = new Intent ("android.intent.action.SMS");
startActivity(open_SM);
Sms s = new Sms("trlababalan",this);
s.sendSms()
;
here is manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mioc.programing.safetpin"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="mioc.programing.safetpin.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>
<service android:name="TestService">
</service>
<activity
android:name="mioc.programing.safetpin.Sms"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SMS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You have declared Sms as an Activity in your Manifest:
<activity
android:name="mioc.programing.safetpin.Sms"
android:label="#string/app_name" >
But Sms doesn't extend Activity... So these lines cause the exception:
Intent open_SM = new Intent ("android.intent.action.SMS");
startActivity(open_SM);
If you want to run Sms as an Activity you need to turn the entire class into an Android Activity, or if you want Sms to be a utility don't use an Intent to start Sms as an Activity.
I have created new activity that my MainActivity Should lunch, some why the application is crashing on the start of the new activity (called GamePlayActivity).
Here is the java code:
Intent startGameDrill = new Intent(MainActivity.this, GamePlayActivity.class);
startActivity(startGameDrill);
Here is the startGameDrill:
package com.simplemathgame;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class GamePlayActivity extends MainActivity {
int addDrills;
int subDrils;
int mulDrills;
int divDrills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_play);
try {
numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
minBoundText = (TextView) findViewById(R.id.min_text);
maxBoundText = (TextView) findViewById(R.id.max_text);
} catch (Exception e1) {
// TODO Auto-generated catch block
Log.w("game","error");
}
try {
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
subDrils = Integer.parseInt((String) numberOfSubDrills.getText());
mulDrills = Integer.parseInt((String) numberOfMulDrills.getText());
divDrills = Integer.parseInt((String) numberOfDivDrills.getText());
} catch (NumberFormatException e) {
Log.w("GameDrills","string to int");
}
Log.w("add", "" + addDrills);
Log.w("add", "" + subDrils);
Log.w("add", "" + mulDrills);
Log.w("add", "" + divDrills);
}
}
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemathgame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme" >
<activity
android:name="com.simplemathgame.Splash"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.MainActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.simplemathgame.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.GamePlayActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
</application>
</manifest>
here is the logCat:
12-21 22:05:53.949: D/dalvikvm(610): GC_EXTERNAL_ALLOC freed 42K, 53% free 2546K/5379K, external 1917K/2137K, paused 41ms
12-21 22:06:32.809: D/AndroidRuntime(610): Shutting down VM
12-21 22:06:32.809: W/dalvikvm(610): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-21 22:06:32.818: E/AndroidRuntime(610): FATAL EXCEPTION: main
12-21 22:06:32.818: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemathgame/com.simplemathgame.GamePlayActivity}: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:123)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 22:06:32.818: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-21 22:06:32.818: E/AndroidRuntime(610): ... 11 more
Here is the layout of the GamePlayActivity:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow
android:layout_marginTop="10dp"
android:gravity="center_vertical" >
<TextView
android:textColor="#FFFFFFFF"
android:textSize="16sp"
android:textStyle="bold"
android:text="Addition Drills:"/>
</TableRow>
</TableLayout>
Why does it crashes?
Have a look here:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
Line 31 of GamePlayActivity.java is this line:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
Since the only thing on this line that you reference a member or method of is numberOfAddDrills, then it must be null.
Consider that you are checking for exceptions when using findViewById; however, if the view is not found it will just return null, not throw an exception.
Have a look at the activity_game_play.xml you posted; there is no TextView with android:id="#+id/add_drills_number". You need to create it, and the same deal for the other five TextViews.
Oh, and a hint: Don't just catch generic exceptions with } catch (Exception e1) {, especially if you have no intent of reading the logs. I can't tell you how many times people have missed errors from doing this.
If you want to pass values from another layout, you have two options: keep a reference to the View objects (this is a bit silly, don't do this); or pass the data you need from them to your new Activity:
startGameDrill.putExtra("myIntExtra", 5); // For example
Then, in GamePlayActivity.onCreate(), you can fetch it using getIntent()'s extras:
Bundle extras = getIntent().getExtras();
int myIntExtra = extras.getInt("myIntExtra");
Then use that value instead of relying on the view in the previous layout. There is a fuller example in this answer.
Follow the stacktrace until you find some references to your classes:
It shows:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
So you are creating a NullPointerException at line 31 in GamePlayActivity.java
Line 31:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
so maybe numberOfAddDrills is null?
I cannot start a new activity no matter what I do.
I re-downloaded all Android SDKs again.
I re-downloaded Eclipse and installed ADT plugin again
Error Log
09-16 17:53:51.537: W/IInputConnectionWrapper(4661): getCursorCapsMode on inactive InputConnection
09-16 17:53:51.998: D/AndroidRuntime(4661): Shutting down VM
09-16 17:53:51.998: W/dalvikvm(4661): threadid=1: thread exiting with uncaught exception (group=0x41987300)
09-16 17:53:52.022: E/AndroidRuntime(4661): FATAL EXCEPTION: main
09-16 17:53:52.022: E/AndroidRuntime(4661): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lays.decisong/com.lays.decisong.activities.GameActivity}: java.lang.NullPointerException
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.os.Looper.loop(Looper.java:137)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.reflect.Method.invoke(Method.java:511)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-16 17:53:52.022: E/AndroidRuntime(4661): at dalvik.system.NativeStart.main(Native Method)
09-16 17:53:52.022: E/AndroidRuntime(4661): Caused by: java.lang.NullPointerException
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.AlertDialog.<init>(AlertDialog.java:98)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ProgressDialog.show(ProgressDialog.java:110)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ProgressDialog.show(ProgressDialog.java:99)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity$3.<init>(GameActivity.java:401)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity.<init>(GameActivity.java:398)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.Class.newInstanceImpl(Native Method)
09-16 17:53:52.022: E/AndroidRuntime(4661): at java.lang.Class.newInstance(Class.java:1319)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-16 17:53:52.022: E/AndroidRuntime(4661): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
Public key in DecisongApplication.java
public static final String PLAYERS_KEY = "com.lays.decisong.activities.Players";
Button Handler in InputActivity.java
App forces closes when startActivity() is called
public void startGame(View v) {
// check if there's more than one player
if (mPlayers.size() < 2) {
Log.i(TAG, "Only 1 player");
Toast.makeText(mContext, "At least 2 players needed to start game",
Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(this.getApplicationContext(), GameActivity.class);
intent.putStringArrayListExtra(DecisongApplication.PLAYERS_KEY, mPlayers);
startActivity(intent);
overridePendingTransition(R.anim.slide_up_incoming, R.anim.slide_up_outgoing);
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lays.decisong"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<supports-screens
android:anyDensity="true"
android:largeScreens="false"
android:normalScreens="true"
android:smallScreens="false"
android:xlargeScreens="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="com.lays.decisong.DecisongApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
<activity android:name=".activities.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.InstructionsActivity"
android:noHistory="true" />
<activity
android:name=".activities.InputActivity"
android:noHistory="true" />
<activity
android:name=".activities.SettingsActivity"
android:noHistory="true" />
<activity
android:name=".activities.GameActivity"
android:noHistory="true" />
</application>
</manifest>
onCreate of GameActivity.java in com.lays.decisong.activities which never got called
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
// init rdio variables
mTrackQueue = new LinkedList<Track>();
if (mRdio == null) {
mRdio = new Rdio(DecisongApplication.RDIO_API_KEY,
DecisongApplication.RDIO_SECRET_KEY, null, null, this, this);
}
// init quiz variables
mListView = getListView();
mAllAlbums = new HashMap<String, Album>();
mChosenTracks = new ArrayList<Track>();
mAdapter = new TracksAdapter(this, mChosenTracks);
setListAdapter(mAdapter);
// init player variables
mCurrentRoundView = (TextView) findViewById(R.id.current_round);
mCurrentRound = INITIAL_ROUND;
mCurrentPlayerView = (TextView) findViewById(R.id.current_player);
mCurrentPlayer = INITIAL_PLAYER;
mPlayers = new ArrayList<Player>();
if (getIntent().hasExtra(DecisongApplication.PLAYERS_KEY)) {
ArrayList<String> players = getIntent().getStringArrayListExtra(DecisongApplication.PLAYERS_KEY);
for (String p : players) {
mPlayers.add(Player.create(p));
}
}
// setup game conditions
Collections.shuffle(mPlayers);
mCurrentPlayerView.setText(mPlayers.get(mCurrentPlayer).name);
}
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity$3.<init>(GameActivity.java:401)
09-16 17:53:52.022: E/AndroidRuntime(4661): at com.lays.decisong.activities.GameActivity.<init>(GameActivity.java:398)
Looks like you're trying to do some setup in your GameActivity's constructor, or initialise some fields before the activity's Context is fully initialised. You should really do all setup in onCreate(); don't use a constructor, and don't rely on the Context being usable until onCreate() is entered.