My problem is the following:
I want to create a really simple App, that shall play a song when i press a button.
public class MainActivity extends ActionBarActivity {
int counter;
Button add1;
Button sub1;
Button sound;
TextView display;
MediaPlayer mySound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
if (mySound==null){
mySound = MediaPlayer.create(this, R.raw.tetris_theme);
}
else { mySound.release();
}
sound = (Button) findViewById(R.id.button1);
display = (TextView) findViewById(R.id.textView2);
sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO Auto-generated method stub
if (mySound==null){
display.setText("Error");
}
else {
mySound.start();
}
}
});
}
#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;
}
}
}
When I run the app and press the button "sound" it displays "Error"(since I checked whether mySound is null or not)
So, I figured that meant that the mySound MediaPLayer I created is null.
I checked the logcat and it showed the following:
11-25 14:27:02.900: E/MediaPlayer(1026): error (1, -2147483648)
11-25 14:27:02.920: D/MediaPlayer(1026): create failed:
11-25 14:27:02.920: D/MediaPlayer(1026): java.io.IOException: Prepare failed.: status=0x1
11-25 14:27:02.920: D/MediaPlayer(1026): at android.media.MediaPlayer.prepare(Native Method)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.media.MediaPlayer.create(MediaPlayer.java:850)
11-25 14:27:02.920: D/MediaPlayer(1026): at com.example.simpleapp.MainActivity.onCreate(MainActivity.java:49)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.app.Activity.performCreate(Activity.java:5242)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.app.ActivityThread.access$800(ActivityThread.java:138)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.os.Looper.loop(Looper.java:136)
11-25 14:27:02.920: D/MediaPlayer(1026): at android.app.ActivityThread.main(ActivityThread.java:5026)
11-25 14:27:02.920: D/MediaPlayer(1026): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:27:02.920: D/MediaPlayer(1026): at java.lang.reflect.Method.invoke(Method.java:515)
11-25 14:27:02.920: D/MediaPlayer(1026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-25 14:27:02.920: D/MediaPlayer(1026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-25 14:27:02.920: D/MediaPlayer(1026): at dalvik.system.NativeStart.main(Native Method)
I guess that means that create failed, because prepare failed, but why?
I already searched for that problem on this website and found a hundred different cases but none
of the solutions did work for me.
the file I want to play is in mp3 and is located in my R.raw folder.
I can play it on my PC without any problems, so I guess it can't be corrupt or something.
I already tried with different types like .ogg and .aac but nothing(!!) works.
This will occur Your MediaPlayer object is not having any resource associated with it and so returned null.
Your file is having special characters. remove special characters and it must be in lower case.
mySound= MediaPlayer.create(this, R.raw.tetristheme);
your media file shoouldf be in the name tetristheme
Now you can use mySound.start() to start playing your media
Related
MainActivity
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, new LayOutOne()).commit();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.settings:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new PrefFragment())
.addToBackStack(null)
.commit();
break;
}
return super.onOptionsItemSelected(item);
}
PrefFragmentList extends PreferenceFragment
public class PrefFragmentList extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
PrefFragment extends Fragment
public class PrefFragment extends Fragment {
private View v;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = inflater.inflate(R.layout.layout_settings, null);
setHasOptionsMenu(true);
return v;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
menu.findItem(R.id.settings).setVisible(true).setEnabled(false).setChecked(true).setChecked(true);
super.onCreateOptionsMenu(menu, inflater);
}
PROBLEM:
MainActivity call PrefFragment from OptionMenu , when i go back to MainActivity and re-call PrefFragment, the application crash.
LOGCAT:
11-27 12:12:50.857 1387-1387/xx.xxx.myapplication D/dalvikvm: GC_FOR_ALLOC freed 228K, 3% free 9211K/9476K, paused 25ms, total 25ms
11-27 12:13:02.584 1387-1387/xx.xxxx.myapplication D/AndroidRuntime: Shutting down VM
11-27 12:13:02.584 1387-1387/xx.xxxx.myapplication W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4178b700)
11-27 12:13:02.592 1387-1387/xx.xxxx.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at xx.xxx.myapplication.PrefActivity.onCreateView(PrefActivity.java:23)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0e0052, tag null, or parent id 0xffffffff with another fragment for xx.xxxxx.myapplication.PrefFragment
at android.app.Activity.onCreateView(Activity.java:4751)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at xxx.xxxx.myapplication.PrefFragment.onCreateView(PrefFragment.java:23)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I try replace in OnCreateView:
final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
with:
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.layout_settings, container, false);
} catch (InflateException e) {
}
return rootView;
Work fine and resolve this error:
LOGCAT
From the logcat: Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0e0052, tag null, or parent id 0xffffffff with another fragment for xx.xxxxx.myapplication.PrefFragment
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtUserName = (EditText)findViewById(R.id.editText1);
final EditText txtPassword = (EditText)findViewById(R.id.editText2);
Button btnLogin = (Button)findViewById(R.id.button1);
btnLogin.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String username = txtUserName.getText().toString();
String password = txtPassword.getText().toString();
String foo = new String("131031001");
String foo2 = new String("131031002");
String foo3 = new String("131031003");
String foo4 = new String("131031004");
String foo5 = new String("131031005");
try{
if(username.length() > 0 && password.length() >0) {
DBUserAdapter dbUser = new DBUserAdapter(MainActivity.this);
dbUser.open();
dbUser.AddUser();
if(dbUser.Login(username, password))
{
Toast.makeText(MainActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
if(username.equals(foo))
{
Intent nextActivity2 = new Intent(getApplicationContext(),SecondActivity.class);
nextActivity2.putExtra("Second",username);
startActivity(nextActivity2);
}
else if(username.equals(foo2)) {
Intent nextActivity3 = new Intent(getBaseContext(),ThirdActivity.class);
nextActivity3.putExtra("Third",username);
startActivity(nextActivity3);
}
else if(username.equals(foo3))
{
Intent nextActivity4 = new Intent(getBaseContext(),FourthActivity.class);
nextActivity4.putExtra("Fourth",username);
startActivity(nextActivity4);
}
else if(username.equals(foo4)) {
Intent nextActivity5 = new Intent(getBaseContext(),FifthActivity.class);
nextActivity5.putExtra("Fifth",username);
startActivity(nextActivity5);
}
else if(username.equals(foo5)) {
Intent nextActivity6 = new Intent(getBaseContext(),SixthActivity.class);
nextActivity6.putExtra("Sixth",username);
startActivity(nextActivity6);
}
else {
Toast.makeText(MainActivity.this,"Result does not exist! Try later!!", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(MainActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
}
dbUser.close();
}
}catch(Exception e) {
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
Button btreg = (Button)findViewById(R.id.button2);
btreg.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent nextActivity = new Intent(getApplicationContext(),Register.class);
startActivity(nextActivity);
}
});
}
}
SecondActivity.java
public class SecondActivity extends Activity {
Intent intent = getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
}
}
All other activities are coded similar to the above.
Here is my logcat.
10-28 14:53:10.688: E/AndroidRuntime(1196): Process: com.skcetresults, PID: 1196
10-28 14:53:10.688: E/AndroidRuntime(1196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skcetresults/com.skcetresults.SecondActivity}: java.lang.NullPointerException
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.ActivityThread.access$700(ActivityThread.java:135)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.os.Looper.loop(Looper.java:137)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.ActivityThread.main(ActivityThread.java:4998)
10-28 14:53:10.688: E/AndroidRuntime(1196): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 14:53:10.688: E/AndroidRuntime(1196): at java.lang.reflect.Method.invoke(Method.java:515)
10-28 14:53:10.688: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
10-28 14:53:10.688: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
10-28 14:53:10.688: E/AndroidRuntime(1196): at dalvik.system.NativeStart.main(Native Method)
10-28 14:53:10.688: E/AndroidRuntime(1196): Caused by: java.lang.NullPointerException
10-28 14:53:10.688: E/AndroidRuntime(1196): at com.skcetresults.SecondActivity.onCreate(SecondActivity.java:15)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.Activity.performCreate(Activity.java:5243)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-28 14:53:10.688: E/AndroidRuntime(1196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
10-28 14:53:10.688: E/AndroidRuntime(1196): ... 11 more
With just two intents the code worked fine, but when I'm comparing the value with strings, the app crashes, I don't know where the bug is.
Instead of getBaseContext() and getApplicationContext(), use MainActivity.this as context, will work everywhere.
You should put it like this
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Intent i = getIntent();
}
}
If the extra data are strings you can get it like this:
String nameString = i.getStringExtra("nameExtra");
You can't use getIntent() before onCreate(), there's simply no Intent available at that point.
In your code, I suspect that you are trying to get StringExtras from Intent, But as intent is null, it is throwing NullPointerException.
In your Second Activity you can use getIntent() in onCreate() method.
public class SecondActivity extends Activity {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
intent = getIntent();
String userName = intent.getStringExtra("Second");
...
}
}
Use little pieces of code checking, to ensure your intent behaves properly. For example, you could surround your getIntent code with an if statement. For which proper method to call, you have to wander around a bit, as this depends on what you want your app to do. Fortunately, Eclipse delivers you all the related documentation, when you write down some piece of Android resources, e.g. Intent.
I have this code below. It works before in Activity. Now I want to convert it to Fragment. I have tried context.getContentResolver(); or getActivity().getContentResolver(); But it doesn't work. How to fix this ?
public class MyFragmentA extends Fragment {
//song list variables
private ArrayList<Song> songList;
private ListView songView;
public Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
//retrieve list view
songView = (ListView) myFragmentView.findViewById(R.id.song_list);
//instantiate list
songList = new ArrayList<Song>();
//get songs from device
getSongList();
//sort alphabetically by title
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
return a.getTitle().compareTo(b.getTitle());
}
});
//create and set adapter
SongAdapter songAdt = new SongAdapter(this.getActivity(), songList);
songView.setAdapter(songAdt);
return myFragmentView;
}
public void getSongList() {
ContentResolver musicResolver = context.getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
----------------------
----------------------
----------------------
----------------------
}
}
}
This my logcat trace
11-25 20:29:34.001: E/AndroidRuntime(4092): FATAL EXCEPTION: main
11-25 20:29:34.001: E/AndroidRuntime(4092): Process: com.ddev.delta, PID: 4092
11-25 20:29:34.001: E/AndroidRuntime(4092): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ddev.delta/com.ddev.delta.AndroidNavigationTabsActivity}: java.lang.NullPointerException
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.os.Looper.loop(Looper.java:136)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.ActivityThread.main(ActivityThread.java:5146)
11-25 20:29:34.001: E/AndroidRuntime(4092): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 20:29:34.001: E/AndroidRuntime(4092): at java.lang.reflect.Method.invoke(Method.java:515)
11-25 20:29:34.001: E/AndroidRuntime(4092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-25 20:29:34.001: E/AndroidRuntime(4092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-25 20:29:34.001: E/AndroidRuntime(4092): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
11-25 20:29:34.001: E/AndroidRuntime(4092): at dalvik.system.NativeStart.main(Native Method)
11-25 20:29:34.001: E/AndroidRuntime(4092): Caused by: java.lang.NullPointerException
11-25 20:29:34.001: E/AndroidRuntime(4092): at com.ddev.delta.AndroidNavigationTabsActivity.onCreate(AndroidNavigationTabsActivity.java:17)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.Activity.performCreate(Activity.java:5231)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-25 20:29:34.001: E/AndroidRuntime(4092): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
11-25 20:29:34.001: E/AndroidRuntime(4092): ... 12 more
ContentResolver musicResolver = context.getContentResolver();
Your context is not initialized.
Since you're in a fragment lifecycle callback onCreateView() or a method called from one, you can use getActivity() for a Context:
ContentResolver musicResolver = getActivity().getContentResolver();
Remove the Context context; declaration as unnecessary, too.
public Context context;
Your context is null, try use getActivity().getContentResolver();
Initialize context object in onCreateView() before call getSongList():
context = getActivity();
OR
getActivity().getContentResolver()
Create an object of Activity and then initialize it by
Activity a=getActivity();
And then use
ContentResolver musicResolver = a.getContentResolver();
Use Following for these Cases:
Application :- getApplicationContext()
activity :- context
Fragment :- getActivity()
In my Android application i am creating a pie chart using achartengine library. When click a button it takes data from sqlite database and draw a pie chart. This is my code segment.
btnpieChart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SelectDBAdapter selectDBAdapter = SelectDBAdapter
.getDBAdapterInstance(getActivity());
try {
selectDBAdapter.openDataBase();
chartDataMap = selectDBAdapter
.getPieChartData(strBusinessUnit,
currentPeriod, currentYear);
} catch (Exception e) {
selectDBAdapter.close();
e.printStackTrace();
} finally {
selectDBAdapter.close();
}
System.out.println("chartDataMap === "+ chartDataMap);
if (chartDataMap.size() > 0) {
for (Map.Entry<String, Double> entry : chartDataMap.entrySet()) {
lstBrandNames.add(entry.getKey());
lstAchievedVals.add(entry.getValue());
}
ArrayList<Double> distribution = calc_Percentage(lstAchievedVals);
System.out.println("distribution === " + distribution);
lstBrandNames = set_lables(lstBrandNames, distribution);
CategorySeries distributionSeries = new CategorySeries(
"Brands - Achievement Progress");
for (int i = 0; i < distribution.size(); i++) {
distributionSeries.add(lstBrandNames.get(i), distribution.get(i));
}
DefaultRenderer defaultRenderer = new DefaultRenderer();
defaultRenderer.setApplyBackgroundColor(true);
defaultRenderer.setBackgroundColor(Color.WHITE);
for (int i = 0; i < distribution.size(); i++) {
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
seriesRenderer.setColor(color);
seriesRenderer.setGradientEnabled(false);
seriesRenderer.setDisplayChartValues(true);
seriesRenderer.setShowLegendItem(false);
defaultRenderer.setLabelsTextSize(25);
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
defaultRenderer.setLabelsColor(Color.BLACK);
defaultRenderer.setChartTitle("Brands - Achievement Progress");
defaultRenderer.setChartTitleTextSize(30);
defaultRenderer.setZoomButtonsVisible(true);
defaultRenderer.setShowLabels(true);
FragmentTransaction ft = getFragmentManager().beginTransaction();
mChartView = ChartFactory.getPieChartView(getActivity(), distributionSeries, defaultRenderer);
ft.replace(mChartView.getId(), new DummySectionFragment(), "NewFragmentTag");
ft.addToBackStack(null);
ft.commit();
}
}
});
My mChartView is,
private GraphicalView mChartView = null;
Log cat says:
11-25 07:03:58.311: E/AndroidRuntime(2706): FATAL EXCEPTION: main
11-25 07:03:58.311: E/AndroidRuntime(2706): android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.content.res.Resources.getResourceName(Resources.java:1659)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.os.Handler.handleCallback(Handler.java:725)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.os.Looper.loop(Looper.java:137)
11-25 07:03:58.311: E/AndroidRuntime(2706): at android.app.ActivityThread.main(ActivityThread.java:5039)
11-25 07:03:58.311: E/AndroidRuntime(2706): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 07:03:58.311: E/AndroidRuntime(2706): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 07:03:58.311: E/AndroidRuntime(2706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-25 07:03:58.311: E/AndroidRuntime(2706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-25 07:03:58.311: E/AndroidRuntime(2706): at dalvik.system.NativeStart.main(Native Method)
I would be much obliged if anyone could explain what's going on here and how can I solve this issue.
Thanks
Check these lines
mChartView = ChartFactory.getPieChartView(getActivity(), distributionSeries, defaultRenderer);
ft.replace(mChartView.getId(), new DummySectionFragment(), "NewFragmentTag");
especially mChartView.getId() log this value and check it with what you expected
I am very new to android. I have a counter on some SomeActivity, but when I get to the page corresponding to SomeActivity, my app crashes :
final TextView counter = (TextView) findViewById(R.id.laws_counter);
ImageView handDown = (ImageView) findViewById(R.id.handViewDown);
counter.setText("" + 0);
I want that on click of the handown, the counter is idented by -1. Here's
handDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(NewsActivity.this,
"The favorite list would appear on clicking this icon",
Toast.LENGTH_LONG).show();
setDown();
}
private void setDown() {
String count = counter.getText().toString();
int now_count = Integer.parseInt(count) +1;
counter.setText(String.valueOf(now_count));
}
});
Is this code correct ?
Update : here's the logcat
10-22 00:18:05.579: ERROR/AndroidRuntime(378): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.donnfelker.android.bootstrap/com.donnfelker.android.bootstrap.ui.NewsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.donnfelker.android.bootstrap.ui.NewsActivity.onCreate(NewsActivity.java:41)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
... 11 more
There are many ways that you can implement that functionality but think this would be an easy solution.
Make a helper method:
public class PreferencesData {
public static void saveInt(Context context, String key, int value) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
sharedPrefs.edit().putInt(key, value).commit();
}
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
return sharedPrefs.getInt(key, defaultValue);
}
}
Then simply call the putInt method to save the counter in any Activity and getInt to get it again in any other Activity. As long as you use the same key both places.