Unfortunately app stopped work - java

As I press the "Start Activity" button in my app it won't working and forced to stop its working.
Here is 1st xml file (get.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/etget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Enter Your Gender" />
<Button
android:id="#+id/bget1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/etget"
android:text="Start Activity" />
<Button
android:id="#+id/bget2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/etget"
android:layout_toLeftOf="#id/bget1"
android:text="Start Activity for Result " />
<TextView
android:id="#+id/tvget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bget2"
android:text="TextView" />
</RelativeLayout>
Here is java file, Get.java:
package example.katta;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Get extends Activity implements OnClickListener {
Button bg1, bg2;
TextView tv;
TextView etg;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
etg = (TextView) findViewById(R.id.etget);
bg1 = (Button) findViewById(R.id.bget1);
bg2 = (Button) findViewById(R.id.bget2);
tv = (TextView) findViewById(R.id.tvget);
bg1.setOnClickListener(this);
bg2.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bget1:
String bread = etg.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(Get.this, Send.class);
a.putExtras(basket);
startActivity(a);
break;
case R.id.bget2:
break;
}
}
}
Here is 2nd xml file(send.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" >
<TextView
android:id="#+id/etsend"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
<RadioGroup
android:id="#+id/rgsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb1send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="#+id/rb2send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<TextView
android:id="#+id/tvsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/buttonsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
Here is java file(Send.java) :
package example.katta;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class Send extends Activity implements OnClickListener, OnCheckedChangeListener {
Button bts;
TextView tvs,ets;
RadioGroup selection;
String gotbread;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
Initialize();
Bundle gotbasket = getIntent().getExtras();
gotbread = gotbasket.getString("key");
tvs.setText(gotbread);
}
private void Initialize() {
// TODO Auto-generated method stub
ets = (TextView) findViewById(R.id.etsend);
bts = (Button) findViewById(R.id.buttonsend);
tvs = (TextView) findViewById(R.id.tvsend);
selection = (RadioGroup) findViewById(R.id.rgsend);
bts.setOnClickListener(this);
selection.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rb1send:
break;
case R.id.rb2send:
break;
}
}
}

The error is:
android.content.ActivityNotFoundException: Unable to find explicit activity class {example.katta/example.katta.Send}; have you declared this activity in your AndroidManifest.xml?
The solution is to add Send Activity in your AndroidManifest.xml:
<activity
android:name=".Send" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

You should declarate every activities in your manifest, also I think you can create a better code.
I'll give some recommendations to help you.
1- You can explicit in naming their views.
<TextView
android:id="#+id/txt_view_send"
... >
</TextView>
2- Use camelNotation java code
Bundle gotBasket ...
Future large projects is of great help!
3- Use match_parent un your views
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent.
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
4- Bundle is used only when you have many arguments. You can do something like this in your example.
//you can add a variable to get editText value or pass direct the value in to putExtras()
String bread = etg.getText().toString();
Intent a = new Intent(Get.this, Send.class);
a.putExtras("key", bread);
startActivity(a);
http://developer.android.com/reference/android/content/Intent.html
4-validating can help avoid exceptions.
//example
if(getIntent().getExtras() != null){
tvs.setText(getIntent().getExtras().getString("key"));
}
//your example
Bundle gotbasket = getIntent().getExtras();
if(gotbasket != null){
gotbread = gotbasket.getString("key");
if(gotbread != null{
tvs.setText(gotbread);
}
It can produce NullPointerException.
I hope it helps you!!
Cheers
programming continues!

Related

Error while starting an activity from a nested Fragment

I am trying to start a new activity from within a nested fragment using the following code :
Intent rateIntent = new Intent(context, RateServiceActivity.class);
getActivity().startActivity(rateIntent);
I am getting the following error :
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.serve.learn/com.serve.learn.MyOrdersPage.RateServiceActivity}:
android.view.InflateException: Binary XML file line #31: Error inflating class fragment
I have tried all the solutions provided online with no luck. Also, I think this error is specific to nested fragments scenario for me and not the generic errors mentioned in other posts.
There shouldn't be any issues with my xml files because I did not face any issues till now. Only when I am trying to start a new activity from within a nested fragment I am getting this error.
Anyway, this is code in my activity_main.xml (starting from line #31) :
<fragment
android:id="#+id/navigation_drawer"
android:name="com.serve.learn.MainActivityPage.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" />
Here is the nested fragment code :
package com.serve.learn.MyOrdersPage;
import com.serve.learn.R;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Fragment_Fragment_UpcomingOrders extends Fragment implements OnClickListener{
TextView order1Details, order1, order2Details, order2, order3Details, order3;
View rootView;
Button callButton, rateButton;
Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_fragment_upcoming_orders, container, false);
context = rootView.getContext();
order1Details = (TextView) rootView.findViewById(R.id.order1Details);
order1Details.setVisibility(View.GONE);
order1 = (TextView) rootView.findViewById(R.id.order1);
order1.setOnClickListener(this);
order2Details = (TextView) rootView.findViewById(R.id.order2Details);
order2Details.setVisibility(View.GONE);
order2 = (TextView) rootView.findViewById(R.id.order2);
order2.setOnClickListener(this);
order3Details = (TextView) rootView.findViewById(R.id.order3Details);
order3Details.setVisibility(View.GONE);
order3 = (TextView) rootView.findViewById(R.id.order3);
order3.setOnClickListener(this);
callButton = (Button) rootView.findViewById(R.id.callButton);
callButton.setOnClickListener(this);
rateButton = (Button) rootView.findViewById(R.id.rateButton);
rateButton.setOnClickListener(this);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.order1 : order1Details.setVisibility( order1Details.isShown()? View.GONE:View.VISIBLE );
break;
case R.id.order2 : order2Details.setVisibility( order2Details.isShown()? View.GONE:View.VISIBLE );
break;
case R.id.order3 : order3Details.setVisibility( order3Details.isShown()? View.GONE:View.VISIBLE );
break;
case R.id.callButton : Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
break;
case R.id.rateButton : Intent rateIntent = new Intent(context, RateServiceActivity.class);
getActivity().startActivity(rateIntent);
break;
}
}
}
Code for fragment_fragment_upcoming_orders.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal" >
<!-- activity_info layout file -->
<!-- Clickable title -->
<TextView
android:id="#+id/order1"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="#string/Order1" />
<!--content to hide/show -->
<TextView
android:id="#+id/order1Details"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/Order1Details" />
<TextView
android:id="#+id/order2"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="#string/Order2" />
<TextView
android:id="#+id/order2Details"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/Order2Details" />
<TextView
android:id="#+id/order3"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="#string/Order3" />
<TextView
android:id="#+id/order3Details"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/Order3Details" />
<Button
android:id="#+id/callButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />
<Button
android:id="#+id/rateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate" />
</LinearLayout>
Any suggestions as to where I might be going wrong?

How to handle retrive a data from two bundle in single form

Hello everyone if you would kindly help me. I'm stuck on how to retrieve the bundle data from another activity. Basically I have two activities which is that when I pressed a button on the first activity, it will go on the second activity and then sets the string values which is later on to be passed on the first activity. What I did was I used the bundle to put the string values. Here i have 3 forms. I am sending bundle values to second from from 1st form and returning bundle values from 3rd from to second from.
My question is that how can I handle the two bundle(activity) in 2nd form.
In first activity on button click you can do :
Intent in = new Intent(activtiy1, secondact.class);
Bundle b = new Bundle();
b.putString("key", "string_to_pass");
.
.
.
in.putextras(b);
startActivity(in);
Now in Second activity you have to get bundle items :
Bundle b=getIntent().getExtras();
String str = b.getString("key");
Hope it helps.
you have first create ui for first activity like this
<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=".FirstActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="77dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="110dp"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/button1"
android:text="Button 2" />
</RelativeLayout>
ui of second activity like this
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second actvity" />
</LinearLayout>
activity first code where i set index with intent for both button click(you also put string double ling and message with intent)
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id){
case R.id.button1:
Intent i1=new Intent(FirstActivity.this,secondactivity.class);
i1.putExtra("one", 1);
startActivity(i1);
break;
case R.id.button2:
Intent i2=new Intent(FirstActivity.this,secondactivity.class);
i2.putExtra("one", 2);
startActivity(i2);
break;
}
}
}
and second activity i get that integer value to identify which button is clicked and fire operation on that condition
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class secondactivity extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
tv=(TextView)findViewById(R.id.textView1);
Bundle extras = getIntent().getExtras();
int index = extras.getInt("one");
if(index==1){
tv.setText("nformation-A "+index);
}else if(index==2){
tv.setText("nformation-B "+index);
}
}
}

How to load Myanmar Language in Web View in android?

I have a web view in which i will enter the URL and load that content to that web view.
activity_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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:visibility="gone" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/urlField"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:ems="10"
android:inputType="textUri"
android:imeOptions="actionDone"
android:hint="http://proteam.in" />
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/layout"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:onClick="open"
android:text="#string/browse" />
</LinearLayout>
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_below="#+id/layout" />
</RelativeLayout>
MainActivity.java
package com.example.mayanmarbroswer;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText field;
private WebView browser;
private ProgressDialog pDialog;
final Context context = this;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
field = (EditText)findViewById(R.id.urlField);
browser = (WebView)findViewById(R.id.webView1);
browser.setWebViewClient(new MyBrowser());
//myTextView.setTypeface(typeFace);
WebSettings webSettings = browser.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setFixedFontFamily("file:///android_asset/font/Sanpya.ttf");
ActionBar bar = getActionBar();
bar.hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
field.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE)
{
if(field.getText().toString().trim().equals(""))
{
Toast.makeText(getApplicationContext(), "Enter Valid WebSite", Toast.LENGTH_SHORT).show();
}
else
{
open(v);
}
}
return false;
}
});
}
public void open(View view){
String url;
if(field.getText().toString().trim().equals(""))
{
Toast.makeText(getApplicationContext(), "Enter Valid WebSite", Toast.LENGTH_SHORT).show();
}
else
{
if(field.getText().toString().startsWith("http://"))
{
url = field.getText().toString();
}
else
{
url = "http://" + field.getText().toString();
}
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(url);
}
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
/*pDialog = new ProgressDialog(context, ProgressDialog.THEME_HOLO_DARK);
// set indeterminate style
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// set title and message
//pDialog.setTitle("Please wait");
pDialog.setMessage("Loading Pls Wait");
// and show it
pDialog.show();*/
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//do what you want to do
/*if (pDialog.isShowing())
{
pDialog.dismiss();
}*/
}
}
#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;
}
}
URL 1: http://www.mora.gov.mm/
URL 2: http://www.saikwan.info/
i tested this in few android device like moto e (4.4.2), Dell venue 7 (4.4.2), Samsung Duos GT-s7392 (4.1.2) and Videocon z40pro(4.2.2) the above given URL with myanmar font is working correctly. but when i try to load that in Samsung GT-s7582(4.2.2) the myanmar fonts are missing in web view. so, please let me know how to solve this issue.
If you fetch data from resource folder i mean asset folder then try this way.
Java Code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer);
Button buttonPlayVideo1 = (Button)findViewById(R.id.button1);
Button acceess = (Button)findViewById(R.id.button2);
getWindow().setFormat(PixelFormat.UNKNOWN);
//Displays a video file.
buttonPlayVideo1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// VideoView refference see main.xml
VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
//String uriPath = "android.resource://com.android.AndroidVideoPlayer/"+R.raw.k;
String uriPath = "android.resource://com.android.AndroidVideoPlayer/"+R.raw.demo;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
}
});
If you fetch from URL or FTP Server OR HTTP Connection then try this way
Java Page
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://192.168.0.8/aeon/")); // For Video Content
// new Intent(Intent.ACTION_VIEW, Uri.parse("ftp://movies:movies#192.168.0.8/Transport_Streams/"));
startActivity(browserIntent);
}});
acceess.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(AndroidVideoPlayer.this,Videopage.class);
startActivity(i);
}
});
}
XML Code Of Both Way are is Given Following way
XML Page for when fetch from the resource
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF8800"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Welcome To Aeon Group"
android:textColor="#9933CC"
android:textSize="3mm" />
<VideoView
android:id="#+id/videoview"
android:layout_width="fill_parent"
android:layout_height="394dp"
android:layout_above="#+id/playvideoplayer"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/playvideoplayer"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:background="#drawable/button"
android:text="Aeon Content" />
<!-- <VideoView
android:id="#+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/playvideoplayer"
android:layout_below="#+id/textView1" />
-->
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/playvideoplayer"
android:layout_alignBottom="#+id/playvideoplayer"
android:layout_alignParentLeft="true"
android:background="#drawable/button"
android:text="Play" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/playvideoplayer"
android:layout_alignBottom="#+id/playvideoplayer"
android:layout_toRightOf="#+id/playvideoplayer"
android:background="#drawable/button"
android:text="Acces" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Button click force closes the activity

My problem is when I click bSA button I encounter error and the activity closes.
java.lang.runtimeexception unable to start activity componentinfo ...
here is my code
Data.java :
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Data extends Activity implements View.OnClickListener {
Button start, startFor;
EditText sendET;
TextView gotAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
initialize();
}
private void initialize() {
start = (Button) findViewById(R.id.bSA);
startFor = (Button) findViewById(R.id.bSAFR);
sendET = (EditText) findViewById(R.id.etSend);
gotAnswer = (TextView) findViewById(R.id.tvGot);
start.setOnClickListener(this);
startFor.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bSA:
String bread = sendET.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
startActivity(a);
break;
case R.id.bSAFR:
break;
}
}
}
get.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etSend" android:layout_gravity="center_horizontal"/>
<Button
android:layout_below="#+id/etSend"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity"
android:id="#+id/bSA"/>
<Button
android:layout_toLeftOf="#+id/bSA"
android:layout_alignTop="#+id/bSA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity for Results"
android:id="#+id/bSAFR"/>
<TextView
android:layout_below="#+id/bSAFR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:id="#+id/tvGot"/>
</RelativeLayout>
=============================================================
OpenedClass.java
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
public class OpenedClass extends Activity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
TextView question, test;
Button returnData;
RadioGroup selectionList;
String gotBread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
initialize();
Bundle gotBasket = getIntent().getExtras();
gotBread = gotBasket.getString("key");
question.setText(gotBread);
}
private void initialize() {
question = (TextView) findViewById(R.id.tvQuestion);
test = (TextView) findViewById(R.id.tvText);
returnData = (Button) findViewById(R.id.bReturn);
returnData.setOnClickListener(this);
selectionList = (RadioGroup) findViewById(R.id.rgAnswers);
selectionList.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View view) {
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rCrazy:
break;
case R.id.rFun:
break;
case R.id.rBoth:
break;
}
}
}
send.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Hosein is ..."
android:id="#+id/tvQuestion"/>
<RadioGroup
android:id="#+id/rgAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crazy"
android:id="#+id/rCrazy"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Super Fun"
android:id="#+id/rFun"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Both"
android:id="#+id/rBoth"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return"
android:id="#+id/bReturn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tvText"/>
</LinearLayout>
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
You cannot put a inside itself. This causes infinite recursion and stack overflow.
You probably wanted
a.putExtras(basket);

setOnClickListener causes force close in Android

I am trying to create an onClickListener for a button. Everything shows up fine before I create the event listener, but force closes when I add it in.
Here is the code:
package com.austin.mobile;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TxtLingoActivity extends Activity {
/** Called when the activity is first created. */
Button convert;
EditText inputText, outputText;
String myCopy;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
convert = (Button) findViewById(R.id.inputTextBox);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here is the xml:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/inputTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/convert"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Convert" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/outputTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Log Cat is showing this error:
03-19 18:05:21.726: E/dalvikvm(662): Unable to open stack trace file'/data/anr/traces.txt': Permission denied
Any suggestions?
Looks like you are referring to the incorrect id when 'finding' the Button:
Try to change this:
convert = (Button) findViewById(R.id.inputTextBox);
to this:
convert = (Button) findViewById(R.id.convert);
Hope this helps...
its ClassCastException in this line convert = (Button) findViewById(R.id.inputTextBox);
Because R.id.inputTextBox is EditText and you use as Button .change it in R.id.convert

Categories