ERROR :
02-19 21:28:52.920: E/AndroidRuntime(17041): FATAL EXCEPTION: main
02-19 21:28:52.920: E/AndroidRuntime(17041): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gamecik/com.example.gamecik.MainActivity}: java.lang.ClassCastException: com.google.android.gms.ads.AdView cannot be cast to android.widget.LinearLayout
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.access$700(ActivityThread.java:165)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.os.Looper.loop(Looper.java:137)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.main(ActivityThread.java:5450)
02-19 21:28:52.920: E/AndroidRuntime(17041): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 21:28:52.920: E/AndroidRuntime(17041): at java.lang.reflect.Method.invoke(Method.java:525)
02-19 21:28:52.920: E/AndroidRuntime(17041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-19 21:28:52.920: E/AndroidRuntime(17041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-19 21:28:52.920: E/AndroidRuntime(17041): at dalvik.system.NativeStart.main(Native Method)
02-19 21:28:52.920: E/AndroidRuntime(17041): Caused by: java.lang.ClassCastException: com.google.android.gms.ads.AdView cannot be cast to android.widget.LinearLayout
02-19 21:28:52.920: E/AndroidRuntime(17041): at com.example.gamecik.MainActivity.onCreate(MainActivity.java:32)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.Activity.performCreate(Activity.java:5369)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-19 21:28:52.920: E/AndroidRuntime(17041): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
When I use Admob code my application is error..if I delete Admob code my application is good..
PLEASE HELP ME...
Sorry that this question hasn't given much information, I just don't know what information to give to you (if that makes sense?) Please respond and tell me what information you would need and thank you for any and all of your help
HERE IS MY CODE:
MY ACTIVITY JAVA
package com.example.gamecik;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
private AdView adView;
private static final String AD_UNIT_ID = "ca-app-pub-1715534499344773/3123443647";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this);
adView.setAdUnitId(AD_UNIT_ID);
adView.setAdSize(AdSize.BANNER);
LinearLayout layout = (LinearLayout)findViewById(R.id.adView);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
// Add click listeners to all the ImageButtons
View button1 = findViewById(R.id.sescalbuton);
View button2 = findViewById(R.id.magnumsilah);
View button3 = findViewById(R.id.Button03);
View button4 = findViewById(R.id.Button02);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
#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;
}
// Required method if OnClickListener is implemented
#Override
public void onClick(View v) {
// Find which ImageButton was pressed and take appropriate action
switch(v.getId()){
// The cow button
case R.id.sescalbuton:
mp = MediaPlayer.create(this, R.raw.ak47);
break;
// The duck button
case R.id.magnumsilah:
mp = MediaPlayer.create(this, R.raw.magnum);
break;
// The sheep button
case R.id.Button03:
mp = MediaPlayer.create(this, R.raw.shotgun);
break;
// The uzi
case R.id.Button02:
mp = MediaPlayer.create(this, R.raw.uzi);
break;
}
mp.seekTo(0);
mp.start();
}
}
MY XML CODE:
<RelativeLayout 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.android.gms.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wood"
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=".MainActivity" >
<Button
android:id="#+id/sescalbuton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/shotgun"
android:layout_below="#+id/magnumsilah"
android:background="#drawable/ak47" />
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sescalbuton"
android:layout_centerHorizontal="true"
android:background="#drawable/sohtgun" />
<Button
android:id="#+id/Button02"
android:layout_width="150dp"
android:layout_height="110dp"
android:layout_above="#+id/sescalbuton"
android:layout_alignRight="#+id/sescalbuton"
android:background="#drawable/guns" />
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub-1715534499344773/3123443647"
ads:adSize="BANNER"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/adView"
android:layout_alignTop="#+id/adView"
android:layout_marginTop="28dp"
android:src="#drawable/supperweapon" />
<Button
android:id="#+id/magnumsilah"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_alignLeft="#+id/imageView1"
android:layout_below="#+id/imageView1"
android:background="#drawable/magnum" />
</RelativeLayout>
PLEASE HELP ME...WHAT IS PROBLEM ?
If I understood correctly, casting is not your only issue. You're trying to add AdView to a LinearLayout that doesn't exist in your activities XML. But instead in your XML you've added an AdView and your initiating an AdView dynamically as well, which doesn't make any sense. Change your onCreate(..) code to the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = (AdView) findViewById(R.id.adView);
adView.setAdUnitId(AD_UNIT_ID);
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
//.... rest of your onCreate() code
}
Look for whatever R.id.adview is in your layout file. It's not a LinearLayout but you're trying to cast it as one. That's why it's crashing. Probably is an AdView?
The problem seems to be here,
LinearLayout layout = (LinearLayout)findViewById(R.id.adView);
where you are I believe trying to cast your AdView layout to a linear layout.
Change R.id.adView to the id of your LinearLayout. Post your layout xml too if this is not he case.
Try to clean the project after changing the place. This worked for me!
Related
I'm trying to make a simple web browser for my android development class and every time I try to run the app I get the message "Unfortunately, Navegador has stopped".
I followed my teacher's code and everyone in my class could run it without problems. I'm starting to think the problem might be on my computer.
I'm using Eclipse. Some variables on the code are in spanish.
Here's my activity_main.xml
Basically it has 4 buttons
Atras (Backward)
Adelante (Forward)
Actualizar (Refresh)
Ir (Go to website)
<Button
android:id="#+id/btAtras"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="3"
android:text="Atras"
/>
<Button
android:id="#+id/btAdelante"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="3"
android:text="Adelante"
/>
<Button
android:id="#+id/btActualizar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="3"
android:text="Actualizar"
/>
Here's my MainActivity.java
package com.curso.navegador;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText navegador;
Button ir, atras, adelante, actualizar, borrar;
WebView sitio;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sitio = (WebView) findViewById (R.id.wvNavegador);
sitio.setWebViewClient(new ViewClient());
sitio.getSettings().setJavaScriptEnabled(true);
sitio.getSettings().setLoadWithOverviewMode(true);
sitio.getSettings().setUseWideViewPort(true);
navegador = (EditText) findViewById (R.id.wvNavegador);
ir =(Button) findViewById (R.id.btIr);
atras =(Button) findViewById (R.id.btAtras);
adelante =(Button) findViewById (R.id.btAdelante);
actualizar = (Button) findViewById (R.id.btActualizar);
ir.setOnClickListener(this);
atras.setOnClickListener(this);
adelante.setOnClickListener(this);
actualizar.setOnClickListener(this);
borrar.setOnClickListener(this);
sitio.loadUrl("");
}
#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;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btIr:
String web = navegador.getText().toString();
sitio.loadUrl(web);
break;
case R.id.btAtras:
if(sitio.canGoBack())
sitio.goBack();
break;
case R.id.btAdelante:
if(sitio.canGoForward())
sitio.goForward();
break;
case R.id.btActualizar:
sitio.reload();
break;
}
}
}
And we added this additional ViewClient.java class
package com.curso.navegador;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ViewClient extends WebViewClient{
public boolean shouldOverrideUrlLading(WebView v, String url){
v.loadUrl(url);
return true;
}
}
In addition I added the permission on android manifest
android.permission.INTERNET
03-11 12:07:15.289: E/AndroidRuntime(27901): FATAL EXCEPTION: main
03-11 12:07:15.289: E/AndroidRuntime(27901): Process: com.curso.navegador, PID: 27901
03-11 12:07:15.289: E/AndroidRuntime(27901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.curso.navegador/com.curso.navegador.MainActivity}: java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.EditText
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.os.Looper.loop(Looper.java:148)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.main(ActivityThread.java:5443)
03-11 12:07:15.289: E/AndroidRuntime(27901): at java.lang.reflect.Method.invoke(Native Method)
03-11 12:07:15.289: E/AndroidRuntime(27901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
03-11 12:07:15.289: E/AndroidRuntime(27901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-11 12:07:15.289: E/AndroidRuntime(27901): Caused by: java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.EditText
03-11 12:07:15.289: E/AndroidRuntime(27901): at com.curso.navegador.MainActivity.onCreate(MainActivity.java:30)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.Activity.performCreate(Activity.java:6245)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
03-11 12:07:15.289: E/AndroidRuntime(27901): ... 9 more
It's getting really frustrating because I'm getting this same type of error every time I start a new android application and this didn't happen before and I haven't found a solution yet.
I would appreciate very much your help.
The stack trace is telling you what the problem is. You are trying to cast a WebView to an EditText. Based on what you have provided its this line
navegador = (EditText) findViewById (R.id.wvNavegador);
You are first casting wvNavegador to a webview few lines above and then to an edit text, which is obviously a problem.
This question already exists:
android.view.InflateException: Binary XML file line #16: Error inflating class fragment
Closed 6 years ago.
I made an app, it works well in virtual device and real device, but today my app is throwing exceptions, maybe it is the same with some old errors in here, but I can not find my own answer for this. So, please tell me how to solve it, thanks so much
here is my logcat
FATAL EXCEPTION: main
Process: com.gvc.tvschedule, PID: 1918
android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 48 more
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f0b0068}
at android.content.res.Resources.loadDrawable(Resources.java:2073)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.<init>(TextView.java:806)
at android.widget.TextView.<init>(TextView.java:618)
... 51
here under is Layout XML: getprogram_main.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">
<RelativeLayout
android:id="#+id/relativeLayoutProgram"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayoutProgram"
android:orientation="vertical"
android:layout_marginTop="40dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</ListView>
<!-- #android:id/list or #id/android:list -->
</RelativeLayout>
</RelativeLayout>
and: view_program_entry.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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/programtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="15sp"
android:paddingLeft="6sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/programtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:drawableLeft="#+id/programtime" />
<!-- android:background="#color/blue2" -->
</LinearLayout>
and Java : ProgramPickerActivity.java
package com.gvc.tvschedule;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.gvc.service.DBController;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ProgramPickerActivity extends Activity {
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
private static String titleTextFromView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.getprogram_main);
ArrayList<HashMap<String, String>> programList = controller.generalProgram(MainActivity.getNameOfChannel(), DatePickerActivity.getDate());
if (programList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), programList, R.layout.view_program_entry, new String[] {
"ptime", "ptitle" }, new int[] { R.id.programtime, R.id.programtitle });
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView titleVIew = (TextView) view.findViewById(R.id.programtitle);
titleTextFromView = titleVIew.getText().toString();
createPopupWindownForDescription(titleTextFromView);
}
});
}
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("loading...");
prgDialog.setCancelable(false);
}
private void createPopupWindownForDescription(String pTitle){
String content = controller.getDescription(pTitle);
AlertDialog.Builder builder = new AlertDialog.Builder(ProgramPickerActivity.this);
builder.setTitle("Content");
builder.setMessage(content);
builder.show();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(prgDialog!=null)
prgDialog.dismiss();
}
public static String getTitleTextFromView(){
return titleTextFromView;
}
}
I think the trouble is related with the listview, because, when no data put on listview, it is fine, but with data, it threw the errors.
I believe your current problem is
<TextView
android:id="#+id/programtitle"
...
android:drawableLeft="#+id/programtime" /> // HERE
You are referencing the other TextView but you should be referencing a Drawable. My guess is that you want to put it to the left of the other TextView?
If this is the case, the LinearLayout will lay them out from left to right by default so you just need to put them in the order that you want them to appear. Also, not a problem but since they are left to right by default, there's no need for
android:orientation="horizontal"
It's not showing when you don't have any items in your ListView because the layout is never inflated so the error is never caught.
Docs
android:id="#+id/list"
use that, you're welcome
PS: Change
ListView myList = (ListView) findViewById(android.R.id.list);
for:
ListView myList = (ListView) findViewById(R.id.list);
I am new on android and trying to develop simple calculator but this Fatal exception : main occurs please help.
I did comment my onClickListner to check but it did'nt helped at all.
package com.example.calculatorsinglescreen;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText value1,value2,myoperator,result;
Button ok;
String strvalue1,strvalue2,stroperator,strresult;
int ivalue1,ivalue2,ioperator,iresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1 = (EditText) findViewById(R.id.txtvalue1);
value2 = (EditText) findViewById(R.id.txtvalue2);
myoperator = (EditText) findViewById(R.id.txtoperator);
result = (EditText) findViewById(R.id.txtresult);
ok = (Button) findViewById(R.id.btnok);
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (stroperator.equals("+")) {
Toast msg = Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG);
msg.show();
}
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my XML file :
<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.calculatorsinglescreen.MainActivity" >
<TextView
android:id="#+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:text="Value 1" />
<TextView
android:id="#+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/value1"
android:layout_margin="20dp"
android:text="Value 2" />
<TextView
android:id="#+id/operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/value2"
android:layout_margin="20dp"
android:text="Operator" />
<EditText
android:id="#+id/txtvalue1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/value1"
android:layout_alignBaseline="#+id/value1"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtoperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/operator"
android:layout_below="#+id/txtvalue2"
android:layout_toRightOf="#+id/operator"
android:layout_alignParentRight="true"
android:ems="10" />
<EditText
android:id="#+id/txtvalue2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/value2"
android:layout_toRightOf="#+id/value2"
android:layout_below="#+id/txtvalue1"
android:layout_alignParentRight="true"
android:ems="10" />
<Button
android:id="#+id/btnok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Ok" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnok"
android:layout_margin="40dp"
android:text="Result" />
<EditText
android:id="#+id/txtresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/result"
android:layout_toRightOf="#+id/result"
android:ems="10" />
Here is my LogCat to get a clearer view :
10-18 17:51:25.468: D/AndroidRuntime(742): Shutting down VM
10-18 17:51:25.468: W/dalvikvm(742): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-18 17:51:25.488: E/AndroidRuntime(742): FATAL EXCEPTION: main
10-18 17:51:25.488: E/AndroidRuntime(742): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculatorsinglescreen/com.example.calculatorsinglescreen.MainActivity}: java.lang.NumberFormatException: unable to parse '' as integer
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.os.Looper.loop(Looper.java:123)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-18 17:51:25.488: E/AndroidRuntime(742): at dalvik.system.NativeStart.main(Native Method)
10-18 17:51:25.488: E/AndroidRuntime(742): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.Integer.parseInt(Integer.java:362)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.Integer.parseInt(Integer.java:332)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.example.calculatorsinglescreen.MainActivity.onCreate(MainActivity.java:34)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-18 17:51:25.488: E/AndroidRuntime(742): ... 11 more
this is the error
java.lang.ClassCastException: android.widget.TextView
check your line 24 and see if you cast the right widget in java -(xml), this error means incompatible cast widget lol...
if everything seems correct-(mean widgets are cast in java to with their respective class) then clean rebuild and restart..
EDIT: It worked.. you've solved it but there is a new error which is
java.lang.NumberFormatException:
its because of this on these lines
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ioperator = Integer.parseInt(stroperator);
so change these to this
Integer.valueOf(strvalue1); do that as follows
and also looking at your codes.. your string values are pullled from the edittext during oncreate which means when the app starts in oncreate before the Onresume(which is called when the app shows on the), and during oncreate the user cant flirt with your app, and going further to input values, so at the end your strings are empty when you pull from the edittext, so basically what im saying is remove these lines
strvalue1 = value1.getText().toString();
and put them in the click events ... i am lucid enough to you??..
EDIT2: OVERALL CODE
for button click
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.valueOf(strvalue1);
ivalue2 = Integer.valueOf(strvalue2);
if (stroperator.equals("+")) {
Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG).show();
}
}
});
remove ioperator = Integer.parseInt(stroperator); from the code.I think Your trying to convert the operator to Integer.
ioperator = Integer.parseInt(stroperator);
if you mean operator like this +,-,/,* , you only can convert to thr char or stay in string and do something like this:
private int plus(int ivalue1 , int ivalue2 , String operator ){
if(operator.equal("+")){
return ivalue1 + ivalue2;
}
return 0;
}
I'm a total newbie with Android, and I'm trying to switch between two activities ("MainActivity" and "LoginDisplayActivity") and adding the "OnClickListener" seem to make my app crash on startup.
Here is my code :
package info.dremor.kronos;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new MainFragment()).commit();
}
final Button loginButton = (Button) findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginDisplayActivity.class);
startActivity(intent);
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class MainFragment extends Fragment {
public MainFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
trace :
05-26 15:48:11.286: D/AndroidRuntime(947): Shutting down VM
05-26 15:48:11.286: W/dalvikvm(947): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-26 15:48:11.296: E/AndroidRuntime(947): FATAL EXCEPTION: main
05-26 15:48:11.296: E/AndroidRuntime(947): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.dremor.kronos/info.dremor.kronos.MainActivity}: java.lang.NullPointerException
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.os.Looper.loop(Looper.java:137)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-26 15:48:11.296: E/AndroidRuntime(947): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 15:48:11.296: E/AndroidRuntime(947): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 15:48:11.296: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-26 15:48:11.296: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-26 15:48:11.296: E/AndroidRuntime(947): at dalvik.system.NativeStart.main(Native Method)
05-26 15:48:11.296: E/AndroidRuntime(947): Caused by: java.lang.NullPointerException
05-26 15:48:11.296: E/AndroidRuntime(947): at info.dremor.kronos.MainActivity.onCreate(MainActivity.java:31)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.Activity.performCreate(Activity.java:5008)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-26 15:48:11.296: E/AndroidRuntime(947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-26 15:48:11.296: E/AndroidRuntime(947): ... 11 more
activity_main.xml :
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.dremor.kronos.MainActivity"
tools:ignore="MergeRootFrame" />
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="info.dremor.kronos.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp"
android:text="#string/connect" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/login_greeting" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/login" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="#string/password" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/textView2"
android:layout_marginTop="35dp"
android:ems="10"
android:inputType="textPassword" />
</RelativeLayout>
Can someone help me?
In your activity, you set the layout file like this:
setContentView(R.layout.activity_main);
This means, when you write findViewById, it will look within the activity_main.xml file.
Your button on the other hand is within another layout file.
Just move the click event into your fragment, and access the context using getActivity like so:
final Button loginButton = (Button) rootView.findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), LoginDisplayActivity.class);
startActivity(intent);
}
});
your button is in the fragment layout, you're "finding" it from the activity layout. Setup button onclick in the fragment onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final Button loginButton = (Button) rootView.findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener(){...})
return rootView;
}
Remove the button and code from onCreate and shift the button and clicklistener code to onCreateView like this:
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final Button loginButton = (Button) rootView.findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginDisplayActivity.class);
startActivity(intent);
}
});
return rootView;
As your button is in fragment_main, not activity_main. The button is not found.
i have a very annoying bug that i couldn't figure out what causes it. it seens that i am getting a null pointer exception when trying to start an activity from another activity with an Intent. i tried all possible solutions i could think of,checked the syntax houndreds of times, tried to setContentView with main layout before the new layout..nothing!
I need some urgent help here. I have everything declared in the manifest,and the layout xml files. the problem relies in the ShowNoteActivity,called by the main activity (from onItemClick() method) : I get nullpointerexception from the logcat and bunch of other errors i don't know what they are.
anyway,this is the relevant code,tell me if you need more.
public class MainActivity extends Activity implements OnClickListener ,OnItemClickListener{
public final static String EXTRA_MESSAGE = "assaf.notepad.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseManager db = new DatabaseManager(this);
String[]notes = db.listNotes();
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,notes);
ListView lv = (ListView)findViewById(R.id.notes_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
ImageButton addNoteBtn = (ImageButton)findViewById(R.id.add_note);
addNoteBtn.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View view) {
Intent intent = new Intent(this, AddNoteActivity.class);
startActivity(intent);
}
#Override
public void onItemClick(AdapterView adapter, View view, int position, long id) {
DatabaseManager db = new DatabaseManager(this);
String[]notesContent = db.getNoteContent();
Intent intent = new Intent(this,ShowNoteActivity.class);
intent.putExtra(EXTRA_MESSAGE, notesContent[position]);
startActivity(intent);
}
}
and this is the problematic activity!!
public class ShowNoteActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView)findViewById(R.id.show_note);
textView.setText(text);
setContentView(R.layout.activity_show_note);
}
}
this is the logcat
02-19 03:18:07.861: E/AndroidRuntime(829): FATAL EXCEPTION: main
02-19 03:18:07.861: E/AndroidRuntime(829): java.lang.RuntimeException: Unable to start activity ComponentInfo{assaf.notepad/assaf.notepad.ShowNoteActivity}: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.os.Looper.loop(Looper.java:137)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-19 03:18:07.861: E/AndroidRuntime(829): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 03:18:07.861: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-19 03:18:07.861: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-19 03:18:07.861: E/AndroidRuntime(829): at dalvik.system.NativeStart.main(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829): Caused by: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829): at assaf.notepad.ShowNoteActivity.onCreate(ShowNoteActivity.java:19)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.Activity.performCreate(Activity.java:5104)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-19 03:18:07.861: E/AndroidRuntime(829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-19 03:18:07.861: E/AndroidRuntime(829): ... 11 more
the relevant xml ,the "ShowNoteActivity" xml ,with the problematic textView
<?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" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/board"
android:scaleType="fitXY"/>
<Button
android:id="#+id/edit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="#string/edit_btn"
android:textColor="#color/white"/>
<Button
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/edit_btn"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="#string/delete_btn"
android:textColor="#color/white"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/paper"
android:layout_below="#id/edit_btn"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="30dp"/>
<TextView
android:id="#+id/show_note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/edit_btn"
android:layout_marginTop="110dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="45dp"
android:layout_marginBottom="80dp"
android:typeface="sans"
android:textSize="10sp"/>
</RelativeLayou
t>
this is not a database problem or something like that, the problem still exists even if i try to pass a "hello world" string to the new activity. the program always crashes when invoking the new activity.
this is the xml manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="assaf.notepad"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="assaf.notepad.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="assaf.notepad.AddNoteActivity">
</activity>
<activity
android:name="assaf.notepad.ShowNoteActivity">
</activity>
</application>
</manifest>
The problem is that you try to access a TextView before you call setContentView in your ShowNoteActivity. Your code should be:
setContentView(R.layout.activity_show_note);
TextView textView = (TextView)findViewById(R.id.show_note);
textView.setText(text);
try
setContentView(R.layout.activity_show_note);
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView)findViewById(R.id.show_note);
if(textView!=null && text!=null)
{
Log.d("Mytag","Everything is right");
textView.setText(text);
}
else
{
if(textView==null)
Log.d("Mytag","Textview is null");
else
Log.d("Mytag","Text is null");
}