I am trying to use this cool animation called SmallBang
on my Image button. Here is how my code looks for now:
import xyz.hanks.library.SmallBang;
public class MainActivity extends AppCompatActivity {
private SmallBang smallBang;
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smallBang = SmallBang.attach2Window(this);
smallBang.bang((ImageButton) findViewById(R.id.imageButton));
}
public void onSettingsButtonClick(View view) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
finish();
}
}
I want to use that animation and then change to another activity. When i run this code I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
If i remove the smallbang animation, the button and activity change works just fine. What could I do to get the animation working? I've added the library to the Gradle file.
Working Code
public class MainActivity extends Activity {
private SmallBang mSmallBang;
private ImageView ImageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSmallBang = SmallBang.attach2Window(this);
ImageButton = (ImageView) findViewById(R.id.imageButton);
ImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSmallBang.bang(v);
mSmallBang.setmListener(new SmallBangListener() {
#Override
public void onAnimationStart() {
}
#Override
public void onAnimationEnd() {
}
});
}
});
}
}
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/image_1">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
Update your code modification:
public class MainActivity extends Activity {
private SmallBang mSmallBang;
private ImageView ImageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSmallBang = SmallBang.attach2Window(this);
ImageButton = (ImageView) findViewById(R.id.imageButton);
}
public void onSettingsButtonClick(View view) {
mSmallBang.bang(view);
mSmallBang.setmListener(new SmallBangListener() {
#Override
public void onAnimationStart() {
}
#Override
public void onAnimationEnd() {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
finish();
}
});
}
}
Try in this way
import xyz.hanks.library.SmallBang;
public class MainActivity extends AppCompatActivity {
private SmallBang smallBang;
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smallBang = SmallBang.attach2Window(this);
imageButton = (ImageButton) findViewById(R.id.imageButton);
smallBang.bang(view,new SmallBangListener() {
#Override
public void onAnimationStart() {
}
#Override
public void onAnimationEnd() {
onSettingsButtonClick(imageButton);
}
});
}
public void onSettingsButtonClick(View view) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
finish();
}
}
Related
I'm trying to learn basic stuff in android studio with java. I need to create 1 activity where we need a Button and a text to input. When the button is clicked to check is the input value is between 3 words "Primary, Secondary , Higher). Here is the mainactivity code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private int check = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// Check for valid education
// if yes -> next view
if (check == 0 ) {
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
} else if ( check == 1) {
startActivity(new Intent(this, Activity2.class));
}
}
}
Follow this way
add input field in you xml file above button view .
<EditText
android:id="#+id/edu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter your education" />
In your mainactivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private EditText edu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edu = findViewById(R.id.edu);
button = findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v){
String check_edu= edu.getText().toString().toLowerCase();
if ( check_edu.equals("primary") || check_edu.equals("secondary") || check_edu.equals("higher") ){
startActivity(new Intent(this, Activity2.class));
} else {
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
}
});
}
}
I am trying to use progressbar like a 30 secs timer. It works well but when i click back button the progressbar progress continues. I am unable to reset the progressbar progress to 0 when i click the backbutton.
Here is my code:
MainAcitivty:
public class MainActivity extends AppCompatActivity {
public Button btnA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnA = (Button)findViewById(R.id.btnA);
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentA = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intentA);
}
});
}
}
SecondActivity:
public class SecondActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Context context;
float from;
float to;
final TextView textV;
final ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
final int[] i = {0};
textV = (TextView)findViewById(R.id.tvFinal);
mProgressBar = (ProgressBar)findViewById(R.id.progressbar);
mProgressBar.setProgress(i[0]);
mCountDownTimer=new CountDownTimer(30000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress"+ i[0] + millisUntilFinished);
i[0]++;
mProgressBar.setProgress((int) i[0] *100/(30000/1000));
}
#Override
public void onFinish() {
i[0]++;
mProgressBar.setProgress(100);
textV.setText("finish");
Intent intent = new Intent(SecondActivity.this, SecondActivity.class);
startActivity(intent);
}
};
mCountDownTimer.start();
}
}
in order to implement behaviour of back button you need to override onBackPressed() and then implement method to reset progressbar result.
Example:
#Override
public void onBackPressed() {
super.onBackPressed();
progressBar.setProgress(0);
}
I have an interface implemented in MainActivity, and inside that callback method i want to update my TextView but i am getting nullpointer exception.
This is my MainActivity class
public class MainActivity extends AppCompatActivity implements GenericCallback
{
Context mcontext;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext = this;
Button btn = findViewById(R.id.btn);
tv = findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
}
#Override
public void doSomething(Context context, String... a)
{
Toast.makeText(context,"Calback"+a[0]+a[1],Toast.LENGTH_SHORT).show();
tv = findViewById(R.id.tv);//Line 43
tv.setText(a[0]+a[1]);
}
My interface looks like this
public interface GenericCallback
{
void doSomething(Context context, String... a);
}
My SecomdActivity
public class SecondActivity extends AppCompatActivity
{
Context context;
GenericCallback genericCallback = new MainActivity();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
context = this;
String a="Secomd",b = "Activity";
genericCallback.doSomething(context,a,b);
finish();
}
}
StackTrace
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.view.Window$Callback
android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:56)
at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at android.com.callback.MainActivity.doSomething(MainActivity.java:43)
at android.com.callback.SecondActivity.onCreate(SecondActivity.java:21)
I already know what is nullpointer, I already referred this and this
You can use EventBus Library
For installation
compile 'org.greenrobot:eventbus:3.1.1'
First, create a Java class
public class MsgEvent {
String oo;
public String getOo() {
return oo;
}
public void setOo(String oo) {
this.oo = oo;
}
}
Then in SecondActivity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
context = this;
String a="Secomd",b = "Activity";
MsgEvent msgEvent = new MsgEvnet();
msgEvent.setOo(a);
EventBus.getDefault().post(msgEvent);
finish();
}
Then in MainActivity
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MsgEvent event) {
String value = event.getOo();
}
You can use BroadcastReceiver
check the below example
MainActivity
public class MainActivity extends AppCompatActivity {
TextView mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = findViewById(R.id.toolbar_title);
startActivity(new Intent(this, SecondActivity.class));
}
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
mTitle.setText(intent.getStringExtra("DATA"));
Toast.makeText(context, "recieved text : " + intent.getStringExtra("DATA"), Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction("MY_ACTION");
registerReceiver(myBroadcastReceiver, filter);
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
}
XML form MainActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbarIcon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Nilesh"
android:textStyle="bold" />
</LinearLayout>
Second_activity
public class SecondActivity extends AppCompatActivity {
TextView tvText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getSupportActionBar().setTitle("SECOND Activity");
tvText = findViewById(R.id.tvText);
tvText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendBroadcast();
}
});
}
public void sendBroadcast() {
Intent broadcast = new Intent();
broadcast.putExtra("DATA", "MY NAME IS NILESH");
broadcast.setAction("MY_ACTION");
sendBroadcast(broadcast);
}
}
LAYOUT for second activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:layout_width="match_parent"
android:id="#+id/tvText"
android:text="Click to Send Broadcas"
android:layout_height="wrap_content" />
</LinearLayout>
The reason you are getting that null exception is that your MainActivity view is not inflated because it is not instantiated by the framework.
You can create an instance getter method from your first activity.
private static GenericCallback instance;
public static GenericCallback getHandler() {
return instance;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
instance = this;
...
}
And then on your 2nd activity, get that instance instead of instantiating an activity.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
context = this;
String a="Secomd",b = "Activity";
GenericCallback genericCallback = MainActivity.getHandler();
genericCallback.doSomething(context,a,b);
finish();
}
i'm getting this error
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bassammetwally.like/com.example.bassammetwally.like.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
when i'm trying to switch activity in another method in the
mainActivity.class
The Code i'm trying to run(not going to include libraries);
public class MainActivity extends AppCompatActivity {
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button();
}
});
}
public void button()
{
startActivity(i);
}
}
code before that worked
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton)findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener(){
public void onClick( View v ){
startActivity(i);
}
});
}}
Questions:
What is the meaning of the error?
why is this error showing?
Try this code:
final Intent i ;
ImageButton ButtonOne ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i =new Intent(MainActivity.this, profile.class);
ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button();
}
});
}
public void button()
{
startActivity(i);
}
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, profile.class);
startActivity(i);
}
});
}
}
Also Added Profile.java in manifest file
You're initiating variable i twice. When going to function button() you are using the one in public scope (above the onCreate method), not the one from onCreate. Your code should look like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent i = new Intent(this, profile.class);
ImageButton ButtonOne = (ImageButton) findViewById(R.id.profile);
ButtonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button(i);
}
});
}
public void button(Intent i)
{
startActivity(i);
}
}
Being new to Android Application development i was trying to learn connecting two activities using Intent. I tried a code from a book. It keeps throwing an error saying - 'onCreate(Bundle)' is already defined in MainActivity class as well as the NewActivity class. Would be of great help if i could get a solution.
MainActivity.class
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}
NewActivity.class
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
}
If you want to connect those activities you have to do this :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}
And then in yout second activity just delete the:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
And copy this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
And it will work.
Just change your NewActivity to:
public class NewActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
}
A class can contain only one onCreate() method.First learn about Activity Life Cycle http://developer.android.com/training/basics/activity-lifecycle/starting.html
Just remove the first onCreate event on your main activity and new activity. you don't need twice
public class MainActivity extends Activity {
#Override
/*protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});
}