Hello,
I am newbie to android programming.I am trying to make a simple app.The app window has 1 button and 1 ImageView.The button will capture the image and will set ImageView to it.So i wrote a little bit of the code and when I try to run it on emulator,after clicking the capture button I get an error saying application stopped.Please help me!
package com.example.file;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.ImageView;
import java.io.InputStream;
public class MainActivity extends Activity {
Bitmap mImageBitmap;
Button Capture;
ImageView mImageView;
int actioncode=0;
Intent i;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Capture=(Button)findViewById(R.id.c1);
Capture.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
dispatchTakePictureIntent(actioncode);
handleSmallCameraPhoto(i);
}
});
mImageView=(ImageView)findViewById(R.id.i);
}
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
mImageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(mImageBitmap);
}
}
Please use code-tags!!
Plz read your **LogCat**.
Regards
bhushan.crysis said:
Hello,
I am newbie to android programming.I am trying to make a simple app.The app window has 1 button and 1 ImageView.The button will capture the image and will set ImageView to it.So i wrote a little bit of the code and when I try to run it on emulator,after clicking the capture button I get an error saying application stopped.Please help me!
Click to expand...
Click to collapse
Looking at your code, you will get a NullPointerException on calling intent.getExtras() in the last method since you're calling it in the Button click. You should instead override the onActivityResult check request and Result code and call that method there
But in general, make sure to read This awesome guide on debugging, that way you can solve your problems yourself!
Related
How would I go about saving the state of the rating of the stars when I click the button? I need it to save how many stars it has up when I reopen the app. Here is what I have so far...
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class HelloAndroid extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
Toast.makeText(HelloAndroid.this, "State saved", Toast.LENGTH_SHORT).show();
}});
final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(HelloAndroid.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
}
});
}
}
I would suggest http://developer.android.com/guide/topics/data/data-storage.html. I haven't used it yet but it should work for what you are doing.
Hello,
i am trying to use the ...1 and ...2 keys.
These are the two keys between "." and "Shift".
(see http://www.android-hilfe.de/attachm...-neuer-htc-desire-z-sammelthread-img_5169.jpg)
So I have written an app that gives my the keycodes via LogCat:
Code:
package default;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout.LayoutParams;
public class ShowKeycodeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
setContentView(new MyView(this));
}
class MyView extends View {
private static final String LOGID = "MxView";
String message = "No key pressed yet.";
MyView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawText(message, 5, 20, paint);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent ev) {
Log.i(LOGID, keyCode+"");
invalidate();
return true;
}
}
}
All keys have a keycode but those special keys not.
HTC Sense can use these keys.
What can I do now?
It seems as if the kernel does not now that these keys exist?
Thank you
Tobi
I think only sense kernels support these keys
Sent from my HTC Vision using XDA App
With CyanogenMod 7 one can assign custom shortcuts to it.
But I want to use them as cursor left/right...
Try the Buttonremapper to do that!
Great. This is exactly what I've been looking for!
Thank you
hello I need help i need it to when the user clicks a button in my app is calls a specific phone number when I try doing it on my phone i click the button and it says app has crashed
Please help heres my class file:
Code:
package com.d4a.kingdomapp;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.net.Uri;
public class ComputerIssues extends Activity {
String number = "573-642-2800";
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.computerissues);
// Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such.
Button callfulton =(Button)findViewById(R.id.fultonoffice);
callfulton.setOnClickListener(buttonhandler);
}
View.OnClickListener buttonhandler=new View.OnClickListener() {
// Now I need to determine which button was clicked, and which intent or activity to launch.
public void onClick(View v) {
switch(v.getId()) {
// Now, which button did they press, and take me to that class/activity
case R.id.fultonoffice: //<<---- notice end line with colon, not a semicolon
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);
}
}
};
}
Thanks in advance
Regards
Rapsong11
Have you declared the permission in your menifest?
Add this permission
Yes I have
Thanks for the reply
Sent from my Nexus 4 using xda app-developers app
I am having trouble using the back button to go back in webview. I have 5 different fragments in a tab + swipe layout. Each having a different webview. I have my MainActivity.Java as well as a java file for each fragment. Using the examples shown in stackoverflow regarding this (can't link. Sorry) I get a force close whenever I press back on the app. So I received some helped and did a non-conventional stab at it but I get a couple of errors so I'm hoping one of you guys can help me out. I am super new to app development and java in general so please bear with me.
Here is my MainActivity.java where the errors are:
Code:
package com.-------;
import java.util.Locale;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.webkit.WebViewFragment;
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
[user=439709]@override[/user]
public void onBackPressed(){
mSectionsPagerAdapter.getLastFragment().tryGoBack(); [B]<-Error #1[/B]
}
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private WebViewFragment lastFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public WebViewFragment getLastFragment() {
return lastFragment;
}
[user=439709]@override[/user]
public Fragment getItem(int position) {
WebViewFragment Fragment;
switch (position) {
case 0:
Fragment = new fragment1();
break;
case 1:
Fragment = new fragment2();
break;
case 2:
Fragment = new fragment3();
break;
case 3:
Fragment = new fragment4();
break;
case 4:
Fragment = new fragment5();
break;
default:
Fragment = null;
break;
}
lastFragment = Fragment;
return Fragment; [B]<-Error #2[/B]
}
[user=439709]@override[/user]
public int getCount() {
return 5;
}
[user=439709]@override[/user]
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.tab1).toUpperCase(l);
case 1:
return getString(R.string.tab2).toUpperCase(l);
case 2:
return getString(R.string.tab3).toUpperCase(l);
case 3:
return getString(R.string.tab4).toUpperCase(l);
case 4:
return getString(R.string.tab5).toUpperCase(l);
}
return null;
}
}
}
Error #1 is in line 26 and says:
Code:
The method tryGoBack() is undefined for the type WebViewFragment
Error #2 is in line 80 and says
Code:
Type mismatch: cannot convert from WebViewFragment to Fragment
Here is one of my fragment java files as well:
fragment1.java
Code:
package com.-----;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebViewFragment;
public class fragment1 extends WebViewFragment {
public abstract class WebViewFragment extends Fragment{
public abstract void tryGoBack();
}
WebView myWebView;
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment1, container, false);
myWebView = (WebView) root.findViewById(R.id.webview1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("url");
return root;
}
}
Any help is greatly appreciated!
When dealing with two or different classes which all extend one of them, so in this case the WebViewFragment extending Fragment and your fragment1 inheriting from WebViewFragment, you can convert the object simply putting
Code:
(NewType)
in front of it. So for error 1, insert (Fragment1) and for error 2 insert (Fragment).
One advice I'd like to give you, Capitalize class names and don't capitalize object and variable names, so it should be Fragment1 (maybe a more clear name) and WebViewFragment fragment.
Cheers fella's
For a bluetooth remote project I wanted to add some extra tools to increase the ease of use.
At this moment the application is able to turn on and off bluetooth, show a list of paired devices but.. I'd also like to inplement a listview with new unpaired devices.
The thing is, I've got it working.. it seems to logg the new devices but I think there is a catch with my Listview.
after pressing the button to scan for devices the application discovers my bluetooth device but here's what happens after.
A new device is discovered, the listview displays it's name twice underneath eachother, and within a second the activity just shuts off. After trying it twice of thrice the whole application shuts off.
sadly I can't check my logger because after the error there is so much happening that the logg is too short and the error gets deleted.
here's the code for the discovering new devices
```package com.example.bluetest;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class Discoverdevice extends AppCompatActivity {
BluetoothAdapter myBluetoothAdapter;
ArrayAdapter<String> arrayAdapter2;
ArrayList arrayList2;
ListView lvnewdevice;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discoverdevice);
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
lvnewdevice = (ListView)findViewById(R.id.lvnewdevice);
arrayList2 = new ArrayList();
arrayAdapter2 = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList2);
lvnewdevice.setAdapter(arrayAdapter2);
}
public void discoverDevices (View view)
{myBluetoothAdapter.startDiscovery();}
BroadcastReceiver nreceiver = new BroadcastReceiver() {
@override
public void onReceive(Context context, Intent intent) {
String nDaction = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(nDaction)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
arrayList2.add(device.getName());
arrayAdapter2.notifyDataSetChanged();
}
}
};
@override
protected void onStart() {
super.onStart();
IntentFilter newdevice = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(nreceiver, newdevice);
}
}