Please excuse me not good at using english...
I'm a beginner in android programming.
I'm developing android alarm app, I want AlarmActivity to be displayed on the screen when AlarmReceiver is called.
But when AlarmService.onCreate is called, an error occurs.
Process: com.example.app, PID: 11692
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.AlarmActivity}:
android.view.InflateException: Binary XML file line #27: Attempt to
invoke virtual method 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
Here is my code.
Intent i = new Intent(context, AlarmActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
From code above, Using debugger, I checked AlarmActivity.class and context is not null.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
...
}
But, savedInstanceState in AlarmActivity.java is null.
MainActivity.java
public class MainActivity extends AppCompatActivity implements ServiceCallbacks {
...
public void setAlarm (String[] times) {
int timesLen = times.length;
for (int i = 0 ; i < timesLen ; i++) {
if (times[i] != null) {
"2019-11-05 20:24:00";
int year = Integer.parseInt(times[i].substring(0, 4));
int month = Integer.parseInt(times[i].substring(5, 7)) - 1;
int date = Integer.parseInt(times[i].substring(8, 10));
int hour = Integer.parseInt(times[i].substring(11, 13));
int minute = Integer.parseInt(times[i].substring(14, 16));
int second = Integer.parseInt(times[i].substring(17, 19));
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DATE, date);
mCalendar.set(Calendar.HOUR_OF_DAY, hour);
mCalendar.set(Calendar.MINUTE, minute);
mCalendar.set(Calendar.SECOND, second);
intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra("state", "on");
intent.putExtra("time", mCalendar);
intent.putExtra("id", i);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, i, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);
Log.d("LOGGING", "Alarm added: " + times[i]);
}
}
Toast.makeText(MainActivity.this, "Alarm added.", Toast.LENGTH_SHORT);
}
...
}
AlarmReceiver.java
...
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("LOGGING", "AlarmReceiver");
Intent sIntent = new Intent(context, AlarmService.class);
sIntent.putExtra("state", intent.getStringExtra("state"));
sIntent.putExtra("time", intent.getSerializableExtra("time"));
sIntent.putExtra("id", intent.getStringExtra("id"));
Intent i = new Intent(context, AlarmActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(sIntent);
}
else {
context.startService(sIntent);
}
}
}
...
// activity_alarm.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AlarmActivity"
android:background="#CC000000">
<RelativeLayout
android:id="#+id/alarm-main-box"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/layout_bg"
android:layout_centerInParent="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="76.5dp"
android:text="nolvadex"
android:textSize="20dp"
android:textColor="#707070"
android:gravity="center"/>
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/textlines"
android:layout_marginRight="15.5dp"
android:layout_marginLeft="15.5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="76.5dp"
android:text="femara"
android:textSize="20dp"
android:textColor="#707070"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
android:layout_width="64dp"
android:layout_height="90dp"
android:background="#drawable/layout_bg_gray_circle"
android:paddingBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</RelativeLayout>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:background="#drawable/layout_bg_pink">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="AM 8:00"
android:textSize="25dp"
android:textColor="#ffffff"
android:textAlignment="center"
android:gravity="center"
android:lineHeight="60dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="72dp"
android:layout_alignParentBottom="true"
android:background="#drawable/layout_bg_gray">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:id="#+id/button-area">
<RelativeLayout
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true">
<ImageButton
android:id="#+id/center_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#drawable/layout_round_center_button"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:elevation="2dp"
android:translationZ="2dp"
android:stateListAnimator="#null"
android:textColor="#fff"
android:src="#drawable/button_check"
android:adjustViewBounds="true"
android:scaleType="centerInside"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="all"
android:textSize="12dp"
android:textColor="#F48999"
android:layout_marginTop="11dp"
android:gravity="center"
android:layout_below="#+id/center_button"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="83dp"
android:layout_height="match_parent"
android:layout_centerVertical="true">
<ImageButton
android:id="#+id/left_button"
android:layout_width="37dp"
android:layout_height="37dp"
android:gravity="center_vertical|center_horizontal"
android:background="#drawable/layout_round_left_button"
android:layout_marginTop="30dp"
android:layout_marginLeft="33dp"
android:textColor="#fff"
android:src="#drawable/button_x"
android:adjustViewBounds="true"
android:scaleType="centerInside"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"
android:textSize="12dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="2dp"
android:layout_below="#+id/left_button"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="83dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true">
<ImageButton
android:id="#+id/right_button"
android:layout_width="37dp"
android:layout_height="37dp"
android:gravity="center_vertical|center_horizontal"
android:background="#drawable/layout_round_right_button"
android:layout_marginTop="30dp"
android:layout_marginLeft="13dp"
android:textColor="#fff"
android:src="#drawable/button_clock"
android:adjustViewBounds="true"
android:scaleType="centerInside"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="re"
android:textSize="12dp"
android:layout_marginLeft="9dp"
android:layout_marginTop="2dp"
android:layout_below="#+id/right_button"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".AlarmActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="false" />
<service
android:name=".AlarmService"
android:enabled="true" />
</application>
</manifest>
I want to display AlarmActivity.
But this error occurs. please somebody help me.
I found the answer!!!!!!!!!
yeaaaaaaaaaaaaaaaaagghhhfbawjhefbwje!!!!!
I edited
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/textlines"
android:layout_marginRight="15.5dp"
android:layout_marginLeft="15.5dp"/>
to
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/textlines"
android:layout_marginRight="15.5dp"
android:layout_marginLeft="15.5dp"/>
view -> View
It works!
Related
So all I'm trying to do is check orientation and change an image based on that. The problem is that neither image change, nor the toast message is working. Not sure why.. I have this same code working elsewhere.
Java:
public class Test extends AppCompatActivity {
private ArrayList<String> mNames = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
getText();
}
private void getText() {
mNames.add("Menu");
mNames.add("Definitions");
mNames.add("Steps");
mNames.add("Examples");
mNames.add("Related");
mNames.add("Videos");
initRecyclerView();
}
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mNames);
recyclerView.setAdapter(adapter);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_main);
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
ImageView layout = findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.background_l);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.activity_main);
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
ImageView layout = findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.background_p);
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="#+id/background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/recyclerView"
android:paddingTop="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:fontFamily="#font/open_sans_bold"
android:text="#string/definition"
android:textColor="#color/backgroundBlue"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView2"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:fontFamily="#font/open_sans"
android:text="#string/fiveSDef1"
android:textColor="#android:color/background_dark"
android:textSize="14sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView3"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:fontFamily="#font/open_sans_bold"
android:text="#string/uses"
android:textColor="#color/backgroundBlue"
android:textSize="20sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView4"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:fontFamily="#font/open_sans"
android:text="#string/fiveSDef2"
android:textColor="#android:color/background_dark"
android:textSize="14sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView5"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:fontFamily="#font/open_sans"
android:text="#string/fiveSDef3"
android:textColor="#android:color/background_dark"
android:textSize="14sp" />
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/backgroundBlue"
android:fontFamily="#font/open_sans_bold"
android:text="#string/fiveS"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="29sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#id/button2"
android:layout_marginTop="15dp"
android:orientation="horizontal" />
</RelativeLayout>
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ToolTables.1"></activity>
<activity android:name=".ToolTables.2"
android:windowSoftInputMode="adjustPan">
</activity>
<activity android:name=".ToolTables.3"></activity>
<activity android:name=".ToolTables.4"></activity>
<activity android:name=".ToolTables.5"></activity>
<activity
android:name=".Test"
android:configChanges="orientation|keyboardHidden|screenSize|screenLayout">
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
Edit: I've added the rest of the code. Including the XML that couples with the Java. It's not real complicated, mostly text fields and a recyclerView.
onConfigurationChanged() callback only will be called if we specify that we will manage this changes manually on the manifest.xml inside activity tag:
android:configChanges="orientation|screenSize|keyboardHidden"
This indicates that the activity will not be recreated when the device rotates, this behavior is not recomended unless you have a valid reason to do that, for example youtube API recomends this to avoid recreating and reinitializing YoutubePlayerFragment.
Caution: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
https://developer.android.com/guide/topics/resources/runtime-changes
If its not neccesary and only you want to have a diferent image on each orientation you can:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
boolean isLandscape = this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
((ImageView)findViewById(R.id.background)).setImageResource(isLandscape ? R.drawable.background_l : R.drawable.background_p);
getText();
}
I have a button that sends the entered data to the database, After clicking on the button, an error appears unfortunately "project namе" has stopped, please, help!!!!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kz.shoot.photo.photoshoot">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<!--uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /-->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyMaterialTheme">
<activity
android:name=".LoginActivity"
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=".RegisterActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.RegisterActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package kz.shoot.photo.photoshoot;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
#Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new PhotoShootFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new HistoryFragment();
title = getString(R.string.title_history);
break;
case 2:
fragment = new AboutFragment();
title = getString(R.string.title_about);
break;
case 3:
finish();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}}
Fragment_Photoshoot.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:orientation="vertical"
tools:context="kz.shoot.photo.photoshoot.PhotoShootFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/label"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="30dp"
android:text="Мені? ?алам!"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:gravity="center_horizontal"
android:text="Жібергі?із келген жерді? суретін т?сірі?із!"
android:layout_below="#+id/label"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/captured_photo"
android:contentDescription="#string/imageDesc"
android:theme="#android:style/Widget.ImageWell"
android:scrollIndicators="right"
android:layerType="hardware"
android:layout_centerVertical="false"
android:layout_centerInParent="false"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:minHeight="250dp"
android:maxHeight="300dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:text="Камера"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cameraBtn"
android:layout_below="#+id/captured_photo"
android:onClick="takePicture"
android:layout_centerHorizontal="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_margin="10dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/mess_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/mess_title"
android:imeOptions="actionNone"
android:inputType="text"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Хабарлама категориясын та?да?ыз"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="#+id/category_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_marginTop="0dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/message"
android:layout_marginTop="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/photo_description"
android:inputType="text|textMultiLine"
android:maxLines="25"
android:elevation="0dp"
android:minHeight="82dp"
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:text="Жіберу"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sendBtn"
android:layout_below="#+id/captured_photo"
android:layout_centerHorizontal="true" />
</LinearLayout>
</android.support.v7.widget.LinearLayoutCompat>
</LinearLayout>
</ScrollView>
</LinearLayout>
I am using eclipse for developing a game for android. The game is pretty much finished but it won't launch correctly on my emulator of choice (BlueStacks).
It only shows a white screen and the infobar for about 0.5 seconds before crashing. App is still open in background after crashing but can't do or display anything.
I can compile and install the application and the syntax is correct... I assume that my mistake is somewhere in the manifest.
here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.ralfkraemer.starsandstrikes"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="de.ralfkraemer.starsandstrikes.MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The following is my main activity class
package de.ralfkraemer.starsandstrikes;
import java.io.IOException;
import java.util.Random;
import de.ralfkraemer.starsandstrikes.R;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainMenu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymainmenu);
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
randomizeFact();
}
// RANDOM FACT
String[] factresource = getResources().getStringArray(R.array.factres);
protected void randomizeFact(){
TextView randomFact = (TextView)findViewById(R.id.text1);
Random random = new Random();
int maxIndex = factresource.length;
int generatedIndex = random.nextInt(maxIndex);
randomFact.setText(factresource[generatedIndex]);
}
// START GAME
public void startGame(){
Game currentGame = new Game();
currentGame.setScore(0);
setContentView(R.layout.activitygame);
currentGame.running = true;
}
// PLAY STORE (RATE)
public void openPlayStore(){
Uri uri = Uri.parse("url");
Intent intent = new Intent (Intent.ACTION_VIEW, uri);
startActivity(intent);
}
public void goToAbout(){
setContentView(R.layout.activityabout);
}
public void goToHowToPlay(){
setContentView(R.layout.activityhowtoplay);
}
public void goToHighscores() throws IOException{
Highscores highscores = new Highscores();
setContentView(R.layout.activityhighscores);
}
}
And here is the main activity xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background"
tools:context="${linearPackage}.${activityClass}" >
<ImageView
android:id="#+id/header"
android:contentDescription="#string/header_content"
android:src="#drawable/logo_mainmenu"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp" />
<Button
android:id="#+id/button_play"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="65dp"
android:text="#string/mm_button_play"
android:textSize="22sp"
android:onClick="startGame"
android:background="#drawable/button_default" />
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="#string/mm_randomfact_placeholder"
android:textSize="16sp"
android:gravity="center"/>
<Button
android:id="#+id/button_randomfact"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="65dp"
android:text="#string/mm_button_randomfact"
android:textSize="18sp"
android:onClick="randomizeFact"
android:background="#drawable/button_default" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="#+id/button_highscores"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_highscores"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="goToHighscores" />
<Button
android:id="#+id/button_howtoplay"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_howtoplay"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="goToHowToPlay" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="#+id/button_about"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_about"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="goToAbout" />
<Button
android:id="#+id/button_rate"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_rate"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="openPlayStore" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
You are calling getResources while the activity is instantiating on this line here:
// RANDOM FACT
String[] factresource = getResources().getStringArray(R.array.factres);
and the implied Activity.this object does not have a valid context, so it crashes. You need to call this in onCreate or in another method that is called after the object instantiates.
I am writing an app that will eventually use Bluetooth to get a value for two devices' proximity to one another. I am currently trying to use buttons and onClick/onclicklistener etc to bring switch screens from the main UI to a log in or sign up page. Now my app is forcing close on start up and I'm not sure why. Thanks for any help!!
MainActivity:
package com.example.chirp;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter() ;
String status; {
if (bluetooth != null)
{
if (bluetooth.isEnabled())
{
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = mydevicename + ":" + mydeviceaddress;
}
else
{
status = "Bluetooth is not Enabled.";
}
}
Toast.makeText(this, status, Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginbutton = (Button) findViewById(R.id.button1);
loginbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.loginscreen);
}
});
}
#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;
}
public void newMessage(View v){
Intent intent = new Intent(this,loginscreen.class);
startActivity(intent);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#7FFFD4"
android:padding="20dp"
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" >
<!-- <public void loginhere(View view) {
android:
}/>-->
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:text="#string/hello_world"
android:textSize="50sp"
android:typeface="serif" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:layout_gravity="center_horizontal"
android:text="Slide to Quick Find"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button2"
android:layout_width="396dp"
android:layout_height="100dp"
android:layout_marginLeft="75dp"
android:layout_marginTop="300dp"
android:textSize="50sp"
android:text="Sign Up" />
<Button
android:id="#+id/button1"
android:layout_width="396dp"
android:layout_height="100dp"
android:layout_marginLeft="75dp"
android:layout_marginTop="50dp"
android:textSize="50sp"
android:onClick="onclick"
android:text="Log In" />
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chirp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"
android:requiresSmallestWidthDp="320"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.chirp.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="com.example.chirp.loginscreen"
android:label="CHIRP"
/>
</application>
</manifest>
loginscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#7FFFD4"
android:padding="20dp"
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=".login" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Username" />
/
</LinearLayout>
Move this code inside your onCreate method:
if (bluetooth != null) {
if (bluetooth.isEnabled()) {
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = mydevicename + ":" + mydeviceaddress;
} else {
status = "Bluetooth is not Enabled.";
}
//etc...
Currently this code block is outside of any method of your activity and that is for sure not what you want to have.
I am new to programming and after a while I finally fixed all my errors but now the app crashes on startup! I have not written all of this code by myself but I have edited it. Also when I still received errors the app could start (but not work obviously).
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tk.iWeld.iweld"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="tk.iWeld.iweld.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>
</application>
</manifest>
Activity_Main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
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=".MainActivity" >
<EditText
android:id="#+id/editText2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="Text2"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="X"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="_________________________"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark" />
<EditText
android:id="#+id/Text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Text3"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/Text3"
android:layout_marginTop="22dp"
android:text="="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark"
android:textColorHint="#android:color/holo_orange_dark"
android:textSize="30sp" />
<TextView
android:id="#+id/textRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignParentRight="true"
android:clickable="false"
android:longClickable="false"
android:text="Result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/resultbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignTop="#+id/textView3"
android:text="Result" />
<EditText
android:id="#+id/editText1"
android:layout_width="89dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignLeft="#+id/textView2"
android:ems="10"
android:hint="Text1"
android:inputType="number" />
And the MainActivity
package tk.iWeld.iweld;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.menu.main);
}
public void calculateClickHandler(View view) {
// make sure we handle the click of the calculator button
if (view.getId() == R.id.resultbutton) {
// get the references to the widgets
EditText text1Text = (EditText)findViewById(R.id.editText1);
EditText text2Text = (EditText)findViewById(R.id.editText2);
EditText text3Text = (EditText)findViewById(R.id.Text3);
TextView resultText = (TextView)findViewById(R.id.textRes);
// get the users values from the widget references
float text1 = Float.parseFloat(text1Text.getText().toString());
float text2 = Float.parseFloat(text2Text.getText().toString());
float text3 = Float.parseFloat(text3Text.getText().toString());
// calculate the result value
float totalresult = calculateRESULT(text1, text2, text3);
// now set the value in the result text
resultText.setText("" + totalresult);
}
}
// the formula to calculate the result index
private float calculateRESULT (float text1, float text2, float text3) {
return (float) (text1 * text2 / text3);
}
}
There is a mistake in the second line in onCreate():
setContentView(R.menu.main);
You cannot set a menu as view. If you want to use the main layout, use this:
setContentView(R.layout.main);
This...
android:targetSdkVersion="22" />
is SCIENCE FICTION!
The maximum API level today is 19