I am trying to learn the principles of starting a new activity in android. I am using "Android studio". I have created a MainActivity class which contains a text which tells the user to push the button, and the button. When the button is pushed the new activity is supposed to start(aktivitet2Klasse). This activity contains two textviews and a button. When the first button is pressed, the tetview should display "works fine!". When the second button is pushed it is supposed to display "Finished!". The point with the app is just to start one activity from another. However after pushing the button on the first screen (first activity) the app crashes. I have studies a lot of the same issues on these sides, but I cannot figure what is wrong.
Here is the code:
The main activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView instruction;
private Button startButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instruction = (TextView) findViewById(R.id.InstruksjonsView);
startButton = (Button) findViewById(R.id.btn1);
startButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this,aktivitet2Klasse.class);
startActivity(intent);
}
});
}
#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);
}
}
The next activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
#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);
}
}
you forgot to set the content view in the second activity
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//YOU NEED TO SET THE CONTENTVIEW
// like setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
Related
There are two imageviews on AnchorPane in scenebuilder .one imageview1 has image
and now trying to drag and drop on imageview2.
public class FXMLDocumentController implements Initializable {
#FXML
private ImageView imageview1;
#FXML
private ImageView imageview2;
#Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///C:imagedragndropex6/IMG_4093.JPG"));
}
#FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
// ImageView im=(ImageView)event.getSource();
Image img=imageview1.getImage();
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY);
// db.setDragView(imageview1.getImage());
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
}
#FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
#FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
output:
DragDetectedEntered
true
true
DragDetectedExited
DragOverEntered
false
DragOverExited
Plz let me know what is wrong with this trying for hours...
public class FXMLDocumentController implements Initializable {
#FXML
private ImageView imageview1;
#FXML
private AnchorPane anchorpane;
#Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///D:/Downloads/Earthpic.jpg"));
// TODO
}
#FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY_OR_MOVE);
Image img=imageview1.getImage();
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
event.consume();
}
#FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
System.out.println(event.getGestureSource());
System.out.println(event.getGestureTarget());
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
#FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
ImageView imageview2=new ImageView();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setLayoutX(400);
imageview2.setLayoutY(150);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
anchorpane.getChildren().add(imageview2);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
Finally,this works fine...handleDragOver and handleDragDropped has to be set to anchorpane.
I have an imageView in a fragment. On imageView click I want to start an activity. Below is my code showing null pointer exception.
public static class DemoObjectFragment extends Fragment {
// public static final String ARG_OBJECT = "object";
Context con=getActivity();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
ImageView shadow_img=(ImageView) rootView.findViewById(R.id.shadow_img);
ImageView img=(ImageView)rootView.findViewById(R.id.img);
img.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), Article.class);
getActivity().startActivity(intent);
}
});
shadow_img.setImageResource(R.drawable.featured_shadow);
new ImageLoadTask(args.getString("img_url"),(ImageView)rootView.findViewById(R.id.img),con).execute(null, null);
return rootView;
}
}
This code run fine, try litlle changes.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = null;
rootView = (ViewGroup) inflater.inflate(R.layout.layname, container, false);
Button btnSec = (Button)rootView.findViewById(R.id.btnSec);
btnSec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent iz = new Intent(getActivity(), SecondActivity.class);
startActivity(iz)
I have a clickhandler for a button. I want the screen scrolls to an anchor like
Anchor a = new Anchor();
a.setName("stopHere");
(...)
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// Do somethings
(...)
// Scroll to Anchor "stopHere"
?????
}
});
How can I do it?
This is similar when using an anchor click to go to stopHere point , but I wan do it next the button is clicked.
Thanks.
The method Element#scrollIntoView() should do what you want:
final Anchor a = new Anchor();
a.setName("stopHere");
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
a.getElement().scrollIntoView();
}
}
Or use Window#scrollTo(int, int)
final Anchor a = new Anchor();
a.setName("stopHere");
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
Window.scrollTo(a.getAbsoluteLeft(), a.getAbsoluteTop());
}
}
I am programatically showing the soft keyboard when a layout is loaded on a button click.
I am showing the softkeyboard only when the textfield has focus. It works fine. But when I call the same method at another place in the code(not a button click) then the soft keyboard does not show up. Below is my code. Please point out where I went wrong.
public void showNewView() {
setContentView(R.layout.activity_main);
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
isRegisterScreen = true;
final EditText text1 = (EditText) findViewById(R.id.label1);
final EditText text2 = (EditText) findViewById(R.id.label2);
final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(labelText, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(text1.getWindowToken(), 0);
}
}
});
text2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(text2, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(phoneText.getWindowToken(), 0);
}
}
});
text1.requestFocus();
}
I have a main activity (FragmentDemoActivity) which creates 2 tabs. Fragment A and Fragment B.
I have another class called CategoryList which loads a customlist from an sqlite database.
I am using this example which works great for the TAB parts.
I want to load the CategoryList into the Fragment A.
My CategoryList class is as follows
// All the imports
public class CategoryList extends BaseActivity{
ImageView imgFeedback;
ImageView imgAbout;
ListView listCategories;
ProgressBar prgLoading;
TextView txtAlert;
AdView ads;
static String[] CategoryName ;
ListAdapter la;
static int[] id;
List<String> catList;
static DBHelper dbhelper;
class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
#SuppressWarnings("unused")
private Context ctx;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
ctx = context;
}
#Override
public int getCount() {
return CategoryName.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.imgPreview = (ImageView) convertView.findViewById(R.id.imgPreview);
holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.imgPreview.setImageResource(getResources().
getIdentifier(CategoryName[position], "drawable", getPackageName()));
String categoryName = CategoryName[position];
categoryName = categoryName.toLowerCase();
categoryName = Character.toString(categoryName.charAt(0)).toUpperCase()+categoryName.substring(1);
holder.txtRecipeName.setText(categoryName);
return convertView;
}
class ViewHolder {
ImageView imgPreview;
TextView txtRecipeName;
}
}
final Context context = this ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
AppRater.displayAsk2Rate(this, 1, 1, false);
getSupportActionBar().setHomeButtonEnabled(true);
dbhelper = new DBHelper(this);
try {
dbhelper.createDataBase();
}catch(IOException ioe){
throw new Error("Unable to create database");
}
/** then, the database will be open to use */
try{
dbhelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
CategoryName = dbhelper.getAllCategories();
dbhelper.close();
catList = Arrays.asList(CategoryName);
/* imgAbout = (ImageView) findViewById(R.id.imgAbout);
imgAbout.setOnClickListener(new ButtonListner(CategoryList.this));
imgFeedback = (ImageView) findViewById(R.id.imgFeedback);
imgFeedback.setOnClickListener(new ButtonListner(CategoryList.this));*/
listCategories = (ListView) findViewById(R.id.listCategories);
la = new ListAdapter(CategoryList.this);
listCategories.setAdapter(la);
txtAlert = (TextView) findViewById(R.id.txtAlert);
ads = (AdView) findViewById(R.id.ads);
Ads.loadAds(ads);
listCategories.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
String category = catList.get(position);
Intent i = new Intent(CategoryList.this, RecipesList.class);
i.putExtra("Category_Name", category);
startActivity(i);
}
});
}
}
My FragmentA is as follows
public class FragmentA extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved)
{
View view = inflater.inflate(R.layout.categories_list, container, false);
ListView listCategories = (ListView) getActivity().findViewById(R.id.listCategories);
ListAdapter lav = new ListAdapter(CategoryList.this);
listCategories.setAdapter(lav);
return view;
}
#Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
Any pointers ?