[Q] Problem coding with android studio - General Questions and Answers

it seems I have a single bug that I can't seem to clarify and fix
Code:
public static final class id {
public static final int Nexus 5 (5_0")=0x7f070001;
public static final int dummy_button=0x7f070003;
public static final int fullscreen_content=0x7f070000;
public static final int fullscreen_content_controls=0x7f070002;
public static final int switch2=0x7f070004;
in the second line, it underlines( Nexus 5 (5_0")=0x7f070001; ) and show it as a bug when I run the app. what did I do wrong, was renaming the device in the design section cause an error to happen in the code?

Related

MediaPlayer.setDataSource(url) not working

MediaPlayer.setDataSource(url) is not working in my app.
Im puttin the file "hello.mp3" in the assets-folder, and Im using the following
Code:
String soundUrl = "file:///android_asset/hello.mp3";
mp = new MediaPlayer();
try {
mp.setDataSource(soundUrl);
mp.prepare();
mp.start();
}
catch (IOException e) {}
catch (IllegalArgumentException e) {}
catch (IllegalStateException e) {}
This code works when I put the "hello.mp3" in the res/raw-folder:
Code:
mp = new MediaPlayer();
mp = MediaPlayer.create(getBaseContext(), R.raw.hello);
mp.start();
Problem with the last code is that I need to load the sound-files dynamically, and apparently you cant create a string or a uri with the correct sound-file at the end and insert that as the second parameter in the MP.create()-function.
Pseudocode - NOT WORKING
Code:
String mySound = "hello";
Uri myUri = "R.raw." + mySound;
...
mp = MediaPlayer.create(getBaseContext(), myUri);
Any ideas?
Amazing, several months later and still no one is able to realte how to play a local sound dynamically.
An issue with what you are trying to do is that there are multiple versions of MediaPlayer.create() and MediaPlayer.setDataSource() which take different types of parameters.
R.raw.hello is NOT a string. It is an int. Look in the gen directory to find the generated file R.java and in this file you will find raw which has a public static final int definition for R.raw.hello
R.raw.hello has to be passed to one of the routines which takes an int for the parameter and not a string.

[Solved] findViewById Returns Null?

Hi all,
I'm in the process of adding more features to Contacts.apk (http://forum.xda-developers.com/showthread.php?t=599194), and have run into a "bug" with my latest attempt in adding the green call icon to the Contacts list (similar to Call Log), so that you can dial directly without first tapping on a Contact.
I've posted the problem here: http://www.anddev.org/viewtopic.php?p=31252 and would really appreciate if you all could give me some pointers.
Thank you .
Also, the main issue is:
view.findViewById(R.id.call_icon);
is returning null for some reason.
Note: My github does not contain the source with this problem, please go to the anddev thread and download it from there. Thanks!
Did you pull your pants down then try? It worked for me last time
try typecasting callView
I only looked briefly ... sooo ...
How is callView defined in ContactsListActivity.java?
github doesn't show it ....
Code:
final static class ContactListItemCache {
public TextView nameView;
public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
public TextView labelView;
public CharArrayBuffer labelBuffer = new CharArrayBuffer(128);
public TextView numberView;
public CharArrayBuffer numberBuffer = new CharArrayBuffer(128);
public ImageView presenceView;
public ImageView photoView;
}
Also, consider a matching return type before the function call ... something like I dunno ...
Code:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = super.newView(context, cursor, parent);
final ContactListItemCache cache = new ContactListItemCache();
cache.nameView = (TextView) view.findViewById(R.id.name);
cache.labelView = (TextView) view.findViewById(R.id.label);
cache.numberView = (TextView) view.findViewById(R.id.number);
cache.presenceView = (ImageView) view.findViewById(R.id.presence);
cache.photoView = (ImageView) view.findViewById(R.id.photo);
- cache.callView = view.findViewById(R.id.call_icon);
+ cache.callView = (DontPressWithParentImageView ) view.findViewById(R.id.call_icon);
if (cache.callView == null)
Log.d("NULL: CALLVIEW", "Why?");
view.setTag(cache);
return view;
}
I could be off base here ... but since the git appears incomplete in relation to your posted problem ... etc ... best I can do in a quick drive by. Hope it helps.
~enom~
enom: If you go to the anddev thread I've attached the full (non-working, bugged) source. In it:
Code:
final static class ContactListItemCache {
public TextView nameView;
public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
public TextView labelView;
public CharArrayBuffer labelBuffer = new CharArrayBuffer(128);
public TextView numberView;
public CharArrayBuffer numberBuffer = new CharArrayBuffer(128);
public ImageView presenceView;
public ImageView photoView;
public View callView;
}
jasonpeinko: I did that already, not working. The main issue here is view.findViewById(R.id.call_icon) is returning null .
Thanks for the help guys!
So ... I must be blind ... I don't see the attached bugged source ... still could be off base here ... but ...
Since call_icon is type View, should it not be as such in the xml ...
Code:
/>
- <com.android.contacts.ui.widget.DontPressWithParentImageView android:id="@+id/call_icon"
+ <View android:id="@+i/call_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
??
Either way, it's late I need sleep.
~enom~
enom: Nope. View is the base class so be it a Button, ImageButton, etc, they can still be assigned to View... .
did you call
Code:
setContentView(R.layout.main);
before your findViewById?
Camalot i understand thats the usual approach but in this case the content view is already set by the constructor in the code. I will give your advice a shot later, but i will be really confused if it works. Thanks!
I found the bug. It's because the layout in use was contacts_list_item_photo.xml and not contacts_list_item. *smacks head*
Thanks for the input guys !

Java homework..

I missed one class of my java programming class, and now i do not know what to do at all. If anyone could do the problems for me, i would be grateful. Before i get bashed for having someone do my homework, i can learn really easy if someone does it for me, and i can work out how they did it through a matter of trial and error.
Thank you.
Everything you need to know about the Java String class is on this page, including a few samples that are very similar to your assignment.
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
I still don't understand it. I can only learn when someone shows me, so unless someone here can do it i guess i have to turn it in as a zero.
Answer to 1. is here 2 and 3 are up to you.
Code:
class StringApp {
public static void main(String[] args) {
String str,str1,str2,str3;
int length;
str="CSE 201";
length=str.length();
str1="ABC";
str2="XYZ";
str3=str1.concat(str2);
}
}
P.S. I've never written a line of Java code before. Luckily it is not that far from C#.
Thank you! Two more problems left.
Awwwwww....... What the hell.....
Answers to 2 and 4:
Code:
class TestApp
{
public static void main(String[] args)
{
String phrase = "To be or not to be";
String anotherPhrase="not";
String longphrase;
String today ="Tuesday,January 18";
int commapos;
System.out.printf("a. %s is %d characters long.\n",phrase, phrase.length());
System.out.printf("b. %d\n",phrase.indexOf(anotherPhrase));
System.out.printf ("c. %s\n",phrase.substring(3, 12));
longphrase=phrase + ", that is the question.";
System.out.printf ("d. %s\n",longphrase);
System.out.printf("e. %d\n",phrase.indexOf("Alien"));
System.out.printf("f. %c\n",phrase.charAt(0));
System.out.printf("g. %c\n",phrase.charAt(phrase.length()-1));
commapos=today.indexOf(",");
System.out.printf("Today is %s. What a great %s!\n",today.substring(commapos+1),today.substring(0,commapos));
}
}
When run produces:-
Code:
a. To be or not to be is 18 characters long.
b. 9
c. be or not
d. To be or not to be, that is the question.
e. -1
f. T
g. e
Today is January 18. What a great Tuesday!
Question 3.
c. str.charAt(0); // str. missing
d. int n=str1.indexOf(str2); // dot missing between str1 and IndexOf()

[Resolved] [Q][DEV]Android Development Question about Shared Preferences.

Hi, I'm trying to do a simple login with Facebook in my app but I'm having trouble with Shared Preferences.
The idea is to start the app, it opens Activity A, checks if it's logged, and if it isn't, it sends you to activity B, you login and then go back to A.
My problem is that I can't get the SharedPreferences. I can save it, but I can't get it in the other activity.
So, it gets in a loop: A can't get the SP, so thinks it's not logged in, so send you to B, but B is logged on, and sends you to A...
That's my code in B:
Code:
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor edit = fbSP.edit();
edit.putString("access_token", fb.getAccessToken());
edit.putLong("access_expires", fb.getAccessExpires());
edit.commit();
aIMG();
ir();
}
And that's my code in A, where the problem is:
Code:
private SharedPreferences prefs;
public static String TOKEN = null;
public static final String FACEBOOK_DATA = "FacebookStuff";
long EXPIRES = 0;
...
private void SharedP() {
// TODO Auto-generated method stub
prefs = getSharedPreferences(FACEBOOK_DATA, MODE_PRIVATE);
TOKEN = prefs.getString("access_token", null);
EXPIRES = prefs.getLong("access_expires", 0);
if (TOKEN == null && EXPIRES == 0) { //If it's not logged in...
Intent login = new Intent("android.intent.action.FACELOGIN");
startActivity(login);
}
}
Edit: I got it. I was iniciating fbSP with getPreferences, not getSharedPreferences.

Question Joying UIS broadcast receiver messages in android program

Hello,
I have an earlier developed program with these content:
public final static String MTCBroadcastVolChanged = "com.microntek.VOLUME_CHANGED";
public final static String MTCBroadcastVideosignalChange = "com.microntek.videosignalchange";
public final static String MTCBroadcastBackviewend = "com.microntek.backviewend";
On new joying hu the com.microntek doesn't work. What should I replace with these to get it work on joing uis head unit?
Thanks

Categories

Resources