Reputation: 744
I have this activity code:
public class TopJokes extends Activity {
public class Globals extends Application{
String[] myStringArray = {"a","b","c","a","b","c","a","b","c","a","b","c","a","b","c","a","b","c"};
public String[] getMyStringArray() {
return myStringArray;
}
public void setMyStringArray(String[] myStringArray) {
this.myStringArray = myStringArray;
}
}
String[] myStringArray = {"a","b","c","a","b","c","a","b","c","a","b","c","a","b","c","a","b","c"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_jokes);
Globals globals = (Globals)getApplication();
new loadJson().execute();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, globals.getMyStringArray());
ListView listView = (ListView) findViewById(R.id.topJokesList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.top_jokes, menu);
return true;
}
public class loadJson extends AsyncTask<Void, Integer, String>{
@Override
protected String doInBackground(Void... params) {
URL u;
StringBuffer buffer = new StringBuffer();
try {
u = new URL("https://site.com/android/britishJokes/showJokes.php");
URLConnection conn = u.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
buffer.append(inputLine);
in.close();
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
protected void onPostExecute(String buffer) {
JSONArray jsonArray = new JSONArray();
try {
jsonArray = new JSONArray(buffer);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("JSONarray: " + jsonArray);
String[] newJokes = null;
for (int i = 0; i < jsonArray.length(); i++) {
try {
newJokes[i] = jsonArray.getJSONObject(i).toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
myStringArray = newJokes;
}
}
}
I have created the class Globals
- supposed to hold the global variables.
Here is how I have declared it in the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gs.britishjokes"
android:versionCode="1"
android:versionName="1.0"
android:name=".Globals">
The problem is that when I try to access the global variable myStringArray
In this lines:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, globals.getMyStringArray());
I'm getting tons of errors and I can't understand what is wrong.
I'm pretty sure that I'm missing something really small, but as a total beginner I'm not able to spot it.
Here is the log:
02-09 12:00:22.627: E/AndroidRuntime(3249): FATAL EXCEPTION: main
02-09 12:00:22.627: E/AndroidRuntime(3249): Process: com.gs.britishjokes, PID: 3249
02-09 12:00:22.627: E/AndroidRuntime(3249): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gs.britishjokes/com.gs.britishjokes.TopJokes}: java.lang.ClassCastException: android.app.Application cannot be cast to com.gs.britishjokes.TopJokes$Globals
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.os.Handler.dispatchMessage(Handler.java:102)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.os.Looper.loop(Looper.java:137)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-09 12:00:22.627: E/AndroidRuntime(3249): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 12:00:22.627: E/AndroidRuntime(3249): at java.lang.reflect.Method.invoke(Method.java:515)
02-09 12:00:22.627: E/AndroidRuntime(3249): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-09 12:00:22.627: E/AndroidRuntime(3249): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-09 12:00:22.627: E/AndroidRuntime(3249): at dalvik.system.NativeStart.main(Native Method)
02-09 12:00:22.627: E/AndroidRuntime(3249): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.gs.britishjokes.TopJokes$Globals
02-09 12:00:22.627: E/AndroidRuntime(3249): at com.gs.britishjokes.TopJokes.onCreate(TopJokes.java:76)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.Activity.performCreate(Activity.java:5243)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-09 12:00:22.627: E/AndroidRuntime(3249): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-09 12:00:22.627: E/AndroidRuntime(3249): ... 11 more
Please, give me a clue. I'm lost!
Upvotes: 1
Views: 198
Reputation: 11131
You should make your Globals
class as static class inside Activity
or write it in a seperate java file (recommended) and declare it in manifest under application
tag with fully qualified class name..
<application
android:name="com.packagename.app.TopJokes$Globals"
... />
Upvotes: 1
Reputation: 3759
In your AndroidManifest.xml, set the name of your application
to your Application class' name:
I'd also recommend putting Globals
into its own class, rather than in inner class of some Activity.
...
<application
android:name="Globals"/>
...
Upvotes: 1