couldnt save which view has focus? - android

my app contains "Settings" activity which contains 2 imagebuttons "back" and "about", whenever i press any of these buttons i get:
"couldnt save which view has focus because the focused view android.widget.LinearLayout#4055a9e0 has no id"
the xml code for my_settings:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll2"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="15" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="8dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/settings" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|end"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/tb1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="#string/sett1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border2"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_back"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/back_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_about"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/about_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
dont pay attention to the "scrollview" (i dont think the problem is there), just look at the end of the code for the 2 imagebuttons..
and this the java code for Settings:
public class Settings extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_settings);
//code...
ImageButton goBack=(ImageButton)findViewById(R.id.st_ibt1);
ImageButton about=(ImageButton)findViewById(R.id.st_ibt2);
goBack.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, MainActivity.class);
startActivity(intent);
//finish();
}
});
about.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, About.class);
startActivity(intent);
//finish();
}
});
}
so, how to fix this? i dont understand what this error means.. any suggestions

I think the problem is :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
You enable focus to this LinearLayout, but you didn't provide any id for it.

Related

FATAL Exception:java.lang.RuntimeException: Unable to start activity ComponentInfo

I try to solve this problem but i don't find any solution so please help me to solve it.
1)My Package.java file:
b_indian=(Button)findViewById(R.id.b_indian);
b_classic=(Button)findViewById(R.id.b_classic);
b_royal=(Button)findViewById(R.id.b_royal);
t_pkg=(TextView)findViewById(R.id.t_pkg);
t_indian=(TextView)findViewById(R.id.t_indian);
t_classic=(TextView)findViewById(R.id.t_classic);
t_royal=(TextView)findViewById(R.id.t_royal);
b_indian.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Package.this,Indianpkg.class);
startActivity(i);
}
});
b_classic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Package.this,Classicpkg.class);
startActivity(i);
}
});
b_royal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Package.this,Royalpkg.class);
startActivity(i);
}
});
}
}
2)activity_package.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Package"
android:id="#+id/t_pkg"
android:layout_gravity="center_horizontal"
style="#style/Title"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_indian"
android:textSize="18sp"
android:gravity="center"
android:singleLine="false"
android:paddingBottom="10dp"
android:layout_margin="25dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="INDIAN"
android:id="#+id/t_indian"
android:layout_margin="55dp"
android:gravity="center_horizontal"
style="#style/SimpleText"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_classic"
android:textSize="18sp"
android:gravity="center"
android:singleLine="false"
android:paddingBottom="10dp"
android:layout_margin="25dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="CLASSIC"
android:layout_margin="50dp"
android:id="#+id/t_classic"
android:layout_gravity="center_horizontal"
style="#style/SimpleText"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_royal"
android:textSize="18sp"
android:gravity="center"
android:singleLine="false"
android:paddingBottom="10dp"
android:layout_margin="25dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ROYAL"
android:layout_margin="55dp"
android:id="#+id/t_royal"
android:layout_gravity="center_horizontal"
style="#style/SimpleText"/>
</RelativeLayout>
</TableRow>
</TableLayout>
This is my package file and i put same code to intent three different packages in that file but i intent only indian package and rest of two packages is not intent and i have error:FATAL Exception.
Following all files:
1)Indianpkg.java
b_i_venue=(Button)findViewById(R.id.b_i_venue);
b_i_decor=(Button)findViewById(R.id.b_i_decor);
b_i_caterer=(Button)findViewById(R.id.b_i_caterer);
b_i_photo=(Button)findViewById(R.id.b_i_photo);
b_i_mehandi=(Button)findViewById(R.id.b_i_mehandi);
b_i_dj=(Button)findViewById(R.id.b_i_dj);
t1=(TextView)findViewById(R.id.t1);
b_i_venue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Indianpkg.this,Indian_venue.class);
startActivity(i);
}
});
b_i_decor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Indianpkg.this,Indian_decoration.class);
startActivity(i);
}
});
b_i_caterer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Indianpkg.this,Indian_decoration.class);
startActivity(i);
}
});
b_i_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Indianpkg.this,Indian_caterer.class);
startActivity(i);
}
});
b_i_mehandi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Indianpkg.this,Indian_mehandi.class);
startActivity(i);
}
});
b_i_dj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Indianpkg.this,Indian_dj.class);
startActivity(i);
}
});
}
}
2)activity_indianpkg.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Indian Wedding"
android:id="#+id/t1"
android:layout_gravity="center_horizontal"
style="#style/Title"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_i_venue"
android:background="#drawable/i_venue"
android:text="Venue"
android:textSize="18sp"
android:gravity="center|bottom"
android:singleLine="false"
android:paddingBottom="10dp"
android:layout_margin="10dp"
style="#style/Title"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_i_decor"
android:background="#drawable/i_decoration"
android:text="Decoration"
android:textSize="18sp"
android:gravity="center|bottom"
android:singleLine="false"
android:paddingBottom="10dp"
android:layout_margin="10dp"
style="#style/Title"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_i_caterer"
android:text="Caterer"
android:background="#drawable/i_caterer"
android:textSize="18sp"
android:gravity="center|bottom"
android:layout_margin="10dp"
android:paddingBottom="10dp"
style="#style/Title"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_i_photo"
android:text="Photography"
android:background="#drawable/i_photography"
android:textSize="18sp"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:layout_margin="10dp"
style="#style/Title"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_i_mehandi"
android:text="Mehandi"
android:background="#drawable/i_mahendi"
android:textSize="18sp"
android:gravity="center|bottom"
android:layout_margin="10dp"
android:paddingBottom="10dp"
style="#style/Title"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_i_dj"
android:text="DJ"
android:background="#drawable/i_dj"
android:textSize="18sp"
android:gravity="center|bottom"
android:layout_margin="10dp"
style="#style/Title"
android:paddingBottom="10dp"/>
</RelativeLayout>
</TableRow>
</TableLayout>
Above code is run succesfully. But below code is also same but still not run and give FATAL Exception.
3)Classic.java
b_c_venue=(Button)findViewById(R.id.b_i_venue);
b_c_decor=(Button)findViewById(R.id.b_i_decor);
b_c_caterer=(Button)findViewById(R.id.b_i_caterer);
b_c_photo=(Button)findViewById(R.id.b_i_photo);
b_c_mehandi=(Button)findViewById(R.id.b_i_mehandi);
b_c_dj=(Button)findViewById(R.id.b_i_dj);
t2=(TextView)findViewById(R.id.t2);
b_c_venue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Classicpkg.this,Classic_venue.class);
startActivity(i);
}
});
b_c_decor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Classicpkg.this,Classic_decoration.class);
startActivity(i);
}
});
b_c_caterer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Classicpkg.this,Classic_caterer.class);
startActivity(i);
}
});
b_c_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Classicpkg.this,Classic_photography.class);
startActivity(i);
}
});
b_c_mehandi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Classicpkg.this,Classic_mehandi.class);
startActivity(i);
}
});
b_c_dj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Classicpkg.this,Classic_dj.class);
startActivity(i);
}
});
}
}
4)activity_classic.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" Classic Wedding"
android:id="#+id/t2"
android:layout_gravity="center_horizontal"
style="#style/Title"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_c_venue"
android:background="#drawable/i_venue"
android:text="Venue"
android:textSize="18sp"
android:gravity="center|bottom"
android:singleLine="false"
android:paddingBottom="10dp"
android:layout_margin="10dp"
style="#style/Title"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_c_decor"
android:background="#drawable/i_decoration"
android:text="Decoration"
android:textSize="18sp"
android:gravity="center|bottom"
android:singleLine="false"
android:paddingBottom="10dp"
android:layout_margin="10dp"
style="#style/Title"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_c_caterer"
android:text="Caterer"
android:background="#drawable/i_caterer"
android:textSize="18sp"
android:gravity="center|bottom"
android:layout_margin="10dp"
android:paddingBottom="10dp"
style="#style/Title"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_c_photo"
android:text="Photography"
android:background="#drawable/i_photography"
android:textSize="18sp"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:layout_margin="10dp"
style="#style/Title"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_c_mehandi"
android:text="Mehandi"
android:background="#drawable/i_mahendi"
android:textSize="18sp"
android:gravity="center|bottom"
android:layout_margin="10dp"
android:paddingBottom="10dp"
style="#style/Title"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/b_c_dj"
android:text="DJ"
android:background="#drawable/i_dj"
android:textSize="18sp"
android:gravity="center|bottom"
android:layout_margin="10dp"
style="#style/Title"
android:paddingBottom="10dp"/>
</RelativeLayout>
</TableRow>
</TableLayout>
enter image description here
May be you haven't add activity class in AndroidMaifest.xml
if you don't!
Add Activity to your AndroidMaifest.xml
like this
<activity
android:name=".Classic"
android:label="your_activity_label">
</activity>
Then Edit Code in Classic.java
b_c_venue=(Button)findViewById(R.id.b_c_venue);
b_c_decor=(Button)findViewById(R.id.b_c_decor);
b_c_caterer=(Button)findViewById(R.id.b_c_caterer);
b_c_photo=(Button)findViewById(R.id.b_c_photo);
b_c_mehandi=(Button)findViewById(R.id.b_c_mehandi);
b_c_dj=(Button)findViewById(R.id.b_c_dj);
t2=(TextView)findViewById(R.id.t2);
Your ids are wrong for your classic view, change ...i... for ....c... in your Classic Activity, like this:
b_c_venue=(Button)findViewById(R.id.b_c_venue);

