Generate random string-java and xml [duplicate] - java

This question already exists:
Android Development-XML and Java Files, Insert Random String into Activity
Closed 8 years ago.
I am trying to write a code to generate a random string from a set of arrays. I need help with fixing this code and then what do I include in the xml file to ensure that the code from java will work? Please help! Thanks!
Below is my code from java:
package com.momsmealplanner;
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class OutcomeActivity extends Activity
{
public String[] myString;
Resources res = getResources();
myString = res.getStringArray(R.array.restArray);
Random random = new Random(System.currentTimeMillis());
int[] textViews =
{
//add textviews to this array
};
for (int v : textViews)
{
TextView tv = (TextView)findViewById(v);
tv.setText(myString[random.nextInt(myString.length)]);
}
}
}
Where am I going wrong?

Related

"kBrushless cannot be resolved or is not a field" error in FRC Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
Importer "kBrushless" states that is is not resolved of not a field. Any help?
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc.robot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.drive.RobotDriveBase.MotorType;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
// import edu.wpi.first.hal.FRCNetComm.tResourceType;
// import edu.wpi.first.hal.HAL;
import frc.robot.autos.*;
import frc.robot.commands.*;
import frc.robot.subsystems.*;
public class Robot2 extends TimedRobot {
private static final int leadDeviceID = 1;
private static final int followDeviceID = 2;
private static final int kJoystickPort = 0;
private CANSparkMax m_leadMotor;
private CANSparkMax m_followMotor;
private Joystick m_joystick;
#Override
public void robotInit() {
m_leadMotor = new CANSparkMax(leadDeviceID, MotorType.kBrushless);
m_followMotor = new CANSparkMax(followDeviceID, MotorType.kBrushless);
m_leadMotor.restoreFactoryDefaults();
m_followMotor.restoreFactoryDefaults();
}
}
This is also using the WPI libraries and FRC. This is also using all of the imported libraries, as it is shown below.
You are importing the wrong MotorType enum:
the edu.wpi.first.wpilibj.drive.RobotDriveBase.MotorType doesn't contain a kBrushless constant.
it seems that you are using CANSparkMax (because you write m_leadMotor = new CANSparkMax(leadDeviceID, MotorType.kBrushless);)
the [CANSparkMax constructor](https://codedocs.revrobotics.com/java/com/revrobotics/cansparkmax#%3Cinit%3E(int,com.revrobotics.CANSparkMaxLowLevel.MotorType) takes as second parameter an instance of CANSparkMaxLowLevel.MotorType, which has a constant kBrushless
Therefore it seems that you should replace
import edu.wpi.first.wpilibj.drive.RobotDriveBase.MotorType;
with
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
and probably also add
import com.revrobotics.CANSparkMax;

Android create Toast using class reflection?

I am creating a hook using AndHook to test some functions getting called. I need to show a Toast inside a method without being able to get the context object (I can't directly import MainActivity because I am injecting the script without having the corresponding package when compiling so I can't use MainActivity.this). Here's the code sample:
import andhook.lib.HookHelper;
import android.widget.Toast;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
public class AndHookConfig {
#HookHelper.Hook(clazz = BitmapFactory.class)
private static Bitmap decodeFile(BitmapFactory thiz, String path) {
Toast.makeText(Class.forName("com.example.activity.MainActivity").this, "Bitmap.decodeFile("+path+")", Toast.LENGTH_LONG).show();
return (Bitmap)(HookHelper.invokeObjectOrigin(thiz, path));
}
}
I think the only way to do this is using reflection but the code sample doesn't compile and results in an error. Do you have any solutions ?
Thanks in advance.

Have a problem with packages not getting recognized [duplicate]

This question already has answers here:
The declared package does not match the expected package ""
(25 answers)
Closed 3 years ago.
I am trying to create a puzzle solving algorithm with an A* search. everything should be right but I got a problem when I try to use one of the new "MoveUp MoveDown MoveLeft MoveRight" classes that I implemented i get an error that (the declared package "rushhour" does not match the expected package)
MoveDown
package rushhour;
import search.Action;
import search.State;
public class MoveDown implements Action{
int carNum;
int nPositions;
public MoveDown(int carNum, int nPositions){
this.carNum = carNum;
this.nPositions = nPositions;
}
public int getCost() {
return 1;
}
public String toString(){
return "move down";
}
}
then the class I use it in looks like this
package rushhour;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.lang.model.element.Element;
import search.Action;
import search.State;
public class GameState implements search.State {
boolean[][] occupiedPositions;
List<Car> cars; // target car is always the first one
int nrRows;
int nrCols;
...
...
...
public boolean isLegal(Action action) {
if(action instanceof MoveDown){
Car car = cars.get(((MoveDown) action).carNum);
int nextPos = car.getRow() + car.getLength() + ((MoveDown) action).nPositions;
Is is expected that your folder hierarchy matches your package hierarchy, (and vice-versa).
If you classpath is /src/main/java/ your files should look like this:
/src/main/java/rushour/GameState.java
/src/main/java/rushour/MoveDown.java
/src/main/java/search/Action.java
/src/main/java/search/State.java

How can I get extra data by Intent using Android annotation?

I'm really new to Android. I'm trying to pass the data to the text view of another class. But for some reason, it does not appear in the second class / page (the textview didnt change). I was able to do it when I was not using android annotation, but I was told that using annotations is a lot easier so I'm trying to convert it to that but for some reason it isn't working? I might be missing something.
Main 1
package com.example.faculty.labactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
#EActivity(R.layout.world)
public class MainActivity extends AppCompatActivity {
#ViewById(R.id.userName)
EditText user;
#Click(R.id.signIn)
public void signInButton(){
Main2Activity_.intent().name(user.getText().toString()).start();
}
Main 2
package com.example.faculty.labactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
#EActivity(R.layout.welcome)
public class Main2Activity extends AppCompatActivity {
#Extra("name")
String n;
#ViewById(R.id.name)
TextView tv;
Intent in2 = getIntent();
#AfterViews
void home(){
tv.setText("Hello, " + n + " !");
}
}
In your receiving Activity (for example MyActivity) you must declare the name of the object you want to receive like that
#Extra("name")
String name;
To pass data, when you create your intent you must do something like this:
Main2Activity_.intent(this).name(user.getText().toString()).start();
If you need a more precise solution edit your question to show more of your code.
You can look at the official doc for a more complete example
It might have to do with the dependencies you should add for the annotations to your project_root/build.gradle and app/build.gradle files.
Make sure to check this thread: what is Android Annotations and why we use Android Annotations?

Error 'ImageGenerator' cannot be resolve

I am trying the following code but getting:
ImageGenerator cannot be resolved
I have imported the required libraries of JavaCV and OpenCV. Do I need to import more libraries? I have searched, but could not find it!
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.nio.ByteBuffer;
import javax.naming.Context;
enum Resolution {
NORMAL, HIGH
}
public class KinectCapture
{
private BufferedImage image = null;
private int imWidth, imHeight;
private int fps;
private boolean isReleased = true;
// when Kinect context has been released
// OpenNI
private Context context;
private ImageGenerator imageGen; **Here I am getting the error.**
public KinectCapture()
{ this(Resolution.NORMAL); }
public KinectCapture(Resolution res)
{ configOpenNI(res); }
private void configOpenNI(Resolution res)
// create context and image generator
{
try {
context = new Context();
// add the NITE Licence
License licence = new License("PrimeSense", "0KOIk2JeIBYClPWVnMoRKn5cdY4="); // vendor, key
context.addLicense(licence);
imageGen = ImageGenerator.create(context);
I cant find ImageGenerator in the JavaDoc so im assuming you either have a package given to you by your school or whatever, then you have to import that as well, or you need to create the Class ImageGenerator somewhere in your package.
EDIT: After some research, I found this: https://github.com/OpenNI/OpenNI/blob/master/Wrappers/OpenNI.java/src/org/openni/ImageGenerator.java

Categories