Can't figure out why, but I have no errors on eclipse, (simple classroom app - button creates new activity, sending in a textView and an imageView to the new activity) but as soon as I run my app and I press my button, the app crashes and I recieve this error on the LogCat
Logcat
02-12 17:13:26.297: E/AndroidRuntime(398): Caused by: java.lang.NullPointerException
02-12 17:13:26.297: E/AndroidRuntime(398): at edu.colum.iam.SecondPage.onCreate(SecondPage.java:38)
When I take that line out (and any other line that would affect it) the app runs fine, and it sends my textView over. Here is the .java I have, and I'll post the xml just incase as well.
IntentsActivity.java
public class IntentsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)this.findViewById(R.id.button1);
btn.setOnClickListener(myListener);
}
// Create an anonymous implementation of OnClickListener
View.OnClickListener myListener = new View.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(IntentsActivity.this, SecondPage.class);
String text = ((TextView)findViewById(R.id.textView1)).getText().toString();
ImageView Selection = ((ImageView) findViewById(R.id.imageView1));
myIntent.putExtra("Text", text);
myIntent.putExtra("img", R.drawable.icon);
or an image)
startActivity(myIntent);
//endclass
SecondPage.java
public class SecondPage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//get extras
Intent intent = this.getIntent();
Bundle b = intent.getExtras();
String text = b.getString("Text");
//show text
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(text);
//show image
ImageView image = (ImageView) findViewById(R.id.imageView1);
int resource = getIntent().getIntExtra("img", R.drawable.icon);
image.setImageDrawable(getResources().getDrawable(resource));
// ^ this is the line my logcat crashes as an error
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:text="#string/Button1" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:id="#+id/textView1" android:text="#string/FirstLayout"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"></TextView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:id="#+id/textView1"
android:text="#string/SecondLayout"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"></TextView>
<Button android:text="#string/Button2" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
According to the layout you posted, imageView1 is in your first xml not in second.xml which is the one you inflate in SecondPage.java so naturally it is null when you try to call a method on it.
You will need to add the ImageView to your second.xml.
You can have the same ImageViews in different layout files (I don't know why you would name them the same) and then access them from within your code as long as you have set the right layout inside onCreate or wherever you want to access those components. So, yes, just copy the ImageView to the second.xml and it should work.
Related
I am new to java for android.
Then I attempt to put in textview anything, all views do not be shown.
Main.XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textNew"
android:text="Hello world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="75px"/>
<Button
android:id="#+id/buttonNew"
android:text="Click me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
int randomInt;
TextView textNew = findViewById(R.id.textNew);
SharedPreferences sp = getSharedPreferences("settings", Activity.MODE_PRIVATE);
Button clickButton = findViewById(R.id.buttonNew);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textNew.setText("5");
randomInt = Integer.parseInt(sp.getString("setting", "0"));
}
}
I tried to delete textNew.setText("text"). It is helped. But how I can set text!? Me need it!
You can't access the button and the Textview before setting the view content
SetContentView(View) sets the activity content to an explicit view.
In your case, you're setting the view to R.layout.main where the Button and the TextView are defined
To fix this move the onCreate and the setContentView before the rest of the code in the onCreate method
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}
I am doing the first experiment with Android and I have the following problem with this simple application.
Basically my application consist into an ImageView showing a background immage, a TextView showing a message and a button.
When the user click this button the text of my TextView have to change and the background immage of my *ImageView also have to change.
So this is my activiy_main.xml file containing the layout of my main activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B388FF"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/android_cookie_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
<TextView
android:id="#+id/status_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="I'm so hungry"
android:textColor="#android:color/white"
android:textSize="34sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="EAT COOKIE"
android:onClick="eatCookie"/>
</LinearLayout>
And this is the code of the previous activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void eatCookie(View v) {
TextView messaggio = (TextView) findViewById(R.id.status_text_view);
messaggio.setText("I'm so full");
ImageView sfondo = (ImageView) findViewById(R.id.android_cookie_image_view);
sfondo.setImageDrawable(Drawable.createFromPath("#drawable/after_cookie"));
}
}
As you can see, when the user click the button it is perform the eatCookie() method that first retrieve the TextView reference and change the text of this TextView. It works fine.
Then it retrieve the reference related to the ImageView and try to change the viewed immage, I have done it by this line:
sfondo.setImageDrawable(Drawable.createFromPath("#drawable/after_cookie"));
In my project I have put the after_cookie.jpg file into the /res/drawable/ folder.
The problem is that it can't work. The default immage of the android_cookie_image_view disappear but is not replaced with the after_cookie.jpg image.
What is wrong? What am I missing? How can I fix this issue?
Try this
sfondo.setImageDrawable(getResources().getDrawable(R.drawable.after_cookie));
Use this instead.
ImageView sfondo = (ImageView) findViewById(R.id.android_cookie_image_view);
sfondo.setImageDrawable(getResources().getDrawable(R.drawable.after_cookie));
And make sure you call that eatCookie function on onCreate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eatCookie();
}
I am trying to set the text of my EditText, but I am getting a null pointer exception. I have seen other posts answering this question, however I am extending ActionBarActivity so I can not simply solve this problem by using something like EditText f1 = (EditText) getActivity().findViewById(R.id.field1);
public class SwitchURL extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch_url);
Intent intent = getIntent();
getActionBar().setDisplayHomeAsUpEnabled(true);
EditText f1 = (EditText) findViewById(R.id.field1);
EditText f2 = (EditText)findViewById(R.id.field2);
SharedPreferences pref1 = getSharedPreferences("textField1", 0);
SharedPreferences pref2 = getSharedPreferences("textField1", 0);
Log.v("", pref1.getString("textField1", "DNE"));
Log.v("", pref2.getString("textField2", "DNE"));
f1.setText("hello", TextView.BufferType.EDITABLE);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
I know I am getting the NullPointerException because I am not accessing the right view that contains this editText. I have a method that is called when a button is clicked and it works fine.
public void changeUrl(View v) {
EditText f1 = (EditText)findViewById(R.id.field1);
EditText f2 = (EditText)findViewById(R.id.field2);
SharedPreferences pref1 = getSharedPreferences("textField1", 0);
SharedPreferences pref2 = getSharedPreferences("textField1", 0);
SharedPreferences.Editor edit1 = pref1.edit();
SharedPreferences.Editor edit2 = pref2.edit();
edit1.putString("textField1", f1.getText().toString());
edit2.putString("textField2", f2.getText().toString());
edit1.commit();
edit2.commit();
Log.v("", pref1.getString("textField1", "Fail"));
Log.v("", pref2.getString("textField2", "Fail"));
}
Any idea how I can access this view so that I can set the text of my editText? Here is the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android_native.SwitchURL$PlaceholderFragment"
android:background="#color/white"
android:orientation="vertical"
android:id="#+id/contain" >
<TextView
android:id="#+id/urlScreenTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change URL"
android:textColor="#color/black"
android:textSize="30sp"
android:layout_gravity="center"/>
<EditText
android:id="#+id/field1"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="http://stage1.onshift.com"
android:textColor="#color/black"
android:textSize="17sp"
android:background="#drawable/switch_url_fields_background"
android:inputType="text"
android:layout_gravity="center"
android:layout_marginTop="18dp"
android:paddingLeft="7dp"/>
<EditText
android:id="#+id/field2"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="http://stage.v2.onshift.com"
android:textColor="#color/black"
android:textSize="17sp"
android:background="#drawable/switch_url_fields_background"
android:inputType="text"
android:layout_gravity="center"
android:layout_marginTop="9dp"
android:paddingLeft="7dp"/>
<Button
android:id="#+id/switchUrlButton"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="Change URL"
android:textColor="#color/white"
android:textSize="22sp"
android:background="#drawable/switch_url_button"
android:inputType="text"
android:layout_gravity="center"
android:layout_marginTop="13dp"
android:gravity="center"
android:onClick="changeUrl"/>
</LinearLayout>
If the EditText is inside of a Fragment, then when your Activity is created the Fragment has not been created yet and has not had it's Views inflated yet. Attempting to access any of its Views at this time with findViewById() will return null.
Your code for accessing Views of the Fragment belongs in onCreateView or in onViewCreated of the Fragment class itself, not inside of onCreate of the Activity. You should also put all of your logic in the Fragment, so that you can better modularize your code.
This looks like you are trying to access views defined in the fragment's layout from within the activity. The activity doesn't know about the fragment layout at creation. Move your code which is getting the views and managing their content into the fragment's onCreatView().
Hi I'm new to Android Programming and I'm trying to make a simple program that changes the text by clicking a button. Here's my code:
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button_scan = (Button) findViewById(R.id.button_scan);
button_scan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonBeenPressed();
}
});
}
public void buttonBeenPressed(){
final Button button_scan = (Button) findViewById(R.id.button_scan);
TextView tv_barcode = (TextView)findViewById(R.id.textview_barcode);
if (tv_barcode != null){
tv_barcode.setText("been pressed.");
} else {
button_scan.setText("it's null dawg.");
}
}
}
And my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Scan" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/button_scan"></Button>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:id="#+id/textview_barcode"></TextView>
</LinearLayout>
However the TextView is returning NULL and i don't know why. Any suggestions? Thanks.
Hmmm i got no problems with your code, runs without any error! Are you working with eclipse? Then try to clean your project.
Looks all correct to me....
Your best bet to debug this may be to run your app on the emulator or a phone and run hierarchyviewer, find your TextView and check the id.
You can use textview or a button. Here is an example with a textview I've found from http://www.ahotbrew.com/android-textview-example/
<TextView
android:text="#string/textview_onclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview_onclick"
android:layout_below="#+id/textview_center"
android:textSize="25dp"
android:onClick="changeTextColor"
android:clickable="true"/>
In your MainActivity do this
public void changeTextColor (View view)
{
TextView textView = (TextView) view.findViewById(R.id.textview_onclick);
textView.setText("newWord");
}