Button inside cardview does not work

<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:orientation="horizontal"
android:padding="10dp"
android:paddingTop="8dp"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/view"
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/accent"/>
<TextView
android:id="#+id/m_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF6600"
android:textSize="30dp"
android:layout_toRightOf="#id/view"
android:text="hey th"
android:textStyle="bold" />
<Button
android:id="#+id/m_delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/m_name"
android:layout_weight="0"
android:text="Delete" />
</RelativeLayout>
</android.support.v7.widget.CardView>
I have put a button inside like this and this is shown in recycler view.
The problem I am facing is that the button click only works if I long press on the button else if i click normally then intent is called.
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MetricsDAO dao = new MetricsDAO(context);
dao.deleteMetrics(metrices.get(position).getEventName());
dao.close();
Intent i = new Intent(context, HealthGraph.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
// ((Activity) context).finish();
}
});
I am doing this inside on onBindViewHolder()..
What is it that i am doing wrong or missing?

findViewById() function returns null

I have layout file custom_lock.xml. It has 3 child layouts. Problem is when I try to access the elements of the layout from MainActivity.java, it returns null. Its driving me nuts. Please help me. Thanks.
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:name="#+id/place1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:gravity="center"
android:text="Place"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:name="#+id/listofapps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_blue_light"
android:text="List of Apps" />
<ListView
android:name="#+id/sites"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_green_light" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:name="#+id/calories"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_purple"
android:text="Health Data" />
<ListView
android:name="#+id/dialer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/darker_gray" />
</LinearLayout>
</LinearLayout>
Following is my MainActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_lock);
startService(new Intent(this, MyService.class));
TextView placexml = (TextView) findViewById(R.id.place1);
if (placexml == null)
System.out.println("placexml");
String place = "work";
//System.out.println("place: "+place);
placexml.setText(place);
boolean isApiRunning = ContextSdk
.isSemusiSensing(getApplicationContext());
if (isApiRunning)
Api.stopContext();
else {
SdkConfig config = new SdkConfig();
config.setPlacesAccuracyLevel(PlacesAccuracyLevel.EAccuracyHigh);
config.setActivityAccuracyLevel(ActivityAccuracyLevel.EAccuracyHigh);
config.setActivityTrackingAllowedState(true);
config.setAnalyticsTrackingAllowedState(true);
config.setDemographicsTrackingAllowedState(true);
config.setPedometerTrackingStateAllowed(true);
config.setPlacesTrackingAllowedState(true);
config.setRuleEngineEventStateAllowed(true);
Api.startContext(getApplicationContext(), config);
}
// reloadRssHandler.postDelayed(relaodRssRunnable, time);
//updateUILayer();
/*
txt1.setOnClickListener(new OnClickListener() {
#Override public void onClick(View arg0) { // TODO Auto-generated
method stub System.out.println("finishing......."); finish();
}
});
*/
}
When I run this program, "placexml" gets printed on console. I don't know, why this is happening. place1 is returning null from xml. Please help.
Change lines
android:name="#+id/place1"
to
android:id="#+id/place1"
You need to set ID instead of name.
setContentView(R.layout.custom_lock);
Wrong name of your layout "custom_layout"?
Try changing
<TextView
android:name="#+id/place1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:gravity="center"
android:text="Place"
android:textColor="#000000" />
with
<TextView
android:id="#+id/place1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:gravity="center"
android:text="Place"
android:textColor="#000000" />

