activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="edu.uncc.sbagursu.currencyconvertor.MainActivity">
<EditText
android:hint="Input Amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/inputAmount" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/inputAmount"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:id="#+id/radioGroup2">
<RadioButton
android:id="#+id/aud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/aud"
android:onClick="onRadioButtonClicked1"/>
<RadioButton
android:id="#+id/cad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cad"
android:onClick="onRadioButtonClicked1"/>
<RadioButton
android:id="#+id/inr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/inr"
android:onClick="onRadioButtonClicked1"/>
</RadioGroup>
<TextView
android:text="#string/convertTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:id="#+id/textView2"
android:layout_below="#+id/radioGroup2"
android:layout_alignRight="#+id/radioGroup2"
android:layout_alignEnd="#+id/radioGroup2" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/inputAmount"
android:layout_centerHorizontal="true"
android:layout_marginTop="170dp"
android:id="#+id/radioGroup3">
<RadioButton
android:id="#+id/usd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/usd"
android:onClick="onRadioButtonClicked2"/>
<RadioButton
android:id="#+id/gbp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gbp"
android:onClick="onRadioButtonClicked2"/>
</RadioGroup>
<Button
android:text="#string/convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup3"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="#+id/convertButton"
android:onClick="onClickConvert"/>
<TextView
android:text="#string/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/convertButton"
android:layout_alignLeft="#+id/radioGroup3"
android:layout_alignStart="#+id/radioGroup3"
android:layout_marginTop="28dp"
android:id="#+id/textView3" />
<Button
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_alignLeft="#+id/convertButton"
android:layout_alignStart="#+id/convertButton"
android:layout_marginTop="23dp"
android:id="#+id/clearButton" />
</RelativeLayout>
MainActivity.java :
package com.example.shara.currencyconvertor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView txt = (TextView) findViewById(R.id.textView3);
private RadioGroup grp1 = (RadioGroup) findViewById(R.id.radioGroup2);
private RadioGroup grp2 = (RadioGroup) findViewById(R.id.radioGroup3);
private EditText inputAmt = (EditText) findViewById(R.id.inputAmount);
private final String inputText = inputAmt.getText().toString();
private double inputAmount = Integer.parseInt(inputText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.convertButton).setOnClickListener(this);
findViewById(R.id.clearButton).setOnClickListener(this);
grp1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton) findViewById(checkedId);
}
});
grp2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn2 = (RadioButton) findViewById(checkedId);
}
});
}
#Override
public void onClick(View v) {
int radioId1 = grp1.getCheckedRadioButtonId();
int radioId2 = grp2.getCheckedRadioButtonId();
double result = 0;
if(v.getId()==R.id.convertButton){
if(radioId1 == R.id.aud){
if(radioId2 == R.id.usd){
result = inputAmount*1.34;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/1.34);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
if(radioId1 == R.id.cad){
if(radioId2 == R.id.usd){
result = inputAmount*1.32;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if (radioId2 == R.id.gbp) {
result = inputAmount * (0.83 / 1.32);
Log.d("test", "Result is" + result);
txt.setText(""+result);
}
}
else {
Log.d("test", "nothing here");
}
if(radioId1 == R.id.inr){
if(radioId2 == R.id.usd){
result = inputAmount*68.14;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/68.14);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
}
else if(v.getId()== R.id.clearButton){
txt.setText("");
}
}
}
Unfortunately though everything seems right I am unable to get the app to run and the app crashes. Any help in resolving this is appreciated. I am a novice in android.
Updated with Logcat file:
01-21 17:42:56.229 6017-6017/? I/art: Late-enabling -Xcheck:jni
01-21 17:42:56.244 6017-6023/? E/art: Failed sending reply to debugger: Broken pipe
01-21 17:42:56.244 6017-6023/? I/art: Debugger is no longer active
01-21 17:42:56.278 6017-6017/? W/System: ClassLoader referenced unknown path: /data/app/com.example.shara.currencyconvertor-1/lib/x86
01-21 17:42:56.278 6017-6017/? I/InstantRun: Instant Run Runtime started. Android package is com.example.shara.currencyconvertor, real application class is null.
01-21 17:42:56.526 6017-6017/? W/System: ClassLoader referenced unknown path: /data/app/com.example.shara.currencyconvertor-1/lib/x86
01-21 17:42:56.556 6017-6017/? D/AndroidRuntime: Shutting down VM
01-21 17:42:56.556 6017-6017/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shara.currencyconvertor, PID: 6017
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shara.currencyconvertor/com.example.shara.currencyconvertor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at com.example.shara.currencyconvertor.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-21 17:47:56.701 6017-6017/com.example.shara.currencyconvertor I/Process: Sending signal. PID: 6017 SIG: 9
LogCat after making changes:
01-21 18:33:57.523 11590-11590/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shara.currencyconvertor, PID: 11590
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shara.currencyconvertor/com.example.shara.currencyconvertor.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.shara.currencyconvertor.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
You're initializing the views before the onCreate has loaded. It makes sense you're getting a NullPointerException.
Try the changes here. Notice that all the views are being declared at the top but initialized in the OnCreate. The "inputText" can't be final, Unless you declare it inside the onCreate.
package com.example.shara.currencyconvertor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView txt;
private RadioGroup grp1;
private RadioGroup grp2;
private EditText inputAmt;
private String inputText;
private double inputAmount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.convertButton).setOnClickListener(this);
findViewById(R.id.clearButton).setOnClickListener(this);
txt = (TextView) findViewById(R.id.textView3);
grp1 = (RadioGroup) findViewById(R.id.radioGroup2);
grp2 = (RadioGroup) findViewById(R.id.radioGroup3);
inputAmt = (EditText) findViewById(R.id.inputAmount);
inputText = inputAmt.getText().toString();
inputAmount = Integer.parseInt(inputText);
grp1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton) findViewById(checkedId);
}
});
grp2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn2 = (RadioButton) findViewById(checkedId);
}
});
}
#Override
public void onClick(View v) {
int radioId1 = grp1.getCheckedRadioButtonId();
int radioId2 = grp2.getCheckedRadioButtonId();
double result = 0;
if(v.getId()==R.id.convertButton){
if(radioId1 == R.id.aud){
if(radioId2 == R.id.usd){
result = inputAmount*1.34;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/1.34);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
if(radioId1 == R.id.cad){
if(radioId2 == R.id.usd){
result = inputAmount*1.32;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if (radioId2 == R.id.gbp) {
result = inputAmount * (0.83 / 1.32);
Log.d("test", "Result is" + result);
txt.setText(""+result);
}
}
else {
Log.d("test", "nothing here");
}
if(radioId1 == R.id.inr){
if(radioId2 == R.id.usd){
result = inputAmount*68.14;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/68.14);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
}
else if(v.getId()== R.id.clearButton){
txt.setText("");
}
}
}
UPDATE:
Sorry I just now noticed the XML.
You are tring to parse the content of the EditText when it's empty. You need to parse it after the use wrote something. If you dont wanna add a button, you can use:
inputAmt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
inputText = inputAmt.getText().toString();
inputAmount = Integer.parseInt(inputText);
}
#Override
public void afterTextChanged(Editable s) {
}
});
Related
In an Android app that I developed, the app crashes when I move to a certain activity. I saw that many people encountered a similar problem before, but none of the former answers solve my problem.
Here's the relevant code:
StillsActivity.java
the code crashes on the last line of this snippet
public class StillsActivity extends SpatialFilteringActivity {
private static final int SELECT_PICTURE = 1;
private static final String TAG = "Stills";
private Uri mURI;
private Bitmap mBitmap;
private ImageView mImageView;
private Mat mImToProcess = new Mat();
private Mat mImGray = new Mat();
private Mat mFilteredImage = new Mat();
private SeekBar mSeekBarSpatial;
private SeekBar mSeekBarIntensity;
private SeekBar mSeekBarAlpha;
private SeekBar mSeekBarBeta;
private TextView mTextViewSpatial;
private TextView mTextViewIntensity;
private TextView mTextViewAlpha;
private TextView mTextViewBeta;
private MenuItem mSelectedItem;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE) {
if (resultCode == RESULT_OK) {
mURI = data.getData();
if (mURI != null) {
try {
mBitmap = Util.getBitmap(this, mURI);
mImageView.setImageBitmap(Util.getResizedBitmap(mBitmap,
1000));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stills);
// APP CRASHES HERE
mImageView = (ImageView) findViewById(R.id.imageView1);
Button loadButton = (Button) findViewById(R.id.loadButton);
loadButton.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SimpleDateFormat")
#Override
public void onClick(View v) {
Log.i(TAG, "onClick event");
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select an image"), SELECT_PICTURE);
}
});
mSeekBarSpatial = (SeekBar) findViewById(R.id.seekBarSpatial);
mTextViewSpatial = (TextView) findViewById(R.id.sigmaSpatialTextView);
setSeekBar(mSeekBarSpatial, mTextViewSpatial, getResources().getString(R.string.stringSpatial), MyImageProc.SIGMA_SPATIAL_MAX);
mSeekBarIntensity = (SeekBar) findViewById(R.id.seekBarIntensity);
mTextViewIntensity = (TextView) findViewById(R.id.sigmaIntensityTextView);
setSeekBar(mSeekBarIntensity, mTextViewIntensity, getResources().getString(R.string.stringIntensity), MyImageProc.SIGMA_INTENSITY_MAX);
mSeekBarAlpha = (SeekBar) findViewById(R.id.seekBarAlpha);
mTextViewAlpha = (TextView) findViewById(R.id.alphaTextView);
setSeekBar(mSeekBarAlpha, mTextViewAlpha, getResources().getString(R.string.stringAlpha), MyImageProc.ALPHA_MAX);
mSeekBarBeta = (SeekBar) findViewById(R.id.seekBarBeta);
mTextViewBeta = (TextView) findViewById(R.id.betaTextView);
setSeekBar(mSeekBarBeta, mTextViewBeta, getResources().getString(R.string.stringBeta), MyImageProc.BETA_MAX);
}
// etc.
SpatialFilteringActivity.java
public class SpatialFilteringActivity extends AppCompatActivity {
private static final String TAG = "SpatialFiltering";
//menu members
private SubMenu mResolutionSubMenu;
private SubMenu mCameraSubMenu;
//flags
private Boolean mSettingsMenuAvailable =false;
protected static final int SETTINGS_GROUP_ID = 1;
protected static final int RESOLUTION_GROUP_ID = 2;
protected static final int CAMERA_GROUP_ID = 3;
protected static final int DEFAULT_GROUP_ID = 4;
protected static final int COLOR_GROUP_ID = 5;
protected static final int FILTER_GROUP_ID = 6;
protected static final int STILL_GROUP_ID = 7;
private MyJavaCameraView mOpenCvCameraView;
private CameraListener mCameraListener = new CameraListener();
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
private String[] mCameraNames = {"Front", "Rear"};
private int[] mCameraIDarray = {CameraBridgeViewBase.CAMERA_ID_FRONT, CameraBridgeViewBase.CAMERA_ID_BACK};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spatial_filtering);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mOpenCvCameraView = (MyJavaCameraView) findViewById(R.id.Java_Camera_View);
mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_ANY);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(mCameraListener);
Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SimpleDateFormat")
#Override
public void onClick(View v) {
Log.i(TAG, "onClick event");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String currentDateandTime = sdf.format(new Date());
String fileName = Environment.getExternalStorageDirectory().getPath() +
"/sample_picture_" + currentDateandTime + ".jpg";
mOpenCvCameraView.takePicture(fileName);
addImageToGallery(fileName, SpatialFilteringActivity.this);
Toast.makeText(SpatialFilteringActivity.this, fileName + " saved",
Toast.LENGTH_SHORT).show();
}
});
}
//etc.
activity_stills.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="il.ac.tau.adviplab.iplab2_hn.StillsActivity">
<ImageView
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:src="#drawable/loadimage"
android:adjustViewBounds="true"
android:layout_centerVertical="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/load_button"
android:id="#+id/loadButton"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBarSpatial"
android:layout_marginTop="13dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/sigmaSpatialTextView"
android:layout_alignStart="#+id/sigmaSpatialTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="#+id/sigmaSpatialTextView"
android:text="#string/stringSpatial"
android:layout_below="#+id/seekBarSpatial"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBarIntensity"
android:layout_marginTop="15dp"
android:layout_below="#+id/sigmaSpatialTextView"
android:layout_alignLeft="#+id/sigmaSpatialTextView"
android:layout_alignStart="#+id/sigmaSpatialTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/sigmaIntensityTextView"
android:text="#string/stringIntensity"
android:layout_below="#+id/seekBarIntensity"
android:layout_alignLeft="#+id/seekBarIntensity"
android:layout_alignStart="#+id/seekBarIntensity"/>
<SeekBar
android:id="#+id/seekBarAlpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_below="#+id/sigmaIntensityTextView"
android:layout_alignLeft="#+id/alphaTextView"
android:layout_alignStart="#+id/alphaTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alphaTextView"
android:text="#string/stringAlpha"
android:layout_marginTop="12dp"
android:layout_below="#+id/seekBarAlpha"
android:layout_alignLeft="#+id/sigmaIntensityTextView"
android:layout_alignStart="#+id/sigmaIntensityTextView"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBarBeta"
android:layout_marginTop="8dp"
android:layout_below="#+id/alphaTextView"
android:layout_alignLeft="#+id/alphaTextView"
android:layout_alignStart="#+id/alphaTextView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/betaTextView"
android:text="#string/stringBeta"
android:layout_below="#+id/seekBarBeta"
android:layout_alignLeft="#+id/seekBarBeta"
android:layout_alignStart="#+id/seekBarBeta"/>
</RelativeLayout>
note: the #drawable/loadimage is 1000*978 pixels, if that matters
Logs
java.lang.RuntimeException: Unable to start activity ComponentInfo{il.ac.tau.adviplab.iplab2_hn/il.ac.tau.adviplab.iplab2_hn.StillsActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:666)
at android.view.View.<init>(View.java:3969)
at android.view.ViewGroup.<init>(ViewGroup.java:573)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at il.ac.tau.adviplab.iplab2_hn.StillsActivity.onCreate(StillsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I'd appreciate any help.
Thanks!
Please follow background image size
For Supported device
LDPI:
Portrait: 200x320px
Landscape: 320x200px
MDPI:
Portrait: 320x480px
Landscape: 480x320px
HDPI:
Portrait: 480x800px
Landscape: 800x480px
XHDPI:
Portrait: 720px1280px
Landscape: 1280x720px
i am new in android and i was trying to make my app works in all phones
it works in 23 API and don't work in 19 API kitkat it crashes each time i open the application
Is there a way to fix this problem ?
and could you tell me what was my problem and explain it to me,
public class MainActivity extends AppCompatActivity {
private Button ButtonStart,ButtonReset ;
private TextView Number ;
private CountDownTimer myTimer ;
private MediaPlayer TimePassesSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonStart = (Button)findViewById (R.id.button); //initialize view
Number = (TextView)findViewById (R.id.textView); //initialize view
ButtonReset = (Button)findViewById (R.id.button2);
MediaPlayer TimePassesSound;
TimePassesSound = new MediaPlayer();
TimePassesSound = MediaPlayer.create(getApplicationContext(), R.raw.time_passing);
addListerOnButton (); //call method of the view
addListerOnButton2 (); //call method of the view
}
public void addListerOnButton () {
ButtonStart.setOnClickListener (
new View.OnClickListener () {
public void onClick(View v) {
if (ButtonStart.getText ().toString () != "Stop") {
int StartTime = Integer.parseInt (Number.getText ().toString ());
myTimer = new CountDownTimer (StartTime*1000, 1000) {
public void onTick(long millisUntilFinished) {
ButtonStart.setText ("Stop");
Number.setText (""+millisUntilFinished / 1000);
}
public void onFinish() {
Number.setText("60");
ButtonStart.setText ("Start");
TimePassesSound.setLooping(false);
TimePassesSound.start();
}
}.start();
} else {
ButtonStart.setText ("Start");
myTimer.cancel ();
}
}
}
);
}
public void addListerOnButton2 () {
ButtonReset.setOnClickListener (
new View.OnClickListener () {
public void onClick(View v) {
if ("Start".equals (ButtonStart.getText ().toString())) {
Number.setText ("60");
myTimer.cancel ();
}else{
Toast.makeText (MainActivity.this,"You must stop the countdown first",Toast.LENGTH_LONG).show();
}
}
}
);
}
}
public void addListerOnButton2 () {
ButtonReset.setOnClickListener (
new View.OnClickListener () {
public void onClick(View v) {
if ("Start".equals (ButtonStart.getText ().toString())) {
Number.setText ("60");
myTimer.cancel ();
}else{
Toast.makeText (MainActivity.this,"You must stop the countdown first",Toast.LENGTH_LONG).show();
}
}
}
);
}
}
-- Error messages
Process: com.aabdelrahman730yahoo.mydesign, PID: 3627
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aabdelrahman730yahoo.mydesign/com.aabdelrahman730yahoo.mydesign.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.aabdelrahman730yahoo.mydesign.MainActivity.addListerOnButton(MainActivity.java:51)
at com.aabdelrahman730yahoo.mydesign.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Main Activity :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_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.aabdelrahman730yahoo.mydesign.MainActivity"
android:touchscreenBlocksFocus="false"
android:background="#34416a">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="#+id/button2"
android:layout_above="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="60"
android:id="#+id/textView"
android:singleLine="true"
android:textSize="80dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
That's was my full problem i hope someone could help me with
You are calling the method of the view without initializing it.
You need to initialize the view first and then call the method
ButtonStart = (Button)findViewById (R.id.button); //initialize view
Number = (TextView)findViewById (R.id.textView); //initialize view
addListerOnButton (); //call method of the view
addListerOnButton2 (); //call method of the view
Check this,
As #Rod_ mentioned and from error log, it clearly states that view is not initialized.
java.lang.NullPointerException
at
com.aabdelrahman730yahoo.mydesign.MainActivity.addListerOnButton(MainActivity.java:51)
at
com.aabdelrahman730yahoo.mydesign.MainActivity.onCreate(MainActivity.java:43)
Make sure following buttons were initialized in your java code.
ButtonStart and ButtonReset -- Buttonreset is not initialized here it seems.
Change your code like below.
under oncreate
ButtonStart = (Button)findViewById (R.id.button);
ButtonReset = (Button)findViewById (R.id.button_); --> You missed it ..
Number = (TextView)findViewById (R.id.textView);
addListerOnButton ();
addListerOnButton2 ();
Hope it seems clear..!!
UPDATE:
You may not initialized the media player.
The problem may be also due to usage of media player. Kindly check the code below.
MediaPlayer mediaPlayer;
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.time_passing);
in your case
MediaPlayer TimePassesSound;
TimePassesSound = new MediaPlayer();
TimePassesSound = MediaPlayer.create(getApplicationContext(), R.raw.time_passing);
Add the above lines befor calling the functions.
I'm trying to get the input value from EditText in SearchUser to query in SearchResult
Here is my Code for SearchUser :
package com.example.yearbookmuict9sec2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.EditText;
public class SearchUser extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_user);
addButtonListenerHomeButton();
addButtonListenerViewAll();
addButtonListenerSearchButton();
}
private void addButtonListenerSearchButton() {
// TODO Auto-generated method stub
Button button = (Button) findViewById(R.id.SearchButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText edtSearch;
String Answer;
edtSearch = (EditText)findViewById(R.id.answer);
Answer = edtSearch.getText().toString();
Intent goAnswer = new Intent(getApplicationContext(),SearchResult.class);
goAnswer.putExtra("Answer", Answer);
startActivity(goAnswer);
}
});
}
private void addButtonListenerViewAll() {
// TODO Auto-generated method stub
Button button = (Button) findViewById(R.id.viewALLBUTTON);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(SearchUser.this,ViewAll.class);
startActivity(i);
}
});
}
private void addButtonListenerHomeButton() {
// TODO Auto-generated method stub
ImageButton button = (ImageButton) findViewById(R.id.HomeButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(SearchUser.this,Home.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_user, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my activity_search_user :
<ScrollView
android:id="#+id/scrollView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/home1"
android:layout_alignTop="#+id/scrollView4"
android:background="#fffbf8f0"
android:src="#drawable/ic_home_black_24dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/home1"
android:layout_below="#+id/HomeButton"
android:text="Back Home" />
<Button
android:id="#+id/viewALLBUTTON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/SearchButton"
android:layout_alignBottom="#+id/SearchButton"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/HomeButton"
android:background="#ffff6c41"
android:text="View All"
android:textColor="#ffffffff" />
<ImageView
android:id="#+id/home1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:src="#drawable/logo_180" />
<Button
android:id="#+id/SearchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/home1"
android:layout_below="#+id/home1"
android:layout_marginLeft="56dp"
android:layout_marginTop="47dp"
android:background="#ff606efd"
android:nestedScrollingEnabled="false"
android:text="Search"
android:textColor="#ffffffff" />
<EditText
android:id="#+id/answer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/viewALLBUTTON"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
Here is my code for SearchResult :
package com.example.yearbookmuict9sec2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class SearchResult extends Activity {
SQLiteDatabase mDb;
Database mHelper;
Cursor mCursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
EditText txtAnswer;
Button btnBack;
String showAnswer;
txtAnswer = (EditText)findViewById(R.id.answer);
btnBack = (Button)findViewById(R.id.SearchButton);
showAnswer = getIntent().getStringExtra("Answer");
txtAnswer.setText(showAnswer);
ListView listView1 = (ListView)findViewById(R.id.listView1);
mHelper = new Database(this);
mDb = mHelper.getWritableDatabase();
mHelper.onUpgrade(mDb, 1, 1);
mCursor = mDb.rawQuery("SELECT " + Database.COL_StudentID + ", "
+ Database.COL_FNAME + ", " + Database.COL_LNAME+ ", "
+ Database.COL_NNAME + " FROM " + Database.TABLE_NAME +" WHERE "
+ Database.COL_StudentID + " = ?", new String[] {showAnswer});
ArrayList<String> dirArray = new ArrayList<String>();
mCursor.moveToFirst();
while ( !mCursor.isAfterLast() ){
dirArray.add("ID : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_StudentID)) + "\n"
+ "Firstname : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_FNAME)) + "\n"
+ "Lastname : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_LNAME)) + "\n"
+ "Nickname : " + mCursor.getString(mCursor.getColumnIndex(Database.COL_NNAME)));
mCursor.moveToNext();
}
ArrayAdapter<String> adapterDir = new ArrayAdapter<String>(getApplicationContext()
, android.R.layout.simple_list_item_1, dirArray);
listView1.setAdapter(adapterDir);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("First Line", "First line of text");
datum.put("Second Line","Second line of text");
data.add(datum);
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2,
new String[] {"First Line", "Second Line" },
new int[] {android.R.id.text1, android.R.id.text2 });
}
public void onPause() {
super.onPause();
mHelper.close();
mDb.close();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_result, 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);
}
}
Here is my activity_search_result :
<Button
android:id="#+id/backToHome"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/listView1"
android:layout_toEndOf="#+id/imageView"
android:background="#ffafff9b"
android:text="Back to Home" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Student"
android:textColor="#FFFFFF"
android:textSize="40dp" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:cacheColorHint="#00000000" >
</ListView>
</RelativeLayout>
Here is my LogCat :
12-09 03:57:09.421: E/AndroidRuntime(4961): FATAL EXCEPTION: main
12-09 03:57:09.421: E/AndroidRuntime(4961): Process: com.example.yearbookmuict9sec2, PID: 4961
12-09 03:57:09.421: E/AndroidRuntime(4961): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yearbookmuict9sec2/com.example.yearbookmuict9sec2.SearchResult}: java.lang.NullPointerException
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.os.Looper.loop(Looper.java:136)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-09 03:57:09.421: E/AndroidRuntime(4961): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 03:57:09.421: E/AndroidRuntime(4961): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 03:57:09.421: E/AndroidRuntime(4961): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-09 03:57:09.421: E/AndroidRuntime(4961): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-09 03:57:09.421: E/AndroidRuntime(4961): at dalvik.system.NativeStart.main(Native Method)
12-09 03:57:09.421: E/AndroidRuntime(4961): Caused by: java.lang.NullPointerException
12-09 03:57:09.421: E/AndroidRuntime(4961): at com.example.yearbookmuict9sec2.SearchResult.onCreate(SearchResult.java:39)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.Activity.performCreate(Activity.java:5231)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 03:57:09.421: E/AndroidRuntime(4961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
12-09 03:57:09.421: E/AndroidRuntime(4961): ... 11 more
Help me please.
What i have to do to fix this problem? I'm new here.
The problem is sourced on line 39 in SearchResult.java. You can see this in the LogCat on the line after the one that starts with Caused by: java.lang.NullPointerException.
txtAnswer is not a valid id for an EditText object in your activity_search_result.xml, so it returned null on line 35 when you searched for it. You cannot call .setText() on a null object.
txtAnswer is null. This is because it is not in the activity's view. Your activity loads its view as follows:
setContentView(R.layout.activity_search_result);
Yet the element with ID answer id not defined in activity_search_result.xml. It is defined in activity_search_user.xml.
So your problem is either:
a) You are loading the wrong view for your activity OR
b) You forgot to add the EditText with ID answer to your view (activity_search_result.xml)
Edit - looking at your code, I think it is likely that option is is the problem. So to fix this, change
setContentView(R.layout.activity_search_result);
to
setContentView(R.layout.activity_search_user);
You activity_search_result.xml does not contain any view that you have specified in your activity. You may have to pass right layout xml file in setContentView or change id of EditText and Button.
This question already has an answer here:
"Unfortunately, ... has stopped" when Downloading file in Android
(1 answer)
Closed 8 years ago.
Im making an app where the user has to download a file in order for the app to work.
I added a new Class called 'HTTPTest' which downloads the file. When I click on the button to download the file, it says "Unfortunately, ... has stopped.".
First, heres the Log:
Process: com.NautGames.xecta.app, PID: 2759
java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.NautGames.xecta.app.MainActivity for onClick handler on view class android.widget.Button with id 'Button01'
at android.view.View$1.onClick(View.java:3810)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getMethod(Class.java:857)
at android.view.View$1.onClick(View.java:3803)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Heres the HTTPTest Class:
package com.NautGames.xecta.app;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HTTPTest extends Activity {
String Path = Environment.getExternalStorageDirectory().getPath() + "/Download";
String dwnload_file_path = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/187259_10000060421658402_744490318028_q.jpg";
String dest_file_path = Path;
Button b1;
ProgressDialog dialog = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.Button01);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog = ProgressDialog.show(HTTPTest.this, "", "Downloading file...", true);
new Thread(new Runnable() {
public void run() {
downloadFile(dwnload_file_path, dest_file_path);
}
}).start();
}
});
}
public void downloadFile(String url, String dest_file_path) {
try {
File dest_file = new File(dest_file_path);
URL u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
fos.write(buffer);
fos.flush();
fos.close();
hideProgressIndicator();
} catch(FileNotFoundException e) {
hideProgressIndicator();
return;
} catch (IOException e) {
hideProgressIndicator();
return;
}
}
void hideProgressIndicator(){
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
}
}
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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.NautGames.chatbot.app.MainActivity"
android:background="#drawable/oneiric640x960">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:hint="Talk to Xecta"
android:layout_below="#+id/view"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_below="#+id/editText1"
android:onClick="buttonOnClick" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="File Download Demo from Coderzheaven \n\nFile to download : http://coderzheaven.com/sample_folder/sample_file.png \n\nSaved Path : sdcard/\n"
android:id="#+id/textView"
android:layout_below="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="31dp" />
<Button
android:text="Download File"
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:onClick="onClick">
</Button>
</RelativeLayout>
MainActivity.java
package com.NautGames.xecta.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
//Chat bot library
import org.alicebot.ab.Chat;
import org.alicebot.ab.Bot;
import android.view.View;
import android.widget.Button;
import android.os.Environment;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//EditText mEdit = (EditText)findViewById(R.id.editText1);
public void buttonOnClick(View v)
{
input = (TextView) findViewById(R.id.editText1);
String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/Ab";
Button button=(Button) v;
//Creating bot
String botname="xecta";
String path= dbPath;
Bot xecta = new Bot(botname, path);
Chat chatSession = new Chat(xecta);
String request = input.getText().toString();
String response = chatSession.multisentenceRespond(request);
((Button) v).setText(response);
}
#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);
}
}
Please tell me how to fix this, Thanks.
as per this answer on your other question
you need to add an onClick(View view) method to your MainActivity
When I start the activity on my phone, it says that the app is not responding. The problem is the back button which I can't seem to program to make it gather the previous text from the textView.
Xml code for activity
<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:background="#drawable/background"
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=".Author" >
<TextView
android:id="#+id/textView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/button_shape"
android:text="#string/Getstarted"
android:textColor="#FFFFFF"
android:textSize="23sp" />
<ImageButton
android:id="#+id/next"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:background="#drawable/button_shape"
android:contentDescription="#string/Next"
android:onClick="NextQuote"
android:src="#drawable/navigationnextitem" />
<ImageButton
android:id="#+id/share"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_alignTop="#+id/next"
android:layout_centerHorizontal="true"
android:background="#drawable/button_shape"
android:contentDescription="#string/share"
android:onClick="Sharing"
android:src="#drawable/socialshare" />
<ImageButton
android:id="#+id/back"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/textView1"
android:layout_alignTop="#+id/next"
android:background="#drawable/button_shape"
android:contentDescription="#string/Back"
android:onClick="PreviousQuote"
android:src="#drawable/navigationpreviousitem" />
</RelativeLayout>
Java code for activity
package com.android.motivateme3;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
public class Author extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
Button NextQuote = (Button)findViewById(R.id.next);
final TextView display = (TextView) findViewById(R.id.textView1);
NextQuote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Random numGen = new Random();
int rNumber = numGen.nextInt(10);
if (rNumber == 0)
{
display.setText(R.string.Author1);
}
else if (rNumber == 1)
{
display.setText(R.string.Author2);
}
else if (rNumber == 2)
{
display.setText(R.string.Author3);
}
else if (rNumber == 3)
{
display.setText(R.string.Author4);
}
else if (rNumber == 4)
{
display.setText(R.string.Author5);
}
else if (rNumber == 5)
{
display.setText(R.string.Author6);
}
else if (rNumber == 6)
{
display.setText(R.string.Author7);
}
else if (rNumber == 7)
{
display.setText(R.string.Author8);
}
else if (rNumber == 8)
{
display.setText(R.string.Author9);
}
else if (rNumber == 9)
{
display.setText(R.string.Author10);
} }
});
}
ImageButton Sharing = (ImageButton)findViewById(R.id.share);
Sharing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
TextView text = (TextView)findViewById(R.id.textView1);
String quote = text.getText().toString();{
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("plain/text");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is a great quote (from the Motivate Me! app)");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, quote);
startActivity(Intent.createChooser(shareIntent, "Share via:"));}}});
Button BackQuote = (Button)findViewById(R.id.back);
final TextView display = (TextView) findViewById(R.id.textView1);
BackQuote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String prev = (String) display.getText();
display.setText(prev);
}
});}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.author, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is the stack trace
04-03 14:29:34.174: E/AndroidRuntime(17383): at android.app.ActivityThread.main(ActivityThread.java:4441)
04-03 14:29:34.174: E/AndroidRuntime(17383): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 14:29:34.174: E/AndroidRuntime(17383): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 14:29:34.174: E/AndroidRuntime(17383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-03 14:29:34.174: E/AndroidRuntime(17383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-03 14:29:34.174: E/AndroidRuntime(17383): at dalvik.system.NativeStart.main(Native Method)
04-03 14:29:34.174: E/AndroidRuntime(17383): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
04-03 14:29:34.174: E/AndroidRuntime(17383): at com.android.motivateme3.Author.setupActionBar(Author.java:36)
04-03 14:29:34.174: E/AndroidRuntime(17383): at com.android.motivateme3.Author.onCreate(Author.java:25)
04-03 14:29:34.174: E/AndroidRuntime(17383): at android.app.Activity.performCreate(Activity.java:4465)
04-03 14:29:34.174: E/AndroidRuntime(17383): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-03 14:29:34.174: E/AndroidRuntime(17383): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-03 14:29:34.174: E/AndroidRuntime(17383): ... 11 more
04-03 14:29:41.664: I/Process(17383): Sending signal. PID: 17383 SIG: 9
It seems to me that R.id.next refers to an ImageButton, which cannot be cast into a Button, since it is not a subclass of a Button
Do you happen to see a ClassCastException?
To fix that particular issue, replace:
Button NextQuote = (Button)findViewById(R.id.next);
with
ImageButton NextQuote = (ImageButton)findViewById(R.id.next);