I wonder what is the reason of this error happen...
I was developing a CRUD and for some reason burst it. Many codes and take you to analyze, is there anything I can do to know where this error is being triggered
MainActivity.java
package br.com.crud.Activitys;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import br.com.crud.R;
public class MainActivity extends Activity
{
Button btnView;
Button btnCadastro;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCadastro = (Button) findViewById(R.id.btnCadastro);
btnView = (Button) findViewById(R.id.btnView);
btnView.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
}
});
btnCadastro.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), NovoProdutoActivity.class);
startActivityForResult(i, 1);
}
});
}
}
NovoProdutoActivity.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.crud.Activitys;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import br.com.crud.R;
/**
*
* #author muky
*/
public class NovoProdutoActivity extends Activity{
Button btnSalvar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.novo_produto);
btnSalvar = (Button) findViewById(R.id.btnSalvar);
btnSalvar.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Produto Salvo com Sucesso", 6).show();
}
});
}
}
LogCat
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.crud"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" >
<activity android:name="MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- View Cadastro de Produto -->
<activity
android:name="NovoProdutoActivity"
android:label="NovoProduto"/>
<!-- View Listar Produtos -->
<activity
android:name="ListaProdutoActivity"
android:label="ListarProdutos"/>
</application>
</manifest>
Your main activity in code is br.com.crud.Activitys.MainActivity while the manifest says it is br.com.crud.MainActivity. Make sure the full class names agree in manifest and in code.
Related
I am trying to make an app which shows a user selected image which can be set by double-clicking on the button. The app does ask me for an image (which is good) but I don't know how to set it on ImageView if this onActivityResult is not getting called.
MainActivity:
package com.forever.bobby.cheater20;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.tomerrosenfeld.customanalogclockview.CustomAnalogClock;
public class MainActivity extends AppCompatActivity {
private static final int PICK_IMAGE = 100;
Button hiddenOnClickListener;
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
CustomAnalogClock customAnalogClock = (CustomAnalogClock) findViewById(R.id.analog_clock);
customAnalogClock.setAutoUpdate(true);
hiddenOnClickListener = (Button) findViewById(R.id.touchButtonHidden);
hiddenOnClickListener.setOnClickListener(new DoubleClickListener() {
#Override
public void onSingleClick(View v) {
Log.e ("mPressesOnce -_-","..");
}
#Override
public void onDoubleClick(View v) {
Log.e ("mPressesTwice -_-","..");
openGallery();
}
});
}
private void openGallery () {
Intent gallery = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, 0);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode,data);
Log.e ("SHould Be Steeted","");
if (requestCode == 0) {
Uri imgUri;
imgUri = data.getData();
// img.setImageURI(imgUri);
}
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.forever.bobby.cheater20">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Any Kind of help is appreciated as I am a newbie
With android sdk >= 23, some permissions e.g. WRITE_EXTERNAL_STORE, READ_EXTERNAL_STORE, etc. you must request it in java code.
Therefore, when user starts using feature need permission, android will show a dialog and user can grant or cancel a permission. You can read more about it on developer.android.com.
Followed a tutorial but doesnt seem to work on emulation.
MainActivity.java:
package b.myfirstapp;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#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.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);
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity2.class);
startActivity(intent);
}
});
}
}
Activity2.java:
package b.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Activity2 extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="b.myfirstapp" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".Activity2" >
</activity>
</application>
</manifest>
There are no errors in the code, its just that when i emulate it it doesnt work, maybe ive coded it incorrectly, not too sure, it looks fine to me.
I'm new to android dev so im confused. Help!
UPDATED: (For clearance purposes with comments properly inserted)
I think you are missing the call to addListenerOnButton in your MainActivity class, for example inside onCreate right after setContentView....
Also, you should make it private since it's only going to be called from within this class.
You have not called your addListenerOnButton method so you have not set onClick handler to your button.
Your onCreate should look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton()
}
I have two applications with one activity in each of them. Application A and application B. Application A has its activity's launch type as SingleTask. Both Activities have buttons calling each other. What I want is that when I launch activity A from activity B's button which is in turn launched from activity A's button, and then when I press back button activity B should come to foreground. But instead it is going back to launcher. I have attached code for reference.
Application A MainActivity:-
package com.example.appa;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent();
i.setClassName("com.example.appb", "com.example.appb.MainActivity");
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Application A Manifest:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Application B MainActivity:
package com.example.appb;
import com.example.appb.R;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent();
i.setClassName("com.example.appa", "com.example.appa.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
});
}
Application B Manifest is unmodified default mainfest
As it can be seen I have already tried FLAG_ACTIVITY_REORDER_TO_FRONT but it did not help.
P.S :- I cannot change launch type of activity in application A
Thanks in advance
The singleTask launch mode is not compatible with that behavior. You can set a different launch mode each time you start an activity by setting flags, but a better approach might be to reevaluate the choice made for default launch mode.
Tasks and back stack article.
While several people have asked questions about casting fragments to activities (and how that doesn't work), I couldn't find a fix that worked for me. I'm going through this tutorial, and have a main activity that contains two fragments. I'm probably missing something fairly obvious:
There error I get: Cannot cast from Fragment to FragmentListView (line18)
MainActivity:
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends Activity implements FragmentListView.OnSiteSelectedListener{
Fragmentwebview web;
FragmentListView list;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
list = (FragmentListView) manager.findFragmentById(R.id.fragment1);
list.setRefrence(this);
}
#Override
public void onSiteSelected(int i) {
// TODO Auto-generated method stub
web = (Fragmentwebview) manager.findFragmentById(R.id.fragment2);
// Check for landscape mode
if (web!= null && web.isVisible())
{
web.setNewPage(i);
}
else
{
Intent intent = new Intent(this , FragmentSupport.class);
intent.putExtra("index", i);
startActivity(intent);
}
}
}
Manifest:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.numeraid.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.numeraid.FragmentSupport"
android:label="#string/title_activity_fragment_support" >
</activity>
<activity
android:name="com.example.numeraid.FragmentListView"
android:label="#string/title_activity_fragment_list_view" >
</activity>
<activity
android:name="com.example.numeraid.Fragmentwebview"
android:label="#string/title_activity_fragmentwebview" >
</activity>
</application>
</manifest>
The first fragment that's causing the error
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:name="com.example.NumerAid.list"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
EDIT: Adding FragmentListView class
package com.example.numeraid;
import android.R.string;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentListView extends Fragment implements AdapterView.OnItemClickListener
{
ListView list;
String [] websites = {"Google","Facebook","Twitter","Xda-developer"};
OnSiteSelectedListener SiteListener;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentlistview, container, false);
list = (ListView) v.findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<string>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1));
list.setOnItemClickListener(this);
return v;
}
#Override
public void onItemClick(AdapterView adapterView, View view, int position, long l)
{
SiteListener.onSiteSelected(position);
}
// Container Activity must implement this interface
public interface OnSiteSelectedListener {
public void onSiteSelected(int i);
}
public void setRefrence(OnSiteSelectedListener siteListener)
{
this.SiteListener = siteListener;
}
}
Thanks to #corsair for this answer, originally posted as a comment:
My guess is that your FragmentListView class extends android.support.v4.app.Fragment
instead of android.app.Fragment. If you want to use the support version, then you should
use getSupportFragmentManager() instead of getFragmentManager(). Otherwise, you need to
extend android.app.Fragment everywhere as done in the tutorial you are following.
Can someone tell me whats wrong?? lost
Thanks,Mike
P.S.
I figured this might work instead of the splash for 1st page.
page1 or activity 1
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class test1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),test2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
page2 or activity 2
package com.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class test2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.Button02);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}) ;
}
}
Manifiest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".test1"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"></activity>
</application>
</manifest>
What is really wrong is you posting a question on StackOverflow, asking "Can someone tell me whats wrong??", without even taking the time to describe what symptoms you are experiencing. You are lucky that I am in a pretty good mood right now.
There are at least two problems:
Your manifest refers to an Activity2 that does not appear to exist
Your activity test2 is not defined in the manifest
I am guessing you just created your manifest incorrectly, seems that you have test2 as the class but tell the manifest it's name is Activity2.