How to populate the same view with layout inflater?

I have a layout and every time my button is pressed I want to add a group of views stored in another layout. This is the way I tried it so far. The error in logcat is:
"E/AndroidRuntime(7900): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
Code:
material_cost.xml
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/materialCostText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Κόστος Υλικών"
android:textColor="#FF0000"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center_horizontal"
android:layout_margin="5dp"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="4dp"
android:background="#33B5E5" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/materialWrapper">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:id="#+id/btnMaterialSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Αποθήκευση"
android:layout_weight="50"/>
<Button
android:id="#+id/btnMaterialAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Προσθήκη πεδίου"
android:layout_weight="50"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
material_cost_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/materialItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Υλικό"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Τιμή μονάδας"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Ποσότητα"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="4dp"
android:background="#33B5D8" />
</LinearLayout>
</LinearLayout>
And finally my class:
public class MaterialActivity extends Activity implements OnClickListener {
Button btnAdd;
LinearLayout rootLayout;
View layoutItem;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.material_cost);
btnAdd = (Button) findViewById(R.id.btnMaterialAdd);
rootLayout = (LinearLayout) findViewById(R.id.materialWrapper);
layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, rootLayout,false);
rootLayout.addView(layoutItem);
btnAdd.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.btnMaterialAdd:{
inflateEntry();
break;
}
}
}
private void inflateEntry() {
// TODO Auto-generated method stub
Log.v("debug","pressed");
rootLayout.addView(layoutItem);
}
}
you need to inflate the view again every time you click the button:
private void inflateEntry()
{
// TODO Auto-generated method stub
Log.v("debug","pressed");
View layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, null);
rootLayout.addView(layoutItem);
}

Android Horizontal orientation while rotation some parts missing

I am currently working on a application .when i installed my_app in android mobile while rotating in horizontal view my buttons are missing,please help!! Thanks in advance
My java coding is:
public class Draw extends Activity {
ImageView iv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final DrawView drawView = new DrawView(this);
setContentView(R.layout.draw);
FrameLayout frm_layout=(FrameLayout) findViewById(R.id.main_frame);
iv1=(ImageView) findViewById(R.id.imageView1);
frm_layout.addView(drawView);
Intent myIntent = getIntent(); // this is just for example purpose
String test=myIntent.getExtras().getString("guided");
if(test.equals("1"))
{
iv1.setImageResource(R.drawable.untitled);
iv1.setAlpha(100);
}
else
{
//iv1.setImageResource(null);
}
//Toast.makeText(getApplicationContext(), test.toString(), 2000).show();
Button btn_undo=(Button) findViewById(R.id.button1);
btn_undo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
drawView.onClickUndo();
}
});
Button btn_redo=(Button) findViewById(R.id.button2);
btn_redo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
drawView.onClickRedo();
}
});
}
}
My XML coding is below:
<?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"
android:orientation="vertical"
android:background="#android:color/white" >
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="fill_parent"
android:layout_height="472dp"
android:background="#android:color/white" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Redo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Undo" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
</RelativeLayout>
</LinearLayout>
Just Copy and Past in you xml file, it wil run completely. I have tested.
<?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"
android:orientation="vertical"
android:background="#android:color/white" >
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" android:layout_weight="0.1">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.9" android:layout_gravity="bottom" android:layout_marginTop="-10dp">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Redo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Undo" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
</RelativeLayout>
</LinearLayout>
Hope this is what you want.
do one thing make a folder named Layout-land in res folder , in which put your xml file. now you can design your xml in horizontal mode . do whatever changes you want in that file and check :
otw put a scroll bar in whole layout and then check :) good luck

Categories

Resources