Separate phone & tablet XML layouts not deploying - java

I have created 2 XML files where both of which have completely different layouts. Whenever I deploy my app to a phone it loads successfully. However when I do the same thing with a tablet, it doesn't work and therefore crashes. I know that it has something to do with a lack of ListView in the sw600dp layout file but what must be done to make sure that sw600dp devices don't keep checking for the ListView?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
sw600dp/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/abslistview_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:stretchColumns="*"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TableRow
android:id="#+id/MainActivity_tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
<Button
android:id="#+id/MainActivity_btn0"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_circle"
android:onClick="btncircle_click"
android:text="#string/circle"
/>
<Button
android:id="#+id/MainActivity_btn1"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_star"
android:onClick="btnstar_click"
android:text="#string/star"
/>
</TableRow>
<TableRow
android:id="#+id/MainActivity_tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
<Button
android:id="#+id/MainActivity_btn2"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_square"
android:onClick="btnsquare_click"
android:text="#string/square"
/>
<Button
android:id="#+id/MainActivity_btn3"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_triangle"
android:onClick="btntriangle_click"
android:text="#string/triangle"
/>
</TableRow>
<TableRow
android:id="#+id/MainActivity_tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/MainActivity_btn4"
android:layout_column="0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_heart"
android:onClick="btnheart_click"
android:text="#string/heart"
/>
<Button
android:id="#+id/MainActivity_btn5"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:drawableStart="#drawable/ic_crescent"
android:onClick="btncrescent_click"
android:text="#string/crescent"
/>
</TableRow>
</TableLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// array of images for ListView
int[] listviewImage = new int[]{
R.drawable.ic_circle,
R.drawable.ic_star,
R.drawable.ic_square,
R.drawable.ic_triangle,
R.drawable.ic_heart,
R.drawable.ic_crescent
};
// array of strings for ListView
String[] listviewTitle = new String[]{
getResources().getString(R.string.circle),
getResources().getString(R.string.star),
getResources().getString(R.string.square),
getResources().getString(R.string.triangle),
getResources().getString(R.string.heart),
getResources().getString(R.string.crescent)
};
List<HashMap<String, String>> aList = new ArrayList<>();
for (int i = 0; i < 6; i++) {
HashMap<String, String> hm = new HashMap<>();
hm.put("listview_image", Integer.toString(listviewImage[i]));
hm.put("listview_title", listviewTitle[i]);
aList.add(hm);
}
String[] from = {"listview_image", "listview_title"};
int[] to = {R.id.mainitem_img, R.id.mainitem_title};
SimpleAdapter simpleAdapter = new SimpleAdapter(this.getBaseContext(), aList, R.layout.listitem_main, from, to);
ListView list_main = this.findViewById(R.id.listview_main);
list_main.setAdapter(simpleAdapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
//blah, blah, blah
}
if (position == 1) {
//blah, blah, blah
}
if (position == 2) {
//blah, blah, blah
}
if (position == 3) {
//blah, blah, blah
}
if (position == 4) {
//blah, blah, blah
}
if (position == 5) {
//blah, blah, blah
}
}
});
}
public void btncircle_click(View view) {
}
public void btnstar_click(View view) {
}
public void btnsquare_click(View view) {
}
public void btntriangle_click(View view) {
}
public void btnheart_click(View view) {
}
public void btncrescent_click(View view) {
}
}

Your problem is that you are trying to load your listview_main even though in a tablet where that View does not exist, so you are getting a NULL value.
This line is giving you a null value in a tablet:
ListView list_main = this.findViewById(R.id.listview_main);
Because that View is not defined in your tablet layout.
In order to prevent this, you need to control whether you are in a tablet or in a phone. I solved this, creating a resource file inside the res folder, and other in your sw600dp one.
In the main one you will have this:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<bool name="is_tablet">false</bool>
</resources>
And in the sw600dp one:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<bool name="is_tablet">true</bool>
</resources>
Then in your code you need to check whether you are in a tablet or not by doing this:
if (getResources().getBoolean(R.bool.is_tablet))
There you will know what to do according to the loaded layout.
Another option is to have 2 different activities, create a launcher activity that will check your isTablet value and then load your MainActivityPhone or MainActivityTablet.
Hope it helps

Related

App Keeps Stopping when pressing a button

app keeps crashing when ever the button is pressed, tried on emulator as well as on a phone. I'm using Android Studio 3.1.2. How can i fix it, i've tried most of other solutions with same problems over here but didnt work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton(View v) {
display(1);
}
public void display(int n) {
TextView qt = findViewById(R.id.quantity;
qt.setText(n);
}
}
here's its xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Quantity"
android:textAllCaps="true"
android:textSize="32sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="123dp"
android:id="#+id/quantity"
android:text="0"
android:textAllCaps="true"
android:textSize="32sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDER"
android:textSize="32sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp"
android:onClick="onButton"/>
</RelativeLayout>
do the below:
qt.setText(String.valueOf(n));
// or use qt.setText("" + n);
instead of
qt.setText(n);
It is because
setText(#StringRes int resid)
takes the parameter as "int resId" which it expects to be the id of the String value from the Strings.xml file.
But here you want to display the "int" value in the textview, so you need to use the overloaded method:
setText(CharSequence text)
method instead, and convert your int value to the String using
String.valueOf(n) //or by ("" + n)
Change this:
public void display(int n) {
TextView qt = findViewById(R.id.quantity;
qt.setText(n);
}
To this:
public void display(int n) {
TextView qt = findViewById(R.id.quantity); //add closing bracket
qt.setText("" + n); //makes 'n' a string
}

how to show full layout in BottomSheetDialog with min and max height 350dp in android

My layout_xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="#dimen/_350sdp"
android:maxHeight="#dimen/_500sdp"
android:orientation="vertical"
android:id="#+id/commentProblemScreenLL">
<ListView
android:id="#+id/commentProblemLV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/commentProblemNotFound"
android:visibility="gone"
android:id="#+id/commentProblemNotFoundTV"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_40sdp"
android:background="#drawable/side_nav_bar"
android:orientation="horizontal"
android:padding="#dimen/m1dp"
android:weightSum="1"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/commentEditTextET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:background="#null"
android:hint="#string/comment_EditText"
android:padding="5dip"
android:textColor="#color/white"
android:textColorHint="#color/hintcolor" />
<ImageButton
android:id="#+id/commentSendTextIB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:layout_alignParentEnd="true"
android:background="#drawable/side_nav_bar"
android:paddingBottom="#dimen/_4sdp"
android:src="#drawable/cm_chat_send"
android:layout_alignParentRight="true"
android:paddingRight="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"/>
</RelativeLayout>
</RelativeLayout>
Java code:-
private void showCommentDailog()
{
Log.d(TAG,"showCommentDailog enter()");
commentDialog = new BottomSheetDialog(getContext());
View view = ((ProblemsDetailActivity) getActivity()).getLayoutInflater().inflate(R.layout.commentproblemscreen, null);
commentDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
commentProblemLV = (ListView)view.findViewById(R.id.commentProblemLV);
TextView commentProblemNotFoundTV = (TextView)view.findViewById(R.id.commentProblemNotFoundTV);
EditText commentEditTextET = (EditText) view.findViewById(R.id.commentEditTextET);
ImageButton commentSendTextIB = (ImageButton)view.findViewById(R.id.commentSendTextIB);
if(commentDialog.getWindow() != null) {
commentDialog.getWindow().setGravity(Gravity.BOTTOM);
}
//call the function to show list of previous work
commentDialog.setContentView(view);
commentDialog.setCanceledOnTouchOutside(true);
commentDialog.show();
final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from((View) view.getParent());
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
commentDialog.dismiss();
}else{
if (newState != BottomSheetBehavior.STATE_EXPANDED) {
if(commentProblemLV != null && !listIsAtTop()){
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
}
This code does not show the full layout in bottomsheetDailog. Please correct my code as it shows the full layout in bottomsheetDialog.
I want to open full layout in half screen with BottomSheetDialog.
here I should be given my java code also for bottomSheetDialog.
bottomsheetdailog will apear on clickof comment ImageView.

Checkbuttons crashes app when checked

I created a list of checkboxes, and I was trying to increase an int value proportionally to the number of the selected ones. Instead, every time I try to check them (through .isChecked()) the app crashes. I've tried to search everywhere for some solutions, but nothing worked. I really can't understand where is the problem.
Here's the code
XML 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"
tools:context="com.example.deadlybanana.myfirstapp.DisplayPicture">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp">
<LinearLayout
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/unicorn" />
<TextView
android:id="#+id/frasenome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<CheckBox
android:id="#+id/checkbox1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Beatiful?" />
<CheckBox
android:id="#+id/checkbox2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Fabolous?" />
<CheckBox
android:id="#+id/checkbox3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Unique?" />
<CheckBox
android:id="#+id/checkbox4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you a horse?" />
<CheckBox
android:id="#+id/checkbox5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Do you eat grass?" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="checkbutton"
android:text="Check if you are a unicorn" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Java file
package com.example.deadlybanana.myfirstapp;
public class DisplayPicture extends AppCompatActivity {
List<Boolean> checkboxes = new ArrayList<Boolean>();
CheckBox press1;
CheckBox press2;
CheckBox press3;
CheckBox press4;
CheckBox press5;
int points = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_picture);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1);
CheckBox press2 = (CheckBox)findViewById(R.id.checkbox2);
CheckBox press3 = (CheckBox)findViewById(R.id.checkbox3);
CheckBox press4 = (CheckBox)findViewById(R.id.checkbox4);
CheckBox press5 = (CheckBox)findViewById(R.id.checkbox5);
TextView NameString = (TextView) findViewById(R.id.frasenome);
String phrase = name + ", are you some of these things?";
NameString.setText(phrase);
}
public void checkbutton(View view){
if (press1.isChecked()){
points += 4;
}
if (points > 3){
Toast.makeText(this, "You are a unicorn", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, "You are not a unicorn", Toast.LENGTH_LONG).show();
}
}
}
(right now it is being examined just the first checkbox to simplify the code)
Try this: in onCreate()
press1 = (CheckBox)findViewById(R.id.checkbox1);
press2 = (CheckBox)findViewById(R.id.checkbox2);
press3 = (CheckBox)findViewById(R.id.checkbox3);
press4 = (CheckBox)findViewById(R.id.checkbox4);
press5 = (CheckBox)findViewById(R.id.checkbox5);
error occurs as press1.isChecked() is refers to the global CheckBox press1;
which is not yet initialized..
AS you are initializing the checkbox like:
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1); inside onCreate() which only gives it a local instance which is not accessable in your checkbutton(View view) function
Solution
remove the local initialization just use: press1 = (CheckBox)findViewById(R.id.checkbox1);
Set id for your button lets assume it be button and add the following code
Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checkbutton();
}
});
Note:remove the view parameter
Implementing Listeners is the best way to handle the events
I never use the method isChecked() because it never works for me. Do you want to increase the points when you select a checkbutton and maybe show the points when you press a button?
Here is the class code:
public class ButtonActivity extends AppCompatActivity
{
int points=0;
boolean check1=false;
boolean check2=false;
boolean check3=false;
boolean check4=false;
boolean check5=false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),points+"",Toast.LENGTH_SHORT).show();
}
});
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.checkBox:
if(check1)
{
check1=false;
points--;
}
else
{
check1=true;
points++;
}
break;
case R.id.checkBox2:
if(check2)
{
check2=false;
points--;
}
else
{
check2=true;
points++;
}
break;
case R.id.checkBox3:
if(check3)
{
check3=false;
points--;
}
else
{
check3=true;
points++;
}
break;
case R.id.checkBox4:
if(check4)
{
check4=false;
points--;
}
else
{
check4=true;
points++;
}
break;
case R.id.checkBox5:
if(check5)
{
check5=false;
points--;
}
else
{
check5=true;
points++;
}
break;
}
}
}
Here is the layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_button"
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"
android:orientation="vertical"
tools:context="com.example.utente.stackoverflow.ButtonActivity">
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox2"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox3"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox4"
android:onClick="onClick"/>
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox5"
android:onClick="onClick"/>
<Button
android:text="Points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" />
I tested it and it works. I hope this helps you.

Why do I get an error in this ListView creation?

I'm trying to develop an ListView in Android that contains on Customer Name & Address and this list view is in a TabWidget. However, whenever I click that tab to see customer list info, application breaks. Following is my code:
The exception would be:
exception IllegalStateException (id=830019169944)
cause IllegalStateException (id=830019169944) detailMessage "Did
you forget to call 'public void setup(LocalActivityManager
activityGroup)'?" (id=830019169976) stackState (id=830019170216)
stackTrace null suppressedExceptions ArrayList (id=830019170192)
XML page
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:tag="tab0"
android:text="MainMenu"
android:background="#android:drawable/btn_star_big_on"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:tag="tab1"
android:text="Customers"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
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" >
<TextView
android:id="#+id/lblmainmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Menu"
android:textSize="20sp" />
<ImageButton
android:id="#+id/btnStock"
android:layout_marginTop="20dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignLeft="#+id/lblmainmenu"
android:layout_below="#+id/lblmainmenu"
android:onClick="btn_clickstock"
android:scaleType="fitCenter"
android:src="#drawable/dailystock" />
<ImageButton
android:id="#+id/btnDailySummury"
android:layout_marginTop="20dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="40dp"
android:layout_below="#+id/lblmainmenu"
android:layout_toRightOf="#+id/btnStock"
android:onClick="btn_clickDailySummury"
android:scaleType="fitCenter"
android:src="#drawable/dailystock" />
</RelativeLayout>
<ListView
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" >
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>
JAVA Main Activity code
public class MainMenuActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Intent intent = new Intent(this, Customer_List_Activity.class);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
final TabWidget tabWidget = tabHost.getTabWidget();
final FrameLayout tabContent = tabHost.getTabContentView();
// Get the original tab textviews and remove them from the viewgroup.
TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
for (int index = 0; index < tabWidget.getTabCount(); index++) {
originalTextViews[index] = (TextView) tabWidget.getChildTabViewAt(index);
}
tabWidget.removeAllViews();
// Ensure that all tab content childs are not visible at startup.
for (int index = 0; index < tabContent.getChildCount(); index++) {
tabContent.getChildAt(index).setVisibility(View.GONE);
}
// Create the tabspec based on the textview childs in the xml file.
// Or create simple tabspec instances in any other way...
for (int index = 0; index < originalTextViews.length; index++) {
final TextView tabWidgetTextView = originalTextViews[index];
final View tabContentView = tabContent.getChildAt(index);
TabSpec tabSpec = tabHost.newTabSpec((String) tabWidgetTextView.getTag());
if (index == 0)
{
tabSpec.setContent(new TabContentFactory() {
#Override
public View createTabContent(String tag) {
return tabContentView;
}
});
}
else
tabSpec.setContent(intent);
if (tabWidgetTextView.getBackground() == null) {
tabSpec.setIndicator(tabWidgetTextView.getText());
} else {
tabSpec.setIndicator(tabWidgetTextView.getText(), tabWidgetTextView.getBackground());
}
tabHost.addTab(tabSpec);
}
}
#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, 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);
}
ListView Activity Code XML Page
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/text1"
android:textSize="14dp"
android:textColor="#FFFFFF"
android:textStyle="italic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="12dp"
android:textColor="#BFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
ListView Activity Code Java
public class Customer_List_Activity extends ListActivity {
private SimpleAdapter sa;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
HashMap<String,String> item;
for(int i=0;i<HardwareShops.length;i++){
item = new HashMap<String,String>();
item.put( "line1", HardwareShops[i][0]);
item.put( "line2", HardwareShops[i][1]);
list.add( item );
}
sa = new SimpleAdapter(this, list,
R.layout.list_for_customer ,
new String[] { "line1","line2" },
new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter( sa );
}
private String[][] HardwareShops =
{{"Jayasekara","Colombo 03"},
{"Chandana","Colombo 03"},
{"Ruban","Borella"},
{"Safras","Colombo 05"},
{"Harris","Rajagiriya"},
{"HJ","Nawala"},
{"Himali","Nugegoda"},
{"Nilawala","Colombo 13"},
{"Jayantha","Colombo 09"}};
}
What would be the problem? Is there any missing code line?
you need to change MainMenuActivity's base class from Activity to ActivityGroup, as follows:
public class MainMenuActivity extends ActivityGroup {
...
}
ActivityGroup will take care of an instance of LocalActivityManager. So you don't need to create it. After the base class is changed, just call getLocalActivityManager() function defined in the base class to get that instance. Call tabHost's setup function like this:
tabHost.setup(this.getLocalActivityManager());

Using resources in Android freezes the main thread

I'm doing the Google Android App Course, and I encountered a weird error. If I call initVariables(), the layout is not appearing, if I comment it out, it appears. If I return after calling getResources(), I see the layout, if I return after calling getString(), no layout.
in the logcat i only see is some idle timeout reached.
i currently having some problems with my environment (eclipse crash and such), so i can't post a full logcat.
This is the code and the xml:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advanced);
initLayout();
initVariables();
}
protected void initLayout() {
m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);
m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
m_vwJokeLayout = (LinearLayout) findViewById(R.id.jokeListLayout);
}
protected void initVariables() {
Resources resources = getResources();
author = resources.getString(R.string.author_name);
m_nDarkColor = resources.getColor(R.color.dark);
m_nLightColor = resources.getColor(R.color.light);
m_arrJokeList = new ArrayList<Joke>();
}
layout
<?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" >
<LinearLayout
android:id="#+id/InputLayoutViewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/addJokeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Joke" />
<EditText
android:id="#+id/newJokeEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<ScrollView
android:id="#+id/jokeListViewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/jokeListLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</ScrollView>
</LinearLayout>
If R.string.author_name isn't found at runtime, I'll be throwing an exception. You could catch the exception
try {
author = resources.getString(R.string.author_name);
} catch (Resources.NotFoundException e) {
}
but the question should really be why is your R.string.author_name missing? Are you able to successfully get any string resources?

Categories

Resources