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);
}
}
Related
SubActivity cannot INVISIBLE the button in MainActivity I tried to use intent, but I'm not sure.
it SubActivity java code
try to INSIBLE the resetB button in Sub Activity
public class SubActivity extends Activity {
Button resetH, resetH2;
Button resetB, miC;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
ImageButton back = (ImageButton) findViewById(R.id.back);
resetH = (Button) findViewById(R.id.resetH);
resetH2 = (Button) findViewById(R.id.resetH2);
resetB = findViewById(R.id.resetB);
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.VISIBLE);
}
});
resetH.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
resetB.setVisibility(View.INVISIBLE);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
I used intent, but I can't move the button
how can I use Junit to test a button click, everything happens in Android studio in java?
I am very bad at Junit testing
public class ActivityMenu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
Intent intent = new Intent(ActivityMenu.this, Level_Menu.class);
startActivity(intent);
finish();
}catch (Exception e){
}
}
});}
I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText textmsg;
static final int READ_BLOCK_SIZE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textmsg = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
noteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Notes.class);
}
});
resuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Results.class);
}
});
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
}
});
When I run the application the buttons don't work. If I set the buttons as so..
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button agenBtn = (Button) findViewById(R.id.agenBtn);
Button resuBtn= (Button) findViewById(R.id.resuBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
If I use this code above, the code works fine and the buttons work correctly. But other functionality with different classes/activities won't run. Could someone please show me a solution or explain how to solve this issue.
You should place:
this.noteBtn = (Button) findViewById(R.id.noteBtn);
this.notBtn.setOnClickListener(this);
this.resuBtn = (Button) findViewById(R.id.resuBtn);
this.resuBtn.setOnClickListener(this);
this.timeBtn = (Button) findViewById(R.id.timeBtn);
this.timeBtn.setOnClickListener(this);
in onCreate() instead of onClick(). Make the buttons belong to the class, so you can reference them in onClick(). You should not be setting new onClickListeners in onClick(), but should rather have a switch statement based on the clicked view's id to determine which button was pressed.
this code
#Override
public void onClick(View v) {
Button noteBtn = (Button) findViewById(R.id.noteBtn);
Button resuBtn = (Button) findViewById(R.id.resuBtn);
Button timeBtn = (Button) findViewById(R.id.timeBtn);
...
}
is not working because it's never going to get call, since none of your buttons is implementing it, worse yet they have not even been initialized because the onClick has not and will never be called.
the correct way to do it is like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but1 = (Button) findViewById(R.id.but1);
Button resBtn = (Button) findViewById(R.id.resBtn);
Button noteBtn = (Button) findViewById(R.id.noteBtn);
agendaBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View viewTimeTable) {
Intent intent = new Intent(MainActivity.this, Agenda.class);
startActivity(intent);
}
}); etc...
because you are first getting a reference to your buttons and assigning them the onClickListener to each one of it.
I will suggest reading more about the Android Activity life cycle you can find it
here
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);
}
});
}