I have tried for two days to write a code that dial a phone number , but I failed
I have written a midlet and a form called main as a Displayable the form contains a textfield and
command .
When the application starts up the form should appears and calls the dialed number written in
textField when the command pressed.
the dialing process didn't work with me .
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable.*;
import javax.microedition.lcdui.Form.*;
import javax.microedition.midlet.MIDlet;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class DiallNumber extends MIDlet implements CommandListener
{
String num;
private Form main;
private Command call,dial;
private TextField phoneField;
Display display;
public DiallNumber()
{
main = new Form("main");
phoneField = new TextField("label","",10,phoneField.PHONENUMBER);
call = new Command("call",call.OK,0);
main.append(phoneField);
main.addCommand(call);
num=this.phoneField.getString().trim();
main.setCommandListener(this);
}
/**}
* From MIDlet. Called when the MIDlet is started.
*/
public void startApp()
{
// The initial display is the first form
display = Display.getDisplay(this);
display.setCurrent(main);
}
public void call( String number) {
try {
platformRequest("tel:"+number);
} catch (ConnectionNotFoundException ex) {
// TODO: Exception handling
}
}
public void commandAction(Command c, Displayable d){
if(d==main)
{
if(c==call)
call(""+num);
}
}
public void pauseApp()
{
// No implementation required
}
/*
* /
*/
/**
* From MIDlet. Called to signal the MIDlet to terminate.
*
* #param unconditional
* whether the MIDlet has to be unconditionally terminated
*/
public void destroyApp(boolean unconditional)
{
// No implementation required
}
/**
* From CommandListener. Called by the system to indicate that a command has
* been invoked on a particular displayable.
*
* #param command
* the command that was invoked
* #param displayable
* the displayable where the command was invoked
*/
}
My log trace
Copying 1 file to C:\Users\ELHADI\Documents\NetBeansProjects\DiallNumber2\dist\nbrun5217990045006831680
Copying 1 file to C:\Users\ELHADI\Documents\NetBeansProjects\DiallNumber2\dist\nbrun5217990045006831680
Jad URL for OTA execution: http://localhost:8082/servlet/org.netbeans.modules.mobility.project.jam.JAMServlet/C%3A/Users/ELHADI/Documents/NetBeansProjects/DiallNumber2/dist//DiallNumber2.jad
Starting emulator in execution mode
Installing suite from: http://127.0.0.1:49320/DiallNumber2.jad
[WARN] [rms ] javacall_file_open: _wopen failed for: C:\Users\ELHADI\javame-sdk\3.0\work\0\appdb\_delete_notify.dat
I have solved the problem
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable.*;
import javax.microedition.lcdui.Form.*;
import javax.microedition.midlet.MIDlet;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class DiallNumber extends MIDlet
{
private Display display;
private MIDlet midlet;
Form f = new form("f");
/**}
* From MIDlet. Called when the MIDlet is started.
*/
public void startApp()
{
display = Display.getDisplay(this);
display.setCurrent(f);
}
public void pauseApp()
{
// No implementation required
}
/*
* /
*/
/**
* From MIDlet. Called to signal the MIDlet to terminate.
*
* #param unconditional
* whether the MIDlet has to be unconditionally terminated
*/
public void destroyApp(boolean unconditional)
{
// No implementation required
}
class form extends Form implements CommandListener
{
private Command call;
private TextField phoneField;
private String n;
public form(String title )
{
super(title);
call =new Command("call",call.OK,0);
this.phoneField =new TextField("number","",20,phoneField.PHONENUMBER);
addCommand(call);
append(this.phoneField);
this.setCommandListener(this);
}
public void commandAction(Command c,Displayable d)
{
if(c==call)
{
try
{
n=phoneField.getString().trim();
platformRequest("tel:"+n);
}
catch(javax.microedition.io.ConnectionNotFoundException e)
{e.printStackTrace();}
}
}
}
}
Related
The following Tabbed Activity is created by Android Studio 1.2.2 Wizard, it works well in API 9, but someboyd told me that ActionBarActivity is deprecated,
so I hope to replace public class MainActivity extends ActionBarActivity implements ActionBar.TabListener with public class MainActivity extends AppCompatActivity implements ActionBar.TabListener , and stay all other code the same, is it OK?
Thanks!
package com.example.cuiwei.myapplication;
import java.util.Locale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return 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;
}
}
}
You can safely replace ActionBarActivity with AppCompatActivity.
As you can see in v7-appcompat source code from version v22.1.0 ActionBarActivity simply extends AppCompatActivity:
/**
* #deprecated Use {#link android.support.v7.app.AppCompatActivity} instead.
*/
#Deprecated
public class ActionBarActivity extends AppCompatActivity {
}
I initializing a variable in my startApp method and after I do so I call a thread to check the whole run time of the app what is the current screen (canvas) and activating it's thread
import java.io.IOException;
import java.io.InputStream;
import zuma.core.MyGameCanvas;
import zuma.core.Game;
import zuma.gui.LevelSelectionMenu;
import zuma.gui.MainMenu;
import javax.microedition.lcdui.Display;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.*;
import zuma.util.AudioManager;
public class Midlet extends MIDlet implements Runnable{
private static Display display;
private static Game game;
private static MainMenu mainMenu;
private static LevelSelectionMenu levelSelectionMenu;
private static MyGameCanvas myGameCanvas;
private Thread t;
/**
* Specifies what happens when starting the application.
*/
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(getMainMenu());
t = new Thread(this);
t.start();
}
/**
* Specifies what happens when pausing the application.
*/
public void pauseApp() {
}
/**
* Specifies what happens when exiting the application.
* #param unconditional
*/
public void destroyApp(boolean unconditional) {
//game.getLevelsRecordStore().closeRecordStore();
notifyDestroyed();
}
/**
*
* #return the display.
*/
public static Display getDisplay() {
return display;
}
/**
*
* #return the game.
*/
public static Game getGame() {
if (game == null) { //|| game.getLevels() == null) {
game = new Game();
}
return game;
}
/**
*
* #return the mainMenu.
*/
public static synchronized MainMenu getMainMenu() {
if (mainMenu == null) {
mainMenu = new MainMenu();
}
return mainMenu;
}
/**
*
* #return the levelSelectionMenu.
*/
public static LevelSelectionMenu getLevelSelectionMenu() {
if (levelSelectionMenu == null) {
levelSelectionMenu = new LevelSelectionMenu(getGame());
}
return levelSelectionMenu;
}
/**
*
* #return the myGameCanvas.
*/
public static MyGameCanvas getMyGameCanvas() {
if (myGameCanvas == null) {
myGameCanvas = new MyGameCanvas();
}
return myGameCanvas;
}
/**
* Starts the thread of the current display.
*/
public synchronized void run() {
while (true) {
if (display.getCurrent().equals(mainMenu)) {
if (mainMenu.getT() == null || !mainMenu.getT().isAlive()) {
mainMenu.init();
}
if (getMainMenu().isQuitRequest()) {
destroyApp(true);
}
}
else if (display.getCurrent().equals(levelSelectionMenu)) {
if (levelSelectionMenu.getT() == null || !levelSelectionMenu.getT().isAlive()) {
levelSelectionMenu.init();
}
}
else if (display.getCurrent().equals(myGameCanvas)) {
if (myGameCanvas.getT() == null || !myGameCanvas.getT().isAlive()) {
myGameCanvas.init(game.getLevels()[game.getChosenLevel()]);
}
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
Now as you can see I first init the variable mainMenu in this line
display.setCurrent(getMainMenu());
and only then call run method, but somehow SOMETIMES it gives me null pointer exception on this line
if (display.getCurrent().equals(mainMenu))
so somehow the thread is getting called even before the mainMenu is being created and I have no idea how, what I tried is making the run method and the getMainMenu methos synchronized but it didnt work, can anyone help me ?
The thread is not getting called before the main menu is created. After setCurrent is called, there is actually a delay before the displayable is made visible. The doc says: "The setCurrent() method returns immediately, without waiting for the change to take place. Because of this delay, a call to getCurrent() shortly after a call to setCurrent() is unlikely to return the value passed to setCurrent()."
I am getting a NullPointerException which is as follows:
java.lang.NullPointerException
file:/K:/Learner/JavaFx2/ProductApplication/dist/run166129449/ProductApplication.jar!/com/product/app/view/viewsingle.fxml
at com.product.app.controller.ViewSingleController.initialize(ViewSingleController.java:70)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
And my ViewSingleController is as follows:
package com.product.app.controller;
import com.product.app.model.Product;
import com.product.app.service.ViewProductsService;
import com.product.app.util.JSONParser;
import com.product.app.util.TagConstants;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* FXML Controller class
*
* #author Arun Joseph
*/
public class ViewSingleController implements Initializable {
private static String action = "";
#FXML
private TextField txtID;
#FXML
private TextField txtName;
#FXML
private TextField txtPrice;
#FXML
private TextArea txtDesc;
#FXML
private Region veil;
#FXML
private ProgressIndicator p;
private ViewProductsService service = new ViewProductsService();
private JSONObject product = null;
private JSONParser parser = new JSONParser();
private int pid = 1;
public void setPid(int pid) {
this.pid = pid;
}
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");
p.setMaxSize(150, 150);
p.progressProperty().bind(service.progressProperty());
veil.visibleProperty().bind(service.runningProperty());
p.visibleProperty().bind(service.runningProperty());
Product product = new Product();
service.start();
ObservableList<Product> products = service.valueProperty().get();
products.get(pid);
txtID.textProperty().set(String.valueOf(products.get(pid).getPid()));
//product = service.valueProperty().get().get(pid);
//txtID.setText(String.valueOf(product.getPid()));
txtName.textProperty().set(product.getName());
txtPrice.textProperty().set(String.valueOf(product.getPrize()));
txtDesc.textProperty().set(product.getDescription());
}
private SomeService someService = new SomeService();
#FXML
private void handleUpdateButtonClick(ActionEvent event) {
action = "update";
someService.start();
p.progressProperty().bind(service.progressProperty());
veil.visibleProperty().bind(service.runningProperty());
p.visibleProperty().bind(service.runningProperty());
}
#FXML
private void handleDeleteButtonClick(ActionEvent event) {
action = "delete";
someService.start();
p.progressProperty().bind(service.progressProperty());
veil.visibleProperty().bind(service.runningProperty());
p.visibleProperty().bind(service.runningProperty());
}
#FXML
private void handleCancelButtonClick(ActionEvent event) {
closeStage();
}
private void closeStage() {
ViewSingleController.stage.close();
}
private static Stage stage = null;
public static void setStage(Stage stage) {
ViewSingleController.stage = stage;
}
private class SomeService extends Service<String> {
#Override
protected Task<String> createTask() {
return new SomeTask();
}
private class SomeTask extends Task<String> {
#Override
protected String call() throws Exception {
String result = "";
int success = 0;
List<NameValuePair> params = new ArrayList<NameValuePair>();
switch (action) {
case "update":
params.add(new BasicNameValuePair("pid", txtID.getText()));
params.add(new BasicNameValuePair("name", txtName.getText()));
params.add(new BasicNameValuePair("price", txtPrice.getText()));
params.add(new BasicNameValuePair("description", txtDesc.getText()));
product = parser.makeHttpRequest(TagConstants.url_update_product_with_id, "POST", params);
success = product.getInt(TagConstants.TAG_SUCCESS);
if (success == 1) {
result = "Successfully Updated the product";
JOptionPane.showMessageDialog(null, result);
closeStage();
}
break;
case "delete":
params.add(new BasicNameValuePair("pid", txtID.getText()));
product = parser.makeHttpRequest(TagConstants.url_delete_product_with_id, "POST", params);
success = product.getInt(TagConstants.TAG_SUCCESS);
if (success == 1) {
result = "Successfully Deleted the product";
JOptionPane.showMessageDialog(null, result);
closeStage();
}
break;
}
return result;
}
}
}
}
Please help me on how to fix this null pointer problem really help required. Thank you in advance
Inspect the line ViewSingleController.java:70. Put a breakpoint there. Run the program in a debugger and see what variables/fields are null.
The Exception happens on line 70, as you can see from the StackTrace.
On line 70, you call:
txtPrice.textProperty().set(String.valueOf(product.getPrize()));
The NullPointerException means that you are trying to access a method of an object that does not exist. Here, this might be txtPrice, textProperty, product or getPrize.
Skimming over your code, I'd guess it might be txtPrice, because you only set it as a member variable via
private TextField txtPrice;
but you never initialize it. Thus, txtPrice is null and txtPrice.textProperty().set will probably throw the NullPointer.
I'm trying to add a TilePane with ImageView children to a scene in JavaFX. Currently, my FXML is loading an empty TilePane.
The current FXML line that i have making the TilePane is
<TilePane id="MapPane" fx:id="mapPane" layoutX="3.0" layoutY="0.0" prefColumns="9" prefHeight="560.0" prefTileHeight="112.0" prefTileWidth="112.0" prefWidth="1277.0" visible="true"\>
where mapPane is the name of the variable in my .java file
controller:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package screens.gameScreen;
import screens.*;
import mule.*;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.*;
import java.awt.MouseInfo;
import java.awt.Point;
import com.sun.glass.ui.Robot;
/**
* FXML Controller class
*
* #author Stephen
*/
public class GameScreenController implements Initializable, ControlledScreen {
Robot robot = com.sun.glass.ui.Application.GetApplication().createRobot();
ScreenManager screenManager;
TileEngine tileEngine = new TileEngine();
#FXML
TilePane mapPane = tileEngine.createRandomMap(true);;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#Override
public void setScreenParent(ScreenManager screen) {
screenManager = screen;
}
#FXML
private void goToMain(ActionEvent event) {
screenManager.setScreen(mule.MULE.mainMenuScreenID);
}
}
you can use SceneBuilder for create FXML. This can easily solve your problem.
My friend has make me a simple JFrame, and I am meant to work on it and develop it. I have encountered a problem within the first 30 mins of having it: it's not drawing the graphics!!
Here is the code that draws the graphics, and the code from the class that brings up the JFrame.
Thanks in advance.
GameCanvas.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.LinkedList;
/**
*
* #author Kris
*/
public class GameCanvas extends Canvas{
Player p;
LinkedList<Stalker> s = new LinkedList<>();
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
p.paintItAll(g2d);
for(int i = 0; i < s.size(); i++) {
s.get(i).paintItAll(g2d);
}
}
public GameCanvas() {
initComponents();
repaint();
}
public void initComponents() {
p = new Player(new Point(50,50));
s.add(new Stalker(new Point(50,100)));
}
public Point getPlayerPos() {
return p.getPos();
}
}
MainWindow.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* #author Kris
*/
public class MainWindow extends JFrame{
static Toolkit tk = Toolkit.getDefaultToolkit();
static int xSize = ((int) tk.getScreenSize().getWidth());
static int ySize = ((int) tk.getScreenSize().getHeight());
/**
* #param args the command line arguments
*/
public MainWindow() {
super("STALKER!!!!!!!!!!");
GameCanvas canvas = new GameCanvas();
add(canvas, BorderLayout.CENTER);
setSize(xSize, ySize);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
canvas.createBufferStrategy(2);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow();
}
});
}
}
You are using the old AWT Canvas class as the base component. This class follows the AWT painting mechanism, where there is no paintComponent method and you were supposed to override paint to do custom graphics.
Change the base class to JComponent and you can use the newer painting mechanism:
...
import javax.swing.JComponent;
public class GameCanvas extends JComponent {
This article compares how painting works in AWT and Swing if you want to know more about them.