SetText of EditText NullPointerException - java

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().

Related

When I set text to anything, all views is hidden

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);

Button not responding to onClick method

I am developing an Android app and I want to inflate a dialog with the press of a button (I've already done that), but one specific button does not work, and I don't really know why.
My code:
public class SearchUser extends AppCompatActivity{
private static final String TAG = "SearchUser";
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.btnSearchUser);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(SearchUser.this);
View mView = getLayoutInflater().inflate(R.layout.searchpopup, null);
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
});
}
});
dialog.show();
}
});
}
LAYOUT XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="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=".MainActivity">
<Button
android:id="#+id/btnUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/user"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="#+id/mapView"
mapbox:layout_constraintHorizontal_bias="0.0"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toTopOf="#+id/mapView"
mapbox:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/btnSearchUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/search_user"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="#+id/mapView"
mapbox:layout_constraintHorizontal_bias="0.0"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toTopOf="#+id/mapView"
mapbox:layout_constraintVertical_bias="0.129" />
<Button
android:id="#+id/logoutButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/log_out"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="#+id/mapView"
mapbox:layout_constraintHorizontal_bias="1.0"
mapbox:layout_constraintStart_toStartOf="#+id/mapView"
mapbox:layout_constraintTop_toTopOf="parent"
mapbox:layout_constraintVertical_bias="0.0" />
<com.mapbox.mapboxsdk.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toStartOf="parent"
mapbox:layout_constraintTop_toTopOf="parent"
mapbox:mapbox_cameraTargetLat="-34.6131500"
mapbox:mapbox_cameraTargetLng="-58.3772300"
mapbox:mapbox_cameraZoom="12"
mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v10">
</com.mapbox.mapboxsdk.maps.MapView>
<Button
android:id="#+id/startButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:enabled="true"
android:text="#string/crear_fiesta"
android:textColor="#color/mapboxGrayLight"
mapbox:layout_constraintBottom_toBottomOf="parent"
mapbox:layout_constraintEnd_toEndOf="parent"
mapbox:layout_constraintStart_toStartOf="parent" />
I have tried putting a toast message inside the onClick method but nothing happens, so I guess it just does not enter there.
Anyone know why? Thanks for any answer :D
You're overriding the wrong onCreate(), replace it with:
protected void onCreate(#Nullable Bundle savedInstanceState)
Edit: are you sure that this xml should be inflated in class SearchUser? Do you have a class MainActivity like the xml states?
Place the btnSearchUser button below the MapView in XML.
You've also configured MapView with match_parent, which is not applicable to a constraint layout parent. Set the height and width of MapView to 0dp as well.

How do I pass info from one activity's EditText to another for the url to be loaded in that new activity's WebView?

all! I have been searching through StackOverflow for a few hours now and cannot find a solution to my problem.
I am developing an android app that needs to take input from the user in one activity through an EditText (this happens to be an int value). I then need that EditText's int value to be passed to a second activity, and parsed into a url to be loaded into a webview in that second activity. However, I cannot get this to work. In fact, I cannot get my WebView to work at all, even trying to load websites like Google. Below is my code.
public void takeInput(View view) {
Intent intent = new Intent(this, WebViewActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
Integer vendorID = Integer.parseInt(editText.getText().toString());
intent.putExtra(EXTRA_VENDORID, vendorID);
startActivity(intent);
Above is the method that is called when an onscreen button is pressed OnClick.
The code from the next activity is below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
String testURL2 = "www.google.com";
WebView streetwiseSite = (WebView) this.findViewById(R.id.webView);
streetwiseSite.getSettings().setJavaScriptEnabled(true);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String vendorID = extras.getString(MainActivity.EXTRA_VENDORID);
Integer vendorID = Integer.parseInt(extras.getString(MainActivity.EXTRA_VENDORID));
String targetURL = "www.something.org/purchase/";
Here is the XML code for my first activity.
<RelativeLayout 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=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/logo" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:text="MobilePay"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:hint="Vendor ID #"
android:inputType="number" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:onClick="takeInput"
android:text="Pay" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Enter Vendor ID and"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:text="Payment Amount to Pay"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Here is my XML code for the WebView activity, the second activity.
<RelativeLayout 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.august.streetwise.WebViewActivity">
<WebView
android:id="#+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Ideally this should take the user's input, parse it into the URL, and load that website. However, here are the errors I get from logcat.
04-21 23:36:14.837 17904-17904/com.myname.app W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 17904
Any ideas? I've been pulling my hair out over this.
WebView.loadUrl() does not add any protocol formatting to the beginning of the string. Therefore, www.something.org does not automatically become http://www.something.org.
Without the protocol information at the beginning of the string, WebView.loadUrl() throws the decidedly unhelpful error W/BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid because www.something.org isn't a complete URL.
You would think this behavior would be documented somewhere. ;)
change the following line
Intent intent = new Intent(this, WebViewActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
Integer vendorID = Integer.parseInt(editText.getText().toString());
Bundle bn=new Bundle();
bn.putInt(EXTRA_VENDORID, vendorID);
intent.putExtras(bn);
startActivity(intent);
in second activity
setContentView(R.layout.activity_web_view);
String testURL2 = "www.google.com";
WebView streetwiseSite =(WebView) this.findViewById(R.id.webView);
streetwiseSite.getSettings().setJavaScriptEnabled(true);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int vendorID = extras.getInt(MainActivity.EXTRA_VENDORID);
String targetURL = "www.something.org/purchase/";
and for passing this vendorid to a url you should do something like this
String targetURL = "www.something.org/purchase&KEYWORD="+vendorID;
nb:KEYWORD should be the exact word which is used in the api side of the url which you are using
try this to load url in webview
web.setWebViewClient(new WebViewClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(targetURL);

Android: made a text in RelativeLayout and can't change text in activity?

<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/text"
android:textColor="#F0F8FF"
android:textSize="50sp"
android:textStyle="bold" />
This is declaration of the text field in activity_main.xml.
And the activity:
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(napis);
Program crashes when I call setText() in onReasume() and it doesn't do anything when I call setText() in onCreate().
Whole xml file as requested:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/zajebiste1" />
<Chronometer
android:textColor="#F0F8FF"
android:id="#+id/chronometer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="46dp"
android:gravity="center"
android:textSize="40sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/text"
android:textColor="#F0F8FF"
android:textSize="50sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
Activity:
text = (TextView) findViewById(R.id.textView1);
text.setText("Your Text");
Can't show more, cause something doesn't work... xf
onCreate():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.textView1);
napis="Witaj!";
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//,........
text = (TextView) findViewById(R.id.textView1);
text.setText("Your Text");
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
onReasume():
#Override
protected void onResume() {
super.onResume();
senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
First of all, what is up with
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.activity_main); ??
Can you try doing
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_main, this);
TextView text = (TextView) layout.findViewById(R.id.textView1);
text.setText(napis);
setContentView(layout);
set the contentView only once, towards the end of the method..
Try this, then post logcat if it doesn't work

image.setImageDrawable crashes app

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.

Categories