Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old October 20th, 2011, 05:56 PM   #1 (permalink)
New Member
 
Join Date: Dec 2009
Posts: 4
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calling an activity from dynamically created buttons

I am creating a grid of buttons, each button will open an activity (showstory) and send it a number so that the right info can be pulled from an array to display a string in showstory.

I keep getting a force close when I call the New Activity

Here is my Button Adapter, I tried things a couple of different ways. one way is commented out
Code:
package com.soce.qronicle;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

    public class ButtonAdapter extends BaseAdapter {  
     final Context mContext;  
     private int butts;
//     final Intent i;
      
     // Gets the context so it can be used later  
     public ButtonAdapter(Context c, int b) {  
      mContext = c;  
      butts = b;
//      i = new Intent(mContext, showstory.class);
//      i.putExtra("storyid",butts);
     }  
      
     // Total number of things contained within the adapter  
     public int getCount() {  
        return butts; 
     }  
      
      // Require for structure, not really used in my code.  
     public Object getItem(int position) {  
      return null;  
     }  
      
     // Require for structure, not really used in my code. Can  
     // be used to get the id of an item in the adapter for   
     // manual control.   
     public long getItemId(int position) {  
      return position;  
     }  
      
     public View getView(final int position,View convertView, ViewGroup parent) {  
      Button btn;  
      if (convertView == null) {    
       // if it's not recycled, initialize some attributes  
       btn = new Button(mContext);  
       btn.setLayoutParams(new GridView.LayoutParams(100, 100));  
       btn.setPadding(8, 8, 8, 8);  
       }   
      else {  
       btn = (Button) convertView;  
      }  
      btn.setText(Integer.toString(position+1));        
      btn.setTextColor(Color.GRAY);  
      btn.setBackgroundResource(R.drawable.button100);  
      btn.setId(position);  
      Log.d("_CREATED", Integer.toString(position));
      btn.setOnClickListener(new Button.OnClickListener() {  
          public void onClick(View v)
          {
              
              Log.d("_X_X_X", Integer.toString(position));
              Intent i = new Intent(mContext, showstory.class);
              i.putExtra("storyid",butts);
                  mContext.startActivity(i);
             
              //perform action
          }
       });

      return btn;   
     }

    
    
}
Manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.soce.qronicle"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.CAMERA" />
     <uses-feature android:name="android.hardware.camera" />
     <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".QRonicleActivity"
                  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=".addscreen" android:label="Add Screen" >
      </activity>
      <activity android:name=".writestory" android:label="Write Story Screen" >
      </activity>
      <activity android:name=".viewscreen" android:label="View Story Screen" >
      </activity>
      <activity android:name=".showstory" android:label="View the Story" >
      </activity>
      <activity android:name=".ButtonAdapter" android:label="Button Adapter" >
      </activity>

    </application>
</manifest>
LogCat
Code:
10-20 18:55:24.676: INFO/ActivityManager(102): Starting activity: Intent { cmp=com.soce.qronicle/.showstory (has extras) }
10-20 18:55:24.756: WARN/Bundle(1992): Key storyid expected String but value was a java.lang.Integer.  The default value <null> was returned.
10-20 18:55:24.806: DEBUG/dalvikvm(1992): GC_FOR_MALLOC freed 3041 objects / 189560 bytes in 30ms
10-20 18:55:24.806: WARN/Bundle(1992): Attempt to cast generated internal exception:
10-20 18:55:24.806: WARN/Bundle(1992): java.lang.ClassCastException: java.lang.Integer
10-20 18:55:24.806: WARN/Bundle(1992):     at android.os.Bundle.getString(Bundle.java:1040)
10-20 18:55:24.806: WARN/Bundle(1992):     at com.soce.qronicle.showstory.onCreate(showstory.java:17)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.os.Looper.loop(Looper.java:144)
10-20 18:55:24.806: WARN/Bundle(1992):     at android.app.ActivityThread.main(ActivityThread.java:4937)
10-20 18:55:24.806: WARN/Bundle(1992):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 18:55:24.806: WARN/Bundle(1992):     at java.lang.reflect.Method.invoke(Method.java:521)
10-20 18:55:24.806: WARN/Bundle(1992):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-20 18:55:24.806: WARN/Bundle(1992):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-20 18:55:24.806: WARN/Bundle(1992):     at dalvik.system.NativeStart.main(Native Method)
Debug
Code:
Thread [<1> main] (Suspended (exception RuntimeException))    
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2787    
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2803    
    ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 135    
    ActivityThread$H.handleMessage(Message) line: 2136    
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99    
    Looper.loop() line: 144    
    ActivityThread.main(String[]) line: 4937    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]    
    Method.invoke(Object, Object...) line: 521    
    ZygoteInit$MethodAndArgsCaller.run() line: 868    
    ZygoteInit.main(String[]) line: 626    
    NativeStart.main(String[]) line: not available [native method]
Cheers!

anfractuosities is offline  
Reply With Quote
Sponsors
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:09 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo