java.lang.runtimeexception unable to start activity componentinfo - java

i am having this error in this code
java.lang.runtimeexception unable to start activity componentinfo
package estimatewall.example.com.estimatewall;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText inputTxt;
EditText inputTxt1;
TextView setText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputTxt = (EditText) findViewById(R.id.editText);
// Store EditText in Variable
int val = Integer.parseInt(inputTxt.getText().toString());
inputTxt1 = (EditText) findViewById(R.id.editText2);
// Store EditText in Variable
int val1 = Integer.parseInt(inputTxt1.getText().toString());
float block1,block2;
final float sum;
block1=(val*12)/7;
block2=(val1*12)/10;
sum=block1*block2;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try {
TextView textView = (TextView) findViewById(R.id.textView3);
textView.setText("" + sum);
}catch (Exception e)
{
e.printStackTrace();
}
}
});
}
#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);
}
}

Probably getting NumberFormatException because here:
int val = Integer.parseInt(inputTxt.getText().toString());
trying to convert null to int.
Use inputTxt.getText() inside onClick method of Button on click listener to get values input by user in Edittext or set some default values in xml for both EditText's as android:text="0"

Related

Encountered the error Attempt to invoke virtual method 'RecyclerView.setAdapter(RecyclerView$Adapter)' on a null object reference in Android Studio

I encounter the problem " Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference" when I run my code. I've searched for similar solutions but it doesn't seem to fix my error.
How should I revise my code?
This is my MainActivity.java code:
package endlesstaste;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity {
private final LinkedList<String> mImagePathList = new LinkedList<>();
private String mDrawableFilePath =
"android.resource://endlesstaste/drawable/";
private RecyclerView mRecyclerView;
ImageListAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
for (int i = 1; i <= 5; i++) {
mImagePathList.addLast(mDrawableFilePath + "image" + i);
}
// Get a handle to the RecyclerView.
mRecyclerView = findViewById(R.id.recyclerview);
// Create an adapter and supply the data to be displayed.
mAdapter = new ImageListAdapter(this, mImagePathList);
// Connect the adapter with the RecyclerView.
mRecyclerView.setAdapter(mAdapter);
// Give the RecyclerView a default layout manager.
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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);
}
}
This is my ImageListAdapter.java code:
package endlesstaste;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity {
private final LinkedList<String> mImagePathList = new LinkedList<>();
private String mDrawableFilePath =
"android.resource://endlesstaste/drawable/";
private RecyclerView mRecyclerView;
ImageListAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
for (int i = 1; i <= 5; i++) {
mImagePathList.addLast(mDrawableFilePath + "image" + i);
}
// Get a handle to the RecyclerView.
mRecyclerView = findViewById(R.id.recyclerview);
// Create an adapter and supply the data to be displayed.
mAdapter = new ImageListAdapter(this, mImagePathList);
// Connect the adapter with the RecyclerView.
mRecyclerView.setAdapter(mAdapter);
// Give the RecyclerView a default layout manager.
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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);
}
}
You cannot call
mRecyclerView = findViewById(R.id.recyclerview);
before calling
setContentView(R.layout.activity_main);
When you do, the call to findViewById returns null. Move setContentView (and super.onCreate while you're at it) to the start of onCreate before you start getting views.

Why doesn't my "SharedPreferences" work?

The Problem is, that the Text field which is in "Wizard2Activity" just shows "nothing" and not what the user has typed in the "EditText" in "Wizard1".
Wizard1.java:
package com.CENSORED.CENSORED;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Wizard1 extends Activity {
public static final String PREFS ="examplePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wizard1);
final EditText et = (EditText)findViewById(R.id.editText1);
Button nextAct = (Button)findViewById(R.id.button1);
nextAct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
String message = et.getText().toString();
SharedPreferences examplePrefs = getSharedPreferences(PREFS, 0);
Editor editor = examplePrefs.edit();
editor.putString("usermessage", message);
editor.commit();
Intent i = new Intent(getApplicationContext(),
Wizard2Activity.class);
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.wizard1, 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);
}
}
And Wizard 2 Activity:
package com.CENSORED.CENSORED;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Wizard2Activity extends Activity {
public static final String PREFS ="examplePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wizard2);
TextView tv = (TextView)findViewById(R.id.textView1);
SharedPreferences example = getSharedPreferences(PREFS, 0);
String userString = example.getString("userMessage", "nothing");
tv.setText(userString);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.wizard2, 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);
}
}
Thank you for your help! :)
You should change
String userString = example.getString("userMessage", "nothing");
to
String userString = example.getString("usermessage", "nothing");
key is usermessage. It's just typo.
Because the method
editor.putString("usermessage", message);
and
example.getString("userMessage", "nothing");
have different key.

Class won't execute properly inside main activity? [duplicate]

This question already has answers here:
How to use java classes within one activity?
(4 answers)
Closed 7 years ago.
How can you use a java class within one activity, by that I mean is having different components of that activity spread out in a bunch of java classes. I'm a little new to android and this is what I have tried so far:
MainActivity.java
package com.example.alex.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Something(this);
}
#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);
}
}
Something.java
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.app.Activity;
public class Something {
private Activity activity;
private Button add,subtract,multiply,devide;
private EditText editA, editB, editC;
private double doubleA,doubleB,doubleC;
public Something(Activity a){
activity=a;
click();
}
public void click(){
editA = (EditText) activity.findViewById(R.id.editText);
editB = (EditText) activity.findViewById(R.id.editText2);
editC = (EditText) activity.findViewById(R.id.editText3);
doubleA =Double.parseDouble(editA.getText().toString());
doubleB =Double.parseDouble(editB.getText().toString());
add = (Button) activity.findViewById(R.id.add);
subtract = (Button) activity.findViewById(R.id.subtract);
multiply = (Button) activity.findViewById(R.id.multiply);
devide = (Button) activity.findViewById(R.id.devide);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA+doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA-doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA*doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
devide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doubleC = doubleA/doubleB;
String s = "" + doubleC;
editC.setText(s);
}
});
}
}
So I wasn't sure why my listeners weren't working on my buttons so I tried passing the activity to the class that has the listeners added to the buttons but that didn't work in fact now my application won't even start in the emulator. All I wanted to do was have "MainActivity" handle the "Gui" and have the "Something" class handle the listeners but no matter what I do I can't seem to make them communicate with one another to form one Activity.
Someone suggested to do a pass in main activity Something s = new Something(MainActivity.this); and call it from within main activity like this s.click(); but that didn't work.
in android every class that extends activity just have a one xml Layout to view and handle this XML file just in it's Activities,and you can make a lot of java classes that send alot of parameter but when you want to handle XML behavioral you should do in own class that Extends Activity,so you can do this:
public class MainActivity extends ActionBarActivity {
Button myBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Something(this);
myBtn=(Button)findViewById(R.id.Btn);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#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);
}

Cannot Resolve Symbol "OnClickListener"

I am getting the error Cannot Resolve Symbol "OnClickListener" and "Cannot Resolve Symbol "V""
I am starting to get into Java so I am not very good at this and not so familiar with the language
My code:
package com.emiliogaines.counter.counter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
#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);
}
}
I'm not sure why you did that but please move the initializing and listeners to onCreate():
Simply cut these lines of code:
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
and paste them in onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.More);
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mTextView.setText("Some Text");
}
});
}
And then go read the basis of coding and it's standards.
U must store this into onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
Button mButton = (Button) findViewById(R.id.More);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final TextView mTextView = (TextView)findViewById(R.id.Antal);
mTextView.setText("Some Text");
}
});
}

Android Studio App Unfortunately has stopped

I'm new to Android and I was practicing, when i finished this practice app i ran it but it says "unfortunately has stopped" where is the problem?My app looks like a simple log in screen and i don't think there's a problem with the XML file.
package com.mycompany.testbutton;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonLog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = String.valueOf(userName.getText());
String password = String.valueOf(passWord.getText());
String allTogetherToast = "Your Email is " + username + " and your Password is " + password;
Toast.makeText(MainActivity.this, allTogetherToast, Toast.LENGTH_LONG).show();
}
});
}
#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);
}
}
Initialize these
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
after your
setContentView(R.layout.activity_main)
so your code will be like this
package com.mycompany.testbutton;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button buttonLog;
EditText userName;
EditText passWord;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonLog = (Button)findViewById(R.id.button1);
userName = (EditText)findViewById(R.id.usernameText);
passWord = (EditText)findViewById(R.id.passwordText);
buttonLog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = String.valueOf(userName.getText());
String password = String.valueOf(passWord.getText());
String allTogetherToast = "Your Email is " + username + " and your Password is " + password;
Toast.makeText(MainActivity.this, allTogetherToast, Toast.LENGTH_LONG).show();
}
});
}
#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;
}
Move
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
after setContentView(....) inside onCreate(...)
Corrected:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
Views available only after setContentView(....)

Categories

Resources