[APP]2 java files dosen't work - Android Apps and Games

Hi people.
I'm new in JAVA develop, and I'm trying do a simple game.
I tryed to work with 2 separated java files, but I don't know what I'm doing wrong. Example:
File "TesteActivity.java"
package pack.teste;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TesteActivity extends Activity {
test1 test = new test1();
public TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.maoe);
text.setText(test.hahae);
}
}
File "test1.java"
package pack.teste;
public class test1 {
TesteActivity test = new TesteActivity();
public String hahae = "maoe";
}
And dosen't work. Somebody can help me?
Thanks

Related

Android Developers: HelloGallery tutorial: "android.R.styleable cannot be resolved"

Android Developers: HelloGallery tutorial: "android.R.styleable cannot be resolved"
Hello,
I've been following the Views tutorials included in the android developer site:
http://developer.android.com/guide/t...o-gallery.html
When testing the "Hello Gallery" view, I get the compile-time error:
"android.R.styleable cannot be resolved"
I searched for this error on this group but is wasn't answered.
Can anyone help?
The problem reference occurs in the imageAdapter class that is appended after the onCreate() method in the HelloGallery class:
public class ImageAdapter extends BaseAdapter
{
...
TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
}
Strangely enough, when I insert this line into the onCreate() method at the start of the package, there is no problem. It seems that the android.R class is not known to this class.
Thanks,
Paul
any resolution to this issue?
I am running into the same issue and was wondring if you had found a resolution?
Thanks in advance,
-Nathan
tell me you know what is import
If it is what you mean, "import android.R.styleable" is not possible.
vincebodi said:
If it is what you mean, "import android.R.styleable" is not possible.
Click to expand...
Click to collapse
No, it's impossible. As I realized after I did that post, they changed the example codes in 1.5 SDK and broke some applications. If you lose "android." the code should compile.
I copied "ImageAdapter" Class into "HelloGallery" Class. And it's work.
Here is my code:
package com.example.hellogallery;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class HelloGallery extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
Toast.makeText(HelloGallery.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2,
R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7 };
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
attrs.xml and obtainStyledAttributes
Thanks for the code tonghoho...I still had the same problem until I uploaded attrs.xml from the samples, following instructions here: http://www.mail-archive.com/[email protected]/msg34088.html . I used the same code above, but now I'm getting another error. Here's the part with the problem:
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
The error is for obtainStyledAttributes, and is:
The method obtainStyledAttributes(int[]) is undefined for the type ImageAdapter
Any ideas?
Thanks ahead of time...
@Collene: you probably have ImageAdapter in a separate file and not as an inner class of HelloGalery? obtainStyledAttributes() is a method of the Context class so you have to change the code like so:
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.default_gallery);
Complete solution can be found in this thread: http://www.mail-archive.com/[email protected]/msg11862.html
Thank you so much for your help David B. I can see now that the tutorial left off the letter "c" there, and eclipse, while awesome, wasn't really helping me out with diagnosing the problem.

[Q] First program (error message)

Hi, i know a little bit of java so i was thinking start to create program to my android phone.
but even "hello world" program can't run, i get message:
An internal error occurred during: "Launching New_configuration".
Path for project must have only one segment.
How to fix this ?
Thanks
so no one knows how to fix this ?
I might be helpfull if you would've posted the code you have
it's from google site android hello world program
nevertheless :
package com.android.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}

[Q] Joystick Problem (java)

Hey, I'm MakyOnCover and please sorry my English!
I'm trying to make a Android Game in java, but I'm having problems with some kind of joystick I'm trying to denvelop!
The situation is this: the bitmap named joystick1 is not creating when I pressed the screen, so I would like to know why and how can I resolve this problem?
Thank you for your attention!
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
public class Class1 extends Activity implements OnTouchListener{
/** Called when the activity is first created. */
GameView a;
boolean toque;
float x, y, jposx, jposy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
a = new GameView(this);
a.setOnTouchListener(this);
setContentView(new GameView(this));
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
return false;
}
public class GameView extends SurfaceView implements Runnable{
private Bitmap joystick1,joystick2;
private Bitmap shot = BitmapFactory.decodeResource(getResources(),R.drawable.shot);
Class1 e;
SurfaceHolder a;
Thread thread1 = null;
boolean correr = true;
public GameView(Context context){
super (context);
a = getHolder();
thread1 = new Thread(this);
thread1.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(correr){
if(!a.getSurface().isValid())
continue;
Canvas canvas = a.lockCanvas();
joystick2 = BitmapFactory.decodeResource(getResources(),R.drawable.joystick2);
canvas.drawBitmap(joystick2,16,16,null);
if (x != 0 && y != 0){
joystick1 = BitmapFactory.decodeResource(getResources(),R.drawable.joystick1);
canvas.drawBitmap(joystick1,x,y,null);
}
canvas.drawBitmap(shot,0 + 20,canvas.getHeight() - shot.getHeight() - 18,null);
a.unlockCanvasAndPost(canvas);
}
}
}
}

[Q] Codeing Help (andriod)

Today i decided to learn to code using eclipse and the andriod skd (2.3.3), I'm trying to code a basic soundboard and its not coming up with errors but the sounds are still not playing. If anyone could look at the code and give me a hand that would be great
Code:
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
public class PlayIt extends Activity implements OnClickListener {
MediaPlayer mp1;
MediaPlayer mp2;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp1 = MediaPlayer.create(this, R.raw.froze);
mp2 = MediaPlayer.create(this, R.raw.lasagna);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
final Button button = (Button) findViewById(R.id.button02);
button.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
mp1.start();
break;
case R.id.button02:
mp2.start();
break;
}
}
@Override
protected void onDestroy() {
mp1.release();
mp2.release();
super.onDestroy();
}
}
}
i dont have any idea about any code

[Q] WebView app opens up default browser.

Hello I am a Android newbie and I am currently learning how to code applications using Eclipse so I made one yesterday using webview on eclipse but everytime I open the app it takes me to the website but on my default browser. I want the website to open up in the app not on my browser. Anyone know how to fix this?
You need to read the WebView Reference on Android.
Apparently you open the website on a Intent, so the website is opened on the default browser.
Instead, you need to make you Activity act as a WebView on the 'onCreate()' :
Code:
WebView webview = new WebView(this);
setContentView(webview);
And then open the website :
Code:
// Simplest usage: note that an exception will NOT be thrown
// if there is an error loading this page (see below).
webview.loadUrl("my_url");
// OR, you can also load from an HTML string:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
// ... although note that there are restrictions on what this HTML can do.
// See the JavaDocs for loadData() and loadDataWithBaseURL() for more info.
Um this is my MainActivity.java
package com.graaltailor.JFA;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("examplewebsite");
}
}
You need to do something like this to overwrite urlredirect opened in the default browser :
Code:
package com.graaltailor.JFA;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("examplewebsite");
}
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
}
I replaced the code with the one you replied with and I get a bunch of errors.
sorry I made a mistake in the code
Code:
package com.graaltailor.JFA;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("examplewebsite");
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
}
}

Categories

Resources