I'm trying to use a Tabhostin a Dialog so that the user can toggle inside the Dialog between to layouts. But I always get a NullPointerException. Any advice? or How to do it right?
Dialog Method:
public void dialog(){
final Dialog d = new Dialog(this);
d.setTitle("Dialog);
d.setContentView(R.layout.dialog);
d.setCanceledOnTouchOutside(false);
TabHost tabHost = (TabHost) d.findViewById(android.R.id.tabhost);
Button b_set = (Button) d.findViewById(R.id.b_choose);
Button b_cancel = (Button) d.findViewById(R.id.b_cancel);
tabHost.setup(); //Here is the problem
TabHost.TabSpec tab1 = tabHost.newTabSpec("Favs");
TabHost.TabSpec tab2 = tabHost.newTabSpec("All");
tab1.setIndicator("NEWTAB");
tab1.setContent(new Intent(this,Tab1Activity.class));
tab2.setIndicator("NEWTAB");
tab2.setContent(new Intent(this,Tab2Activity.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
d.show();
}
Dialog.xml:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Logcat:
11-30 14:48:06.661 7157-7157/com.test.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.test.app.Main.dialog (Main.java:269)
at com.test.app.Main$1.onClick(Main.java:139)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
TabHost tabHost = (TabHost) d.findViewById(R.id.tabHost);
id is case sensitive
Related
I'm developing an app where I have a fragment as my home page. I'm currently trying to call that fragment from an activity but unfortunately it is crashing. I had even implemented the FragmentTransaction and it still doesnt want to pick it up.
logcat
05-02 22:22:40.331 5927-5927/com.example.treycoco.calorietracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.treycoco.calorietracker, PID: 5927
java.lang.NullPointerException: Attempt to invoke virtual method' android.support.v4.app.FragmentTransactio android.support.v4.app.FragmentTransaction.replace(int, android.support.v4.app.Fragment)' on a null object reference at com.example.treycoco.calorietracker.CalorieDetails$1$1.onClick(CalorieDetails.java:79)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
update logcat
example.treycoco.calorietracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.treycoco.calorietracker, PID: 19224
java.lang.IllegalArgumentException: No view found for id 0x7f0e0080 (com.example.treycoco.calorietracker:id/FragmentHolder) for fragment FragmentHome{d567f7e #0 id=0x7f0e0080}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
caloriedetails.java
public class CalorieDetails extends AppCompatActivity {
private TextView foodName, calories, dateTaken;
private Button shareButton;
private int foodId;
private Button deleteButton;
private android.support.v4.app.FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calorie_details);
foodName = (TextView) findViewById(R.id.detsFoodName);
calories = (TextView) findViewById(R.id.detscaloriesValue);
dateTaken = (TextView) findViewById(R.id.detsDateText);
deleteButton = (Button) findViewById(R.id.deleteButton);
Food food = (Food) getIntent().getSerializableExtra("userObj");
foodName.setText(food.getFoodName());
calories.setText(String.valueOf(food.getCalories()));
dateTaken.setText(food.getRecordDate());
foodId = food.getFoodId();
foodName.setTextColor(Color.WHITE);
dateTaken.setTextColor(Color.WHITE);
calories.setTextSize(34.9f);
calories.setTextColor(Color.WHITE);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO: put delete functionality here
android.support.v7.app.AlertDialog.Builder alert = new
android.support.v7.app.AlertDialog.Builder(CalorieDetails.this);
alert.setTitle("Delete?");
alert.setMessage("Are you sure you want to delete this item?");
alert.setNegativeButton("No", null);
alert.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
DatabaseHandler dba = new
DatabaseHandler(getApplicationContext());
dba.deleteFood(foodId);
Toast.makeText(CalorieDetails.this, "Food Item
Deleted!", Toast.LENGTH_SHORT).show();
FragmentHome fragmentHome = new FragmentHome()
fragmentTransaction =
getSupportedFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentHolder,
fragmentHome);
fragmentTransaction.commit();
CalorieDetails.this.finish();
}
});
alert.show();
}
});
}
}
caloriedetails.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
tools:context=".CalorieDetails"
android:background="#drawable/imgbackground2"
style="#style/AppTheme"
>
<ImageView
android:id="#+id/logo"
android:src="#drawable/weight"
android:layout_centerHorizontal="true"
android:layout_width="180dp"
android:layout_height="180dp" />
<LinearLayout
android:id="#+id/layout"
android:elevation="4dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/detsFoodName"
android:elevation="4dp"
android:text="dkdad"
android:textSize="19sp"
android:textStyle="bold"
android:layout_marginTop="18dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/detsCaloriesTitle"
android:text="Calories:"
android:textSize="18sp"
android:layout_marginTop="18dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/detscaloriesValue"
android:text="200"
android:textSize="18sp"
android:layout_marginTop="18dp"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/detsDateText"
android:text="Consumed on..."
android:textStyle="italic"
android:textSize="14sp"
android:layout_marginTop="14dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/deleteButton"
android:text="DELETE"
android:textColor="#ffff"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="200dp"
android:background="#color/colorBackground2"
android:layout_height="wrap_content" />
</LinearLayout>
You have declared the fragmentManager and fragmentTransaction but not yet initialized.
FragmentHome fragmentHome = new FragmentHome();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentHolder, fragmentHome);
fragmentTransaction.commit();
Remove this line from onClick method,
CalorieDetails.this.finish();
Because you are going to add the fragment in activity so dont finish the activity.
Add this code in your caloriedetails.xml file
<LinearLayout
android:id="#+id/FragmentHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
Please do some google search before start implement.
Your fragmentTransaction is null as you don't initialize it hence the exception.
Do this
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
I am trying to ad admob ads to my app but I am getting this error in the logcat when it launches. The app ran fine before I added the admob so I think it's something to do with that.
03-16 19:08:00.683: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.musicbynumbers.scalesads/com.musicbynumbers.scalesads.MainMenu}: java.lang.NullPointerException
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:123)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-16 19:08:00.683: E/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 19:08:00.683: E/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 19:08:00.683: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 19:08:00.683: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 19:08:00.683: E/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method)
03-16 19:08:00.683: E/AndroidRuntime(529): Caused by: java.lang.NullPointerException
03-16 19:08:00.683: E/AndroidRuntime(529): at com.musicbynumbers.scalesads.MainMenu.onCreate(MainMenu.java:69)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-16 19:08:00.683: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-16 19:08:00.683: E/AndroidRuntime(529): ... 11 more
Java of MainMenu:
package com.musicbynumbers.scales;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import com.musicbynumbers.pianoscalesadscopy.R;
public class MainMenu extends LicenseCheckActivity implements View.OnClickListener {
Button majScales, minHarm, minMel, majArp, minArp, chrome, cont, pent,how;
ImageButton mainMenu;
Intent j;
Intent k;
Intent l;
Intent m;
Intent n;
Intent o;
Intent p;
Intent q;
Intent r;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.activity_main_menu);
Toast.makeText(this, "Checking Application License", Toast.LENGTH_SHORT).show();
// Check the license
//checkLicense();
j = new Intent(MainMenu.this, majorScales.class);
k = new Intent(MainMenu.this, minorHarmonic.class);
l = new Intent(MainMenu.this, MajorArpeggios.class);
m = new Intent(MainMenu.this, MinorArpeggios.class);
n = new Intent(MainMenu.this, ChromaticScales.class);
o = new Intent(MainMenu.this, ContraryMotion.class);
p = new Intent(MainMenu.this, PentatonicScales.class);
q = new Intent(MainMenu.this, minorMelodic.class);
r = new Intent(MainMenu.this, HowItWorks.class);
mainMenu = (ImageButton) findViewById(R.id.imagelogo);
majScales = (Button) findViewById(R.id.majorscalesb);
minHarm = (Button) findViewById(R.id.minorharmonicb);
minMel = (Button) findViewById(R.id.minormelodicb);
majArp = (Button) findViewById(R.id.majorarpeggiosb);
minArp = (Button) findViewById(R.id.minorarpeggiosb);
chrome = (Button) findViewById(R.id.chromaticscalesb);
cont = (Button) findViewById(R.id.contraryb);
pent = (Button) findViewById(R.id.pentatonicscaleb);
how = (Button) findViewById(R.id.howitworksb);
majScales.setOnClickListener(this);
mainMenu.setOnClickListener(this);
minHarm.setOnClickListener(this);
minMel.setOnClickListener(this);
majArp.setOnClickListener(this);
minArp.setOnClickListener(this);
chrome.setOnClickListener(this);
cont.setOnClickListener(this);
pent.setOnClickListener(this);
how.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.imagelogo:
Intent i = new Intent(MainMenu.this, MainMenu.class);
startActivity(i);
break;
case R.id.majorscalesb:
startActivity(j);
break;
case R.id.minorharmonicb:
startActivity(k);
break;
case R.id.majorarpeggiosb:
startActivity(l);
break;
case R.id.minorarpeggiosb:
startActivity(m);
break;
case R.id.chromaticscalesb:
startActivity(n);
break;
case R.id.contraryb:
startActivity(o);
break;
case R.id.pentatonicscaleb:
startActivity(p);
break;
case R.id.minormelodicb:
startActivity(q);
break;
case R.id.howitworksb:
startActivity(r);
break;
}
}
}
activity_main_menu xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/musicbynumbersbg"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainMenu" >
<ImageButton
android:id="#+id/imagelogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="#drawable/topbanner"
android:gravity="center" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<Button
android:id="#+id/howitworksb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="How to Use the App" />
<Button
android:id="#+id/majorscalesb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Major Scales" />
<Button
android:id="#+id/minormelodicb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Melodic Scales" />
<Button
android:id="#+id/minorharmonicb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Harmonic Scales" />
<Button
android:id="#+id/majorarpeggiosb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Major Arpeggios" />
<Button
android:id="#+id/minorarpeggiosb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Minor Arpeggios" />
<Button
android:id="#+id/chromaticscalesb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Chromatic Scales" />
<Button
android:id="#+id/contraryb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Contrary Motion" />
<Button
android:id="#+id/pentatonicscaleb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Pentatonic Scale" />
</LinearLayout>
</ScrollView>
<com.google.ads.AdView android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId=" a15144ab55b99a9"
ads:loadAdOnCreate="true"
ads:adSize="BANNER"
/>
</LinearLayout>
Thanks in advance for trying to help.
I guess you are not setting right layout here.
setContentView(R.layout.activity_main_menu); <<You set the menu layout.
You must set the layout which is in res folder.
If you have layout.xml in res folder then set it like
setContentView(R.layout.layout);
Edit: Clean the project and run it.
My app is crashing. I have errors and I just got done debugging and as the title says thats the problem.
While debugging I found the line that is causing the problem:
listView.setAdapter(arrayAdapter2);
Towards the bottom. I had a similar nullpointerException before here. This time it is different as I am declaring my arrayadapter in the right area. In debugging the result that goes into the array adapter is not NULL.
I have searched the forum and found these:
Why am I getting an InvocationTargetException? Android 2D game
another nullpointerexception
These NullPointerExceptions seem pretty common in android but they are pretty code specific.
In reading the other information correct me if I am wrong the only place the null can be is if the arrayAdapter2 is NULL.
public class ByZipcode extends Activity{
Button btngetLObyzipcode;
Spinner spinner1;
ProgressBar progressBar1;
EditText textinput4byzip;
UserFunctions userFunctions = new UserFunctions();
ArrayAdapter<String> arrayAdapter2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.byzipcodepage);
arrayAdapter2 = new ArrayAdapter<String>(ByZipcode.this,android.R.layout.simple_list_item_1);
// Initializing spinner with predetermined results
spinner1 = (Spinner) findViewById(R.id.spinner1);
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
textinput4byzip = (EditText) findViewById(R.id.textinput4byzip);
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.byzipspinner, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(spinnerAdapter);
btngetLObyzipcode = (Button) findViewById(R.id.btngetLObyzipcode);
btngetLObyzipcode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (textinput4byzip.getText() != null & textinput4byzip.getText().toString().length() == 5 ){
progressBar1.setVisibility(View.VISIBLE);
new DownloadDataTask().execute();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_screen, menu);
return true;
}
private class DownloadDataTask extends AsyncTask<JSONArray, JSONArray, ArrayList<String> > {
#Override
protected ArrayList<String> doInBackground(JSONArray... params) {
String spinValue = spinner1.getSelectedItem().toString();
//if(textinput4byzip.getText() != null)
JSONArray json = userFunctions.getCustomerbyZipCode((textinput4byzip.getText().toString()), spinValue);
ArrayList<String> customers = new ArrayList<String>();
for(int i=0; i < json.length() ; i++) {
JSONObject jarray;
try {
jarray = json.getJSONObject(i);
String zip = jarray.getString("CustomerName");
customers.add(zip);
Log.d(zip,"Output");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return customers;
}
protected void onPostExecute(ArrayList<String> result){
ListView listView = (ListView) findViewById(R.id.listView1);
arrayAdapter2.addAll(result);
listView.setAdapter(arrayAdapter2);
progressBar1.setVisibility(View.GONE);
Intent viewCustomers = new Intent(getApplicationContext(), StoreListView.class);
viewCustomers.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(viewCustomers);
}
}
}
Just in case you are interested the failure is below:
02-10 17:39:36.976: E/AndroidRuntime(7686): FATAL EXCEPTION: main
02-10 17:39:36.976: E/AndroidRuntime(7686): java.lang.NullPointerException
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.example.lo.ByZipcode$DownloadDataTask.onPostExecute(ByZipcode.java:93)
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.example.lo.ByZipcode$DownloadDataTask.onPostExecute(ByZipcode.java:1)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.os.Looper.loop(Looper.java:137)
02-10 17:39:36.976: E/AndroidRuntime(7686): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-10 17:39:36.976: E/AndroidRuntime(7686): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 17:39:36.976: E/AndroidRuntime(7686): at java.lang.reflect.Method.invoke(Method.java:511)
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-10 17:39:36.976: E/AndroidRuntime(7686): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-10 17:39:36.976: E/AndroidRuntime(7686): at dalvik.system.NativeStart.main(Native Method)
As requested the xml page for the above activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/textinput4byzip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:contentDescription="#string/zipcodefielddescription"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:inputType="number"
android:maxLength="#integer/zipcodelength"
android:textSize="#dimen/LargeFont" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="#string/entrzip"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="#dimen/MediumFont" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingLeft="#dimen/MediumFont"
android:paddingTop="#dimen/MediumFont" >
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="285dp"
android:layout_height="308dp"
android:layout_gravity="center_horizontal|center_vertical"
android:visibility="gone" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:entries="#array/byzipspinner"
android:textAlignment="center" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/restaurants"
android:textSize="#dimen/LargeFont" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="CheckBox"
android:textSize="#dimen/LargeFont" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="CheckBox"
android:textSize="#dimen/LargeFont" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="CheckBox"
android:textSize="#dimen/LargeFont" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:paddingRight="#dimen/MediumFont" >
<Button
android:id="#+id/btngetLObyzipcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/getlo"
android:textSize="#dimen/LargeFont" />
</LinearLayout>
</LinearLayout>
ListView listView = (ListView) findViewById(R.id.listView1);
You're looking in byzipcodepage.xml for a ListView with ID listView1. No such view exists; in fact, there is no ListView in that layout at all.
If you want a list in that view (for the adapter to use), you need to add it; if your ListView is within a different activity, you may need to handle this entire situation a bit differently to pass the data from this activity to that one (perhaps through a Bundle).
I am trying to use the tabhost when I add it to my xml it doesn't look right, and I believe there is something that needs to be done in java, I am trying to set up three tabs with three different classes is this possible?
Here is my xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browser History:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call Log"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Messages"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
</LinearLayout>
And I have three different classes, because I am trying to use each tab to open each activity.
Here are the Classes
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.TabHost;
import android.widget.TextView;
public class AndroidSpybetaActivity extends TabActivity {
/** Called when the activity is first created. */
Resources res;
TabHost tabHost;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, Messaging.class);
spec = tabHost.newTabSpec("messaging").setIndicator("Messaging")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TestingData.class);
spec = tabHost.newTabSpec("Calls").setIndicator("Calls")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
TextView view = (TextView) findViewById(R.id.hello);
Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
null, null, null, null);
mCur.moveToFirst();
int index = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
while (mCur.isAfterLast() == false) {
view.append(" WebSite " + mCur.getString(index));
mCur.moveToNext();
}
}
}
Second
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Messaging extends TabActivity{
static TextView messageBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageBox = (TextView) findViewById(R.id.tvSms);
}
public static void updateMessageBox(String msg) {
messageBox.append(msg);
}
}
and third
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.widget.TextView;
public class TestingData extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.call);
String[] projection = new String[] {
Calls.NUMBER
};
Cursor mCur = managedQuery(CallLog.Calls.CONTENT_URI, projection,
Calls.DURATION + "<?", new String[] { "60" }, Calls.DURATION
+ " ASC");
mCur.moveToFirst();
while (mCur.isAfterLast() == false) {
for (int i = 0; i < mCur.getColumnCount(); i++) {
view.append(" Number " + mCur.getString(i));
}
mCur.moveToNext();
}
}
}
Here is my logcat:
08-12 15:19:16.368: D/AndroidRuntime(280): Shutting down VM
08-12 15:19:16.368: W/dalvikvm(280): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-12 15:19:16.628: E/AndroidRuntime(280): FATAL EXCEPTION: main
08-12 15:19:16.628: E/AndroidRuntime(280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.AndroidSpybetaActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Looper.loop(Looper.java:123)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-12 15:19:16.628: E/AndroidRuntime(280): at dalvik.system.NativeStart.main(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:2473)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.addTab(TabHost.java:213)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.johnnydicamillo.spybeta.AndroidSpybetaActivity.onCreate(AndroidSpybetaActivity.java:31)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-12 15:19:16.628: E/AndroidRuntime(280): ... 11 more
08-12 15:19:21.929: I/Process(280): Sending signal. PID: 280 SIG: 9
Yes, it is possible.
You can specify each activity (Start an intent) for each tabs in the following manner
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, CalendarActivity.class);
spec = tabHost.newTabSpec("calendar").setIndicator("Calendar", res.getDrawable(R.drawable.ic_tab_calendar)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ProfileActivity.class);
spec = tabHost.newTabSpec("profile").setIndicator("Profile", res.getDrawable(R.drawable.ic_tab_profile)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
Each activity will have their own content layout views, therefore no need to worry about that in the main layout.
Your main XML layout will be small and simple as
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
I think this is what you want.
It is not possible to have a tab correspond to an activity. The purpose of tabs is to break up one activity into how ever many views if there is too much info display in one view(not to be confused with activity). Here however is how you set up TabHost:
First start with an xml:
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
-
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
-
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AnalogClock
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button" />
</FrameLayout>
</LinearLayout>
</TabHost>
Sorry that's hard to read but if you copy it into eclipse you should be fine. And then the Java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class TabDemo extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs=(TabHost)findViewById(R.id.tabhost); //Id of tab host
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");//make a new tab
spec.setContent(R.id.tab1); //What is in the tab (not an activity but rather a view)
spec.setIndicator("Clock"); //Name of tab
tabs.addTab(spec); //Add it
spec=tabs.newTabSpec("tag2"); //Same thing here
spec.setContent(R.id.tab2);
spec.setIndicator("Button");
tabs.addTab(spec);
}
}
This is relatively simple an I am sure there is support in the android development website and of course just searching on Google. This code was copied out of Beginning Android 4 by Grant Allen and the book explains this topic in much greater detail. Good luck!
I have an app that needs to capture a customer's signature and then present it in an ImageView. For proof-of-concept, I've written a small app that has a layout with a GestureOverlayView at the top, then a "Save Signature" button, then an ImageView below. When I press the Save Signature button, it should create an image from the GestureOverlayView and display it in the ImageView. Below is the code/layout and then a list of issues:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.gesture.GestureOverlayView android:id="#+id/GestureView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFFFF"
android:fadeEnabled="false"
android:gestureStrokeType="multiple"
android:gestureStrokeWidth="1.5"
android:gestureColor="#000000"
android:fadeOffset="600000"
android:eventsInterceptionEnabled="false"
android:alwaysDrawnWithCache="true">
</android.gesture.GestureOverlayView>
<Button android:id="#+id/SubmitButton"
android:text="Save Signature"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<ImageView android:id="#+id/ImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFFFF">
</ImageView>
</LinearLayout>
Code:
public class TestActivity extends Activity {
ImageView iv;
GestureOverlayView gv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)findViewById(R.id.ImageView);
gv = (GestureOverlayView)findViewById(R.id.GestureView);
Button submitButton = (Button)findViewById(R.id.SubmitButton);
submitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
}
});
}
}
I get a Null Pointer exception every time on the createBitmap() method. I believe it is because gv.getDrawingCaches() is returning null. As you can see, drawing cache IS enabled so it should return SOMETHING!
Here's the logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
at com.pgov.barcode.TestActivity$2.onClick(TestActivity.java:42)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Any and all help would be greatly appreciated!
Apparently I answered my own question. Even though I used:
android:alwaysDrawnWithCache="true"
I changed the code to:
gv.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap(gv.getDrawingCache());
iv.setImageBitmap(b);
gv.setDrawingCacheEnabled(false);
And it works! Not sure why the XML flag doesn't affect it though...