Unfortunately my app stopped working? [duplicate] - java

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 8 years ago.
I tried to display "hello world !" by designing an app using a custom font stored in src/Main/CustomF/times.ttf in my laptop.....while execution it disappears very soon telling that "unfortunately your app stopped"....my coding is as below.....please help me..
package com.example.nambimanavalan.customfonts;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.mine);
Typeface custom=Typeface.createFromAsset(getAssets(),"CustomF/times.ttf");
t.setTypeface(custom);
}
}

you need to put your font in assets/Font folder and than you can use
Typeface custom=Typeface.createFromAsset(getAssets(),"Font/times.ttf");

put your custom font in : assets/CustomF/times.ttf and maybe then it will work
assets folder is directly under your project folder, and whenever you create a fresh project it is empty.

Related

How to embed WebView in Java in Android Auto

I'm new to Java and am currently developing an Android Auto app.
It's using WebView to display local HTML files.
I've tried many ways, but everything went wrong.
My App can start in Android Auto, but embedding WebView fails every time.
And the Internet is like empty on this subject.
Here is what I'm trying currently:
package com.example.webviewandroidauto;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.model.Template;
public class StartScreen extends Screen {
public StartScreen(CarContext carContext) {
super(carContext);
}
#NonNull
#Override
public Template onGetTemplate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("file:///android_asset/myresource.html");
}
}
Can someone please help me out?
Thank you!
Proof that my app starts in Android Auto: https://i.stack.imgur.com/OXC80.png

Use MBTiles from online storage with OSMDroid

I have imported OSMDroid in my project and its running quite ok with default OpenStreetMap. I want to use an MBTiles format map instead of OSM. There are a lot of tutorial for using MBTiles as offline map but my MBTiles file is stored in an online storage such as
http://www.example.com/mymbtiles.mbtiles.
I am using the following code in my MainActivity.java which is xerox of default tutorial of OSMDroid
package com.example.user.osmdroid;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import org.osmdroid.api.IMapController;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
public class MainActivity extends Activity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView map = (MapView) findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController.setZoom(9);
GeoPoint startPoint = new GeoPoint(23.7588, 90.38967);
mapController.setCenter(startPoint);
}
}
How to use that online stored MBTiles file with my OSMDroid project?
You can't use it directly. MBTiles is basically an sqlite database. OSMDroid can read and use it from the device's storage but not over the network. To solve this, you'll have to transfer the database to the device, then tell OSMDroid to use it the mbtiles file via tile source and tile providers.
Edit: considering the size of the database, the simplest thing for you to do is to host a simple web service that fetches the tiles from the database (server side) then provide them to your users via a custom ITileSource. I actually have code for such as service, but it can't be released (yet). The "standard" osm way is http://server/port:path/Z/X/Y.png but you can make it whatever format you want so long as there's a ITileSource setup on the android device. Also, you probably want to see to this example https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/SampleCustomTileSource.java Note, we're extending OnlineTileSourceBase

"R cannot be resolved to a variable" just after creating android project? [duplicate]

This question already has answers here:
"R cannot be resolved to a variable"? [duplicate]
(30 answers)
Closed 9 years ago.
Just after creating the new Android Application Project I get the error "R cannot be resolved to a variable" in my MainActivity.java file.
This is it's content:
package com.example.firstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Why am I getting this problem if the app was created by Eclipse choosing all the default options? How can I fix this issue?
It might be because, there might be some problem in your res folder,
The Main reason might be there could be wrong import file of R.java. So Remove, if any import android.R;
There might be some images with same name, or some files with capital letters, or even there might be some errors in your xml files,
Try rebuilding and cleaning your project.
For some quick solutions to this problem, have a look at this,
R.java file not getting created
first try to clean your project and after that build it through eclipse.If the problem persists after this operation than it means its because of your activity_main.xml

How to use a button to within a app to display the phone's contact list

Hello everyone I am completely new to programing no experience at all so please treat me as a 2 year old when you respond. I have alot of time on my hands as I sit in an office with nothing to do so I figure I should do something productive, and since I have a android tablet I figure I'll try to learn how to make apps for it. This is my first attempt and I'm getting an error in the .java portion of the code, I've been reading and reading on various sites and can't seem to get a clear understanding of what I need to do to correct it. PLEASE HELP
Now down to business, in the app I am trying to learn how to display an image its that simple, I'm taking baby steps. So I started a project, read a few tutorials on how to code ImageView and then I got stuck. I have the error that says "The public type TestImages must be defined in its own file" so I ask how to define/create this file? where to put it?
Here is the .java portion of the code where the error lies.
package com.example.myproject;
import com.example.myproject.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class TestImages extends Activity {
/**Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
ImageView image = (ImageView) findViewById(R.id.test_image);
}
}
Please help, and if you know of any other resource out there that can help a newbie/dummy like myself who don't know anything about coding and need every detail explained please point me in that direction. I've tried the Android Developers site but the information presented on there are not explained in detail it looks as if they expects everyone who tries to do this have a background in coding and I don't. Oh and please don't discourge encourage, because I'm eager to learn.
use on click listener on the button and then check this
https://github.com/android/platform_packages_apps_contacts

Creating Background Image for Android App - Java

I'm just getting started with android development and I need help with background images. I want to be able to have a background image and then overlay other items (buttons, text, etc.) on top of that background with a layout. I used a LinearLayout just for the sake of being simple and because I don't know what's best for me at the moment.
Anyways, I can't get an image to display using the following code:
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.*;
public class NewGameActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(Drawable.createFromPath("/assets/images/androidBackground.png"));
this.setContentView(ll);
}
}
ll.setBackgroundResource(R.drawable.image_name);
http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29
This is the preferred way of accessing drawable resources. The image_name is a png image in your drawable or drawable-mdpi folder.
yes if you are using XML then find the id of that linearlayout like
ll=(LinearLayout)findViewById(R.id.linear1)
Then set its background as
ll.setBackgroundResource(R.drawable.image_name);
else here as your code given here you can directly go for
ll.setBackgroundResource(R.drawable.image_name);

Categories

Resources