Android: How do you set up the TabHost - java

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!

Related

What to do about "No view found for id..." in Android Activity with Fragments

I want my app to show this screen to a user in large-land mode so he can give input, press the button, and see output all on the same screen.
For any other mode, I want the two panes shown above to be shown one at a time, in this order, where the second screen appears when the user presses the button, after which pressing the back button returns to the "input" screen on the left:
What I have done works up to the point of showing correct output on the second screen but then I get "No view found for id" container and then an exception occurs
Here's xml for first screen above:
device(large-land).xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent"
android:id ="#+id/linear_layout_container_in_large_land_device_xml"
android:weightSum ="3"
>
<fragment
android:name="com.dslomer64.fragments.InputFragment" android:layout_weight ="2" android:layout_width="wrap_content" android:layout_height="match_parent"
android:id ="#+id/inputFragment"
tools:layout ="#layout/input_fragment"
/>
<fragment
android:name="com.dslomer64.fragments.OutputFragment" android:layout_weight ="1" android:layout_width="wrap_content" android:layout_height="match_parent"
android:id ="#+id/outputFragment"
tools:layout ="#layout/output_fragment"
/>
</LinearLayout>
device.xml (for non-large-land)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/container"
/>
input_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id ="#+id/tvInMainFragment" android:layout_width ="wrap_content" android:layout_height="wrap_content"
android:text="How many?"
/>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/etxIn_input_fragment"
android:text="3"
/>
<Button
android:id="#+id/btnIn_input_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Press to get output in OtherFragment"
/>
</LinearLayout>
output_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:id="#+id/txvIn_output_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Output will go here in OutputFragment"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device);
if (findViewById(R.id.container) != null) {
InputFragment inputFragment;
inputFragment = new InputFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, inputFragment)
.commit();
}
}
}
InputFragment.java
public class InputFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater,
ViewGroup viewGroup,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.input_fragment, viewGroup, false);
Button btn;
btn = (Button)view.findViewById(R.id.btnIn_input_fragment);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
OutputFragment outputFragment;
outputFragment = new OutputFragment();
if (getFragmentManager().findFragmentById(R.id.inputFragment) == null) {
getActivity().setContentView(R.layout.device);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, outputFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
getActivity().setContentView(R.layout.input_fragment);
EditText etx = (EditText) getActivity().findViewById(R.id.etxIn_input_fragment);
fillFragmentOtherWithInfo(etx.getText().toString());
}
});
return view;
}
public void fillFragmentOtherWithInfo(String ks)
{
getActivity().setContentView(R.layout.output_fragment);
TextView tvOther = (TextView)getActivity().findViewById(R.id.txvIn_output_fragment);
String s = "";
for(int i = 0; i < parseInt(ks); i++)
s += ("\n" + (char)(i+65));
tvOther.setText(s);
}
}
What happens is all good up to the point of showing the output on the second screen. It does show correctly, but then I immediately get an exception that is preceded with this message, and I have no idea what to do about it:
03-02 15:59:52.987 7901-7901/com.dslomer64.fragments E/FragmentManager: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ecc848 #1 id=0x7f070000}
container is the id of the FrameLayout in device.xml (non-large-land) that is referenced in the .add part of the fragment transaction in MainActivity onCreate. I get the same error if I use .replace instead of .add.
Here is the entire text of the error:
No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
Activity state:
Local Activity 41e4d8a8 State:
mResumed=true mStopped=false mFinished=false
mLoadersStarted=true
mChangingConfigurations=false
mCurrentConfig={1.15 311mcc480mnc en_US ldltr sw600dp w600dp h888dp 213dpi lrg port finger -keyb/v/h -nav/h skin=/system/framework/framework-res.apk s.16}
Active Fragments in 41e4d9a0:
#0: InputFragment{41e6d1d8 #0 id=0x7f070000}
mFragmentId=#7f070000 mContainerId=#7f070000 mTag=null
mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=2
mAdded=false mRemoving=true mResumed=false mFromLayout=false mInLayout=false
mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
mRetainInstance=false mRetaining=false mUserVisibleHint=true
mFragmentManager=FragmentManager{41e4d9a0 in MainActivity{41e4d8a8}}
mActivity=com.dslomer64.fragments.MainActivity#41e4d8a8
mSavedViewState=
{2131165188=android.view.AbsSavedState$1#41acfcc0, 2131165189=TextView.SavedState{41ec9d10 start=0 end=0 text=3}, 2131165190=android.view.AbsSavedState$1#41acfcc0}
#1: OutputFragment{41ebf0f0 #1 id=0x7f070000}
mFragmentId=#7f070000 mContainerId=#7f070000 mTag=null
mState=0 mIndex=1 mWho=android:fragment:1 mBackStackNesting=1
mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
mRetainInstance=false mRetaining=false mUserVisibleHint=true
mFragmentManager=FragmentManager{41e4d9a0 in MainActivity{41e4d8a8}}
mActivity=com.dslomer64.fragments.MainActivity#41e4d8a8
Added Fragments:
#0: OutputFragment{41ebf0f0 #1 id=0x7f070000}
Back Stack:
#0: BackStackEntry{41e6d288 #0}
mName=null mIndex=0 mCommitted=true
Operations:
Op #0: REPLACE InputFragment{41e6d1d8 #0 id=0x7f070000}
Back Stack Indices:
#0: BackStackEntry{41e6d288 #0}
#1: BackStackEntry{41ebf8d0 #1}
FragmentManager misc state:
mActivity=com.dslomer64.fragments.MainActivity#41e4d8a8
mContainer=android.app.Activity$1#41e4da18
mCurState=5 mStateSaved=false mDestroyed=false
ViewRoot:
mAdded=true mRemoved=false
mConsumeBatchedInputScheduled=false
mPendingInputEventCount=0
mProcessInputEventsScheduled=false
mTraversalScheduled=false
android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
Choreographer:
mFrameScheduled=false
mLastFrameTime=20289147 (31 ms ago)
View Hierarchy:
com.android.internal.policy.impl.PhoneWindow$DecorView{41e50e28 V.E..... R....... 0,0-800,1216}
com.android.internal.widget.ActionBarOverlayLayout{41e51e40 V.ED.... ........ 0,0-800,1216 #1020317 android:id/action_bar_overlay_layout}
android.widget.FrameLayout{41e53548 V.E..... ......I. 0,108-800,1216 #1020002 android:id/content}
android.widget.LinearLayout{41ec5d60 V.E..... ......I. 0,0-800,1108}
android.widget.TextView{41ec6080 V.ED.... ........ 0,0-14,101 #7f070007 app:id/txvIn_output_fragment}
com.android.internal.widget.ActionBarContainer{41e53a68 V.ED.... ........ 0,33-800,108 #1020318 android:id/action_bar_container}
com.android.internal.widget.ActionBarView{41e565d0 V.E..... ........ 0,0-800,75 #1020319 android:id/action_bar}
android.widget.LinearLayout{41e56d40 V.....C. ........ 11,0-248,75}
com.android.internal.widget.ActionBarView$HomeView{41e5abe0 V.E..... ........ 0,0-75,75}
android.widget.ImageView{41e5b0b0 G.ED.... ......I. 0,0-0,0 #102025a android:id/up}
android.widget.ImageView{41e5c1a8 V.ED.... ........ 5,5-69,69 #102002c android:id/home}
android.widget.LinearLayout{41e5d680 V.E..... ........ 75,21-237,54}
android.widget.TextView{41e5db28 V.ED.... ........ 0,0-151,33 #1020265 android:id/action_bar_title}
android.widget.TextView{41e5eb40 G.ED.... ......I. 0,0-0,0 #1020266 android:id/action_bar_subtitle}
com.android.internal.view.menu.ActionMenuView{41ea45b0 V.ED.... ......ID 800,0-800,75}
com.android.internal.widget.ActionBarContextView{41e5f438 G.E..... ......ID 0,0-0,0 #102031a android:id/action_context_bar}
com.android.internal.widget.ActionBarContainer{41e61e58 G.ED.... ......ID 0,0-0,0 #102031b android:id/split_action_bar}
Looper (main, tid 1) {41e42ba0}
(Total messages: 0, idling=false, quitting=false)
id=1: thread exiting with uncaught exception (group=0x41905cf8)
id=1: uncaught exception occurred
.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:698)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
id=1: calling UncaughtExceptionHandler
FATAL EXCEPTION: main
Process: com.dslomer64.fragments, PID: 24665
java.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:698)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
The code below produces the desired activity. WITH Fragments.
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
public static boolean PORTRAIT_MODE;
Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.main);
PORTRAIT_MODE = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT );
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (PORTRAIT_MODE)
{
EditText inputP = (EditText)findViewById(R.id.input_in_main_portrait);
Intent outputActivity;
outputActivity = new Intent(mContext, OutputActivity.class);
outputActivity.putExtra("numberOfLetters", inputP.getText().toString());
startActivity(outputActivity);
} else
{
EditText inputL = (EditText)findViewById(R.id.input_in_main_portrait);
TextView outputL = (TextView)findViewById(R.id.txvIn_output_fragment);
outputL.setText(DoStuff.createOutput(Integer.parseInt(inputL.getText().toString())));
}
}
});
}
}
DoStuff.java
public class DoStuff
{
public static String createOutput(int k){
String s = "";
for(int i = 0; i < k; i++)
s += "\n" + (char)(i+65);
return s;
}
}
OutputActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class OutputActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent i = getIntent();
String s = i.getStringExtra("numberOfLetters");
Log.w("OutputActivity=====", "`````TRYING to be Displaying " + s + " letters in what? Portrait? " + MainActivity.PORTRAIT_MODE);
setContentView(R.layout.output_fragment);
TextView et = (TextView) findViewById(R.id.txvIn_output_fragment);
et.setText(DoStuff.createOutput(Integer.parseInt(s)));
}
}
InputFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class InputFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
super.onCreateView(inflater, parent, savedInstanceState);
return inflater.inflate(R.layout.input_fragment, parent, false);
}
}
OutputFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OutputFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
super.onCreateView(inflater, parent, savedInstanceState);
return inflater.inflate(R.layout.output_fragment, parent, false);
}
}
input_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id ="#+id/tvIn_main_portrait" android:layout_width ="wrap_content" android:layout_height="wrap_content"
android:text="How many?"
/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/input_in_main_portrait"
android:text="4"
/>
<Button
android:id="#+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Press to get output"
/>
</LinearLayout>
output_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:id="#+id/txvIn_output_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Output will go here in OutputFragment"
/>
</LinearLayout>
layout\main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/main_fragment"
android:name="com.dslomer64.fragthis.InputFragment"
/>
</LinearLayout>
layout-land\main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_weight="3"
>
<fragment android:layout_width="wrap_content" android:layout_height="match_parent"
android:id="#+id/input_fragment"
android:name="com.dslomer64.fragthis.InputFragment"
/>
<fragment android:layout_width="wrap_content" android:layout_height="match_parent"
android:id="#+id/output_fragment"
android:name="com.dslomer64.fragthis.OutputFragment"
/>
</LinearLayout>

NullPointerException using TabHost in Dialog? How to do it right?

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

Android Java error:null pointer exception can't see why?

Hi I have an app written that goes from a login page, to another activity with a progress bar, then to another activity with a tab host. If I get rid of the loading page and go from login to the tab host works fine, but if I try to go from login to loading the app stops and says 'Unfortunately application has stopped.' I have checked the logcat and see that there is a null pointer error for the Loading page, but I can't see why. In the intent to go from login to loading I call loading correctly, and the set content view in Loading matches its xml loading_main. Here is my code below, thannks:
Main login activity:
package com.example.loginscreen;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("mara") &&
password.getText().toString().equals("mara")){
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,Loading.class));
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Loading activity:
package com.example.loginscreen;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class Loading extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.button1);
btnStartProgress.setOnClickListener( // <== This is line 33
new OnClickListener() {
#Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Searching for Driver Card...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// driver card is found
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
});
startActivity(new Intent(Loading.this,LinkTabs.class));
}
});
}
// file download simulator
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
}
}
return 100;
}
}
Activity LinkTabs to link all tabs in tab host:
package com.example.loginscreen;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class LinkTabs extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link_main);
Resources ressources = getResources();
TabHost tabHost = getTabHost();
// First tab
Intent intentAndroid = new Intent().setClass(this, HomePage.class);
TabSpec tabSpecHomePage = tabHost
.newTabSpec("HomePage")
.setIndicator("", ressources.getDrawable(R.drawable.overview))
.setContent(intentAndroid);
// Second tab
Intent intentApple = new Intent().setClass(this, HomePage2.class);
TabSpec tabSpecHomePage2 = tabHost
.newTabSpec("HomePage2")
.setIndicator("", ressources.getDrawable(R.drawable.card_summary))
.setContent(intentApple);
// Third tab
Intent intentWindows = new Intent().setClass(this, HomePage3.class);
TabSpec tabSpecHomePage3 = tabHost
.newTabSpec("HomePage3")
.setIndicator("", ressources.getDrawable(R.drawable.details))
.setContent(intentWindows);
// add all tabs
tabHost.addTab(tabSpecHomePage);
tabHost.addTab(tabSpecHomePage2);
tabHost.addTab(tabSpecHomePage3);
//set Windows tab as default (zero based)
tabHost.setCurrentTab(2);
}
}
Tab activities:
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Overview");
setContentView(textview);
}
}
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Card Summary");
setContentView(textview);
}
}
package com.example.loginscreen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HomePage3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Details");
setContentView(textview);
}
}
Here are the xml files:
//activity_main.xml
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="#+id/textView2"
android:ems="10"
tools:ignore="TextFields" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_marginTop="40dp"
android:layout_toLeftOf="#+id/editText1"
android:hint="Password"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/button1"
android:text="TextView"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignParentTop="true"
android:layout_marginTop="155dp"
android:hint="Username"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentTop="true"
android:src="#drawable/tranzlogo1" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_marginTop="26dp"
android:layout_toLeftOf="#+id/textView5"
android:text="#string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/textView4" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:onClick="login"
android:text="#string/Login" />
</RelativeLayout>
//fragment_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.loginscreen.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/tranzlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="#drawable/overview"
tools:ignore="ContentDescription" />
</RelativeLayout>
//link_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.loginscreen.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/tranzlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="#drawable/overview"
tools:ignore="ContentDescription" />
</RelativeLayout>
//loading_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Here is loading_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Here is the mainfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.loginscreen.MainActivity"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomePage" />
<activity android:name=".HomePage2" />
<activity android:name=".HomePage3" />
<activity android:name=".Loading"/>
<activity android:name=".LinkTabs"/>
</application>
</manifest>
And the logcat output:
06-20 10:22:15.289: W/dalvikvm(29524): threadid=1: thread exiting with uncaught exception (group=0x40cca318)
06-20 10:22:15.299: E/AndroidRuntime(29524): FATAL EXCEPTION: main
06-20 10:22:15.299: E/AndroidRuntime(29524): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.Loading}: java.lang.NullPointerException
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.access$600(ActivityThread.java:134)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.os.Looper.loop(Looper.java:137)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.main(ActivityThread.java:4744)
06-20 10:22:15.299: E/AndroidRuntime(29524): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 10:22:15.299: E/AndroidRuntime(29524): at java.lang.reflect.Method.invoke(Method.java:511)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-20 10:22:15.299: E/AndroidRuntime(29524): at dalvik.system.NativeStart.main(Native Method)
06-20 10:22:15.299: E/AndroidRuntime(29524): Caused by: java.lang.NullPointerException
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.example.loginscreen.Loading.addListenerOnButton(Loading.java:33)
06-20 10:22:15.299: E/AndroidRuntime(29524): at com.example.loginscreen.Loading.onCreate(Loading.java:26)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.Activity.performCreate(Activity.java:5008)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-20 10:22:15.299: E/AndroidRuntime(29524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
06-20 10:22:15.299: E/AndroidRuntime(29524): ... 11 more
You have referenced in Your LoadingMain activity the following layout with setContentView:
setContentView(R.layout.loading_main);
But this Layout does not have any button1. Instead, You are trying to reference a button from another layout, from activity_main.xml
btnStartProgress = (Button) findViewById(R.id.button1);
This button1 is inside the activity_main.xml and You cannot reference a button in a layout that is not attached per setContentView. So What You have to do is, add a button to the loading_main.xml, BUT, don´t give it the same id like in the activity main "button1". Sure, it works, to give same ids as long as the right layout is referenced, but id´s should be unique. If the button in the activity_layout should remain, give the id for your button inside loading_main.xml another name, maybe android:id="#+id/loading_main_button_1".
Clearly the error is in xml, you have not added the button here:
<?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="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
</LinearLayout>
Try adding the following in your loading_main.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
The complete thing should look like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="413dp"
android:layout_height="382dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
NullPointerExcception
it causes because using null object or not defining value to object.
In Loading.java you have used
setContentView(R.layout.loading_main);
that don't have button1
add button1 in loading_main.xml it or Change layout in setContentView(R.layout.other_layout);

Android admob fatal exception

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.

Generate bitmap from GestureOverlayView

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

Categories

Resources