06-30 11:59:40.153 27202-27202/com.downloader.newlevel.krim.newleveldownloader E/ViewSystem﹕ ViewRootImpl #2 Surface is not valid.
06-30 11:59:41.114 27202-27216/com.downloader.newlevel.krim.newleveldownloader E/Error﹕ https://photos-4.dropbox.com/t/2/AAAxrzwz2B32dmtjRTNlT1vhQfVd5JuglMkbVV_m8BRkLg/12/145533347/png/32x32/1/_/1/2/Hourglass%20Addon%20by%20Redstone.png/CKPTskUgASACIAMgBCAFIAYgBygBKAI/Jpa4t4f3nMDieOULhjrmus_8mVpqVO37XxrQYkzMoSo?size=800x600&size_mode=2
06-30 12:00:57.429 27202-27202/com.downloader.newlevel.krim.newleveldownloader E/ViewSystem﹕ ViewRootImpl #2 Surface is not valid.
06-30 12:00:58.219 27202-27755/com.downloader.newlevel.krim.newleveldownloader E/Error﹕ https://photos-4.dropbox.com/t/2/AAAxrzwz2B32dmtjRTNlT1vhQfVd5JuglMkbVV_m8BRkLg/12/145533347/png/32x32/1/_/1/2/Hourglass%20Addon%20by%20Redstone.png/CKPTskUgASACIAMgBCAFIAYgBygBKAI/Jpa4t4f3nMDieOULhjrmus_8mVpqVO37XxrQYkzMoSo?size=800x600&size_mode=2
This was found in my logcat, I am currently working on loading image from url.
Activity
package com.downloader.newlevel.krim.newleveldownloader;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import java.io.InputStream;
public class activity00 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity00);
String url000 = "https://photos-4.dropbox.com/t/2/AAAxrzwz2B32dmtjRTNlT1vhQfVd5JuglMkbVV_m8BRkLg/12/145533347/png/32x32/1/_/1/2/Hourglass%20Addon%20by%20Redstone.png/CKPTskUgASACIAMgBCAFIAYgBygBKAI/Jpa4t4f3nMDieOULhjrmus_8mVpqVO37XxrQYkzMoSo?size=800x600&size_mode=2";
new DownloadImageTask((ImageView) findViewById(R.id.image_main00)).execute(url000);
Intent splash = new Intent(this, splash_activity.class);
startActivity(splash);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, 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_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AsyncTask downloading image
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
xmlns:tools="http://schemas.android.com/tools"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
tools:context=".activity00"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TableLayout
android:id="#+id/tableLayout_main00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<Button
android:text="Script"
android:textAllCaps="false"
android:background="#ffffff"/>
<Button
android:text="Add-on"
android:textAllCaps="false"
android:background="#fffffa6a"/>
</TableRow> <TableRow>
<Button
android:text="Map"
android:textAllCaps="false"
android:background="#ff6ec0ff"/>
<Button
android:text="Skin"
android:textAllCaps="false"
android:background="#ffff6d66"/>
</TableRow> <TableRow>
<Button
android:text="Texture Pack"
android:textAllCaps="false"/>
<Button
android:text="Coming Soon"
android:textAllCaps="false"/>
</TableRow>
</TableLayout>
<TextView
android:id="#+id/text_main00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New"
android:textSize="20sp"
android:layout_below="#+id/tableLayout_main00"
android:clickable="true"
android:gravity="right"
android:layout_marginTop="20dp"/>
<android.support.v7.widget.CardView
android:id="#+id/cardview_main00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="4dp"
android:layout_below="#+id/text_main00"
android:clickable="true"
android:layout_marginTop="5dp">
<ImageView
android:id="#+id/image_main00"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scaleType="fitStart"/>
<TextView
android:id="#+id/text_main_cardview00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hourglass Addon by Redstone"
android:layout_gravity="bottom"/>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/text_main01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Popular"
android:textSize="20sp"
android:layout_below="#+id/cardview_main00"
android:clickable="true"
android:gravity="right"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.CardView
android:layout_below="#+id/text_main01"
android:id="#+id/cardview_main01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="4dp"
android:clickable="true"
android:layout_marginTop="5dp">
<ImageView
android:id="#+id/image_main01"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scaleType="fitStart"/>
<TextView
android:id="#+id/text_main_cardview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Armor Display by Chan"
android:layout_gravity="bottom"/>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/text_main02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Coming Soon"
android:textSize="20sp"
android:layout_below="#+id/cardview_main01"
android:clickable="true"
android:gravity="right"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.CardView
android:layout_below="#+id/text_main02"
android:id="#+id/cardview_main02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="4dp"
android:clickable="true"
android:layout_marginTop="5dp">
<ImageView
android:id="#+id/image_main02"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scaleType="fitStart"/>
<TextView
android:id="#+id/text_main_cardview02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PortalPX by TM"
android:layout_above="#+id/text_main_cardview22"/>
<TextView
android:id="#+id/text_main_cardview22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Limited Edition: Cabon Version"
android:layout_gravity="bottom"/>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
I don't know why this logcat appeared. Can anyone explain and solve it please?
"ViewRootImpl #2 Surface is not valid." in logcat means something went wrong in layout. I fixed mine by writing whole layout again...to make sure everything is okay :)
Splash screen had nothing to do with this error
I'm not entirely convinced your ImageView belongs to the layout in which you set the content view to.
Double check if that layout is correct, because this error occurs when you execute findViewById without inflating the parent layout view first with setContentView or with LayoutInflator.
Me too faced the same issue, later I saw that I had made a .xml in res/values during renaming my cardView ids bcoz of which I was facing error, after i deleted that file, my app ran smoothly.
Related
This question already has answers here:
ConstraintLayout views in top left corner
(5 answers)
Closed 5 years ago.
I recently started coding and I wanted to make an app that tells the percentage of a number but the UI keeps overlapping each other please help me solve this because this is my first app and I don't want the first app I made to fail
this is what it looks like in an android studio
and this is what it looks like in the emulator
package com.example.abhay.mathsucks;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
EditText percentageTxt;
EditText numberTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
totalTextView = (TextView) findViewById(R.id.totalTextView);
percentageTxt = (EditText) findViewById(R.id.percentageTxt);
numberTxt = (EditText) findViewById(R.id.numberTxt);
Button calcBtn = (Button) findViewById(R. id.calcBtn);
calcBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
float percentage = Float.parseFloat(percentageTxt.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberTxt.getText().toString());
totalTextView.setText(Float.toString(total));
}
});
}
#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 the mainactivity.java file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.abhay.mathsucks.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:id="#+id/totalTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="443dp"
android:paddingTop="10dp"
android:text="0"
android:textAlignment="center"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="%"
android:textAlignment="center"
android:textSize="20sp"
tools:layout_editor_absoluteX="353dp"
tools:layout_editor_absoluteY="206dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="What is"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="visible"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="104dp" />
<EditText
android:id="#+id/numberTxt"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number"
android:inputType="numberDecimal"
android:paddingTop="10dp"
android:textAlignment="center"
android:textSize="18sp"
tools:layout_editor_absoluteX="-4dp"
tools:layout_editor_absoluteY="370dp" />
<EditText
android:id="#+id/percentageTxt"
android:layout_width="337dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Percentage"
android:inputType="numberDecimal"
android:textAlignment="center"
android:visibility="visible"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="170dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Of"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="visible"
tools:layout_editor_absoluteX="170dp"
tools:layout_editor_absoluteY="272dp" />
<Button
android:id="#+id/calcBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f92b2b"
android:paddingTop="10dp"
android:text="Calculate"
android:textColor="#android:color/background_light"
android:textColorLink="#android:color/background_light"
android:textSize="10sp"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="447dp" />
</android.support.constraint.ConstraintLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.abhay.mathsucks.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#f92b2b"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="#+id/back"
layout="#layout/content_main"
android:visibility="visible"
tools:ignore="IncludeLayoutParam" />
</android.support.design.widget.CoordinatorLayout>
activity_main.xml
I think you messed up with Constraint-layout...
May this one help you
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textColor="#000"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="What is"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:gravity="center"/>
<LinearLayout
android:weightSum="2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_weight="1.5"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Percentage"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:maxLength="3"
android:inputType="number"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="%"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:layout_weight="0.5"
android:gravity="center"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="of"
android:layout_marginTop="50dp"
android:textSize="20dp"
android:gravity="center"/>
<EditText
android:layout_weight="1.5"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:inputType="number"
android:gravity="center"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_alignParentBottom="true"/>
main activity.java
package abhilmohan.blogspot.com;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
EditText amount1;
EditText amount2;
EditText amount3;
EditText amount4;
Button calculate;
double w=0;
double x=0;
double y=0;
double z=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
public void initcontrols() {
amount1=(EditText)findViewById(R.id.editText1);
amount2=(EditText)findViewById(R.id.editText2);
amount3=(EditText)findViewById(R.id.editText3);
amount4=(EditText)findViewById(R.id.editText4);
calculate=(Button)findViewById(R.id.button1);
}
public void calculate() {
w=Double.parseDouble(amount1.getText().toString());
x=Double.parseDouble(amount2.getText().toString());
y=w/12;
amount3.setText(Double.toString(y));
z=w*x/100;
amount4.setText(Double.toString(z));
}
public void gotoactivity (View v) {
Intent intent = new Intent(this,ResultPage.class);
calculate();
startActivity(intent);
}
am not getting result while calling calculator() void method on button click.i want my results to be published in two textviews created in reulst_page layout
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="left"
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="abhilmohan.blogspot.com.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/ctc"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:inputType="number"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/TDS"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:inputType="number"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/ok"
android:textColor="#ffffff"
android:onClick="gotoactivity" />
</LinearLayout>
</RelativeLayout>
resultpage.java
package abhilmohan.blogspot.com;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class ResultPage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_page);
getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
result_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/amount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/tdsamount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/rate"
android:textColor="#ffffff" />
</LinearLayout>
At the moment you are executing calculate() in the MainActivity.java second activity called ResultPage doesn't exist, therefore you can't change it's view (editText3and editText4).
In order to pass data to another activity you should fill your Intent with some extra data and then in your ResultPage activity's onCreate you would get underlying extras.
EDIT
MainActivity.java
public void gotoactivity (View v) {
calculate();
Intent intent = new Intent(this, ResultPage.class);
intent.putExtra("AMOUNT_3", y);
intent.putExtra("AMOUNT_4", z);
startActivity(intent);
}
ResultPage.java inside onCreate
Bundle extras = getIntent().getExtras();
if (extras != null) {
int amount3 = extras.getInt("AMOUNT_3");
int amount4 = extras.getInt("AMOUNT_4");
}
Im taking my first steps in developing for android. Following a tutorial i have created a button, textview,edit text, and am now trying to implimetn a list view.
i have followed the tutorial Exactly, and for some reason, when i implement the Tableview, my button and image disappear.
can anybody find out why?
.java:
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.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
TextView mainTextView;
Button mainButton;
EditText mainEditText;
ListView mainListView;
ArrayAdapter mArrayAdapter;
ArrayList mNameList = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//access the textview defined in Layout XML & set its value
mainTextView = (TextView) findViewById(R.id.main_textview);
mainTextView.setText("set In Java!");
//access the button defined in layout XML
mainButton = (Button) findViewById(R.id.main_button);
mainButton.setOnClickListener(this);
mainEditText = (EditText) findViewById(R.id.main_edittext);
mainListView = (ListView)findViewById(R.id.main_listview);
mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,mNameList);
mainListView.setAdapter(mArrayAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
mainTextView.setText(mainEditText.getText().toString() + " is learning Android Development");
mNameList.add(mainEditText.getText().toString());
mArrayAdapter.notifyDataSetChanged();
}
}
activity.XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/main_textview"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textview"/>
<EditText
android:id="#+id/main_edittext"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/hint"/>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- set OnClickListener to trigger results when pressed -->
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="#string/button"/>
<!--shows an image from your drawable rescources-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/main_textview"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textview"/>
<EditText
android:id="#+id/main_edittext"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/hint"/>
</LinearLayout>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="#string/button"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
Try this..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/main_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="textview" />
<EditText
android:id="#+id/main_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:hint="hint" />
</LinearLayout>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- set OnClickListener to trigger results when pressed -->
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="button" />
<!-- shows an image from your drawable rescources -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
LinearLayout is horizontal by default.
Your ListView is layout_width="match_parent" and root LinearLayout is horizontal.
so ListView Expands wide, ImageView and etc have pushed out and become invisible.
I recommend you to always specify android:orientation ("vertical" or "horizontal") in LinearLayout.
I have a problem with creating things using LayoutInflater:
package com.tip.calculator;
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.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements View.OnClickListener {
EditText billamount, percent, people;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
billamount = (EditText)findViewById(R.id.billtext);
percent = (EditText)findViewById(R.id.percenttext);
people = (EditText)findViewById(R.id.numberpeople);
Button unevensplitbutton = (Button)findViewById(R.id.unevensplit);
Button calculates = (Button)findViewById(R.id.calculate);
unevensplitbutton.setOnClickListener(this);
calculates.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.calculate:
TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2);
View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
calculatenumberinflated.addView(view);
break;
case R.id.unevensplit:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the XML of calculatenumbertable:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/numbertable2">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Each Person Pays"
android:id="#+id/numberpeople"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/eachpersonedit"
android:layout_column="6" />
</TableRow></TableLayout>
And the XML of fragment_main:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bill amount"
android:id="#+id/bill"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/billtext"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage %"
android:id="#+id/percentage"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/percenttext"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of people"
android:id="#+id/textView"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/numberpeople"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Split Unevenly"
android:id="#+id/unevensplit"
android:layout_column="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="#+id/calculate"
android:layout_column="6" />
</TableRow>
</TableLayout>
</ScrollView>
It returns an error where:
12-16 21:22:56.825 28459-28459/com.tip.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.tip.calculator.MainActivity.onClick(MainActivity.java:35)
Where line 35 is the calculatenumberinflated.addView(view); line of code.
I feel lost right now, can someone please clarify what I am doing wrong? Thanks.
You are getting Nullpointer error at this line TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2); which is not at all available in your fragment_main layout. It in the layout calculatenumbertable file and as you are inflating fragment_main layout so its not getting the correct id of your TableLayout.
Try to add your layout in TableRow besides TableLayout. Create separate row in your calculatenumbertable layout and then try add the layout in that row. You can not add layout directly in TableLayout.
Try as below:
In your calculatenumbertable add row in last as below:
<TableRow
android:id="#+id/tablerow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableRow>
In your onClick add the layout as below:
public void onClick(View v) {
switch(v.getId()){
case R.id.calculate:
TableRowcalculatenumberinflated = (TableRow)findViewById(R.id.tablerow);
View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
calculatenumberinflated.addView(view);
break;
case R.id.unevensplit:
break;
}
}
Just move android:id="#+id/numbertable2" from calculatenumbertable.xml to fragment_main.xml in the tableLayouts.
Try this
LayoutInflater inflater=getLayoutInflater();
View view= inflater.inflate(R.layout.calculatenumbertable, null);
calculatenumberinflated.addView(view);
So i've been using facebook's latest sdk (3.5.1) and I installed the sample app Scrumptious on my device but after I tried to login and I approved the premission dialog, it did nothing. I tried deleting the facebook application and apparently it only works when it's not integrating with the facebook application itself and just tries to login via the browser.
Did anyone other than me encountered the problem? Is it solvable?
this is the code for the sample app in facebook.
package com.facebook.scrumptious;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class SplashFragment extends Fragment {
private Button skipLoginButton;
private SkipLoginCallback skipLoginCallback;
public interface SkipLoginCallback {
void onSkipLoginPressed();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.splash, container, false);
skipLoginButton = (Button) view.findViewById(R.id.skip_login_button);
skipLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (skipLoginCallback != null) {
skipLoginCallback.onSkipLoginPressed();
}
}
});
return view;
}
public void setSkipLoginCallback(SkipLoginCallback callback) {
skipLoginCallback = callback;
}
}
this is the xml for the login page.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#303040" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/splash_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:gravity="center"
android:src="#drawable/icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#AFDEFE"
android:textSize="28sp"
android:typeface="serif"
android:textStyle="italic"
android:text="#string/app_name" />
</LinearLayout>
<TextView
android:id="#+id/profile_name"
android:layout_width="174dp"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:lines="2"
android:textSize="17sp"
android:text="#string/get_started"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
<com.facebook.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
<Button
android:id="#+id/skip_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/skip_login"
android:background="#drawable/com_facebook_loginbutton_silver"
android:textSize="#dimen/com_facebook_loginview_text_size"
android:textColor="#color/com_facebook_blue"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
</LinearLayout>
</ScrollView>