App showing a blank screen but Preview looks fine - java

So I've tried to write an application that saves relatively high-quality images but when I start the app in an emulator or my phone, it only shows a blank screen. In the Preview, it looks like it should look.
I have little experience in Java. Not much tho. I've tried a few things like changing the layout or what is displayed but nothing really worked or made any difference.
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameraexample">
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<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=".DisplayImage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="com.example.android.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_path" />
</provider>
</application>
</manifest>
This is the activity_main.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"
tools:context=".MainActivity">
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/capture_image"
android:onClick="captureImage"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/display_image"
android:onClick="displayImage"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
This is activity_main source code:
public class MainActivity extends AppCompatActivity {
String currentImagePath = null;
private static final int IMAGE_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void captureImage(View view) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(getPackageManager()) != null){
File imageFile = null;
try {
imageFile = getImageFile();
}
catch (IOException e) {
e.printStackTrace();
}
if(imageFile != null) {
Uri imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, IMAGE_REQUEST);
}
}
}
public void displayImage(View view) {
Intent intent = new Intent(this, DisplayImage.class);
intent.putExtra("image_path", currentImagePath);
startActivity(intent);
}
private File getImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageName = "jps_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(imageName, ".jpg", storageDir);
currentImagePath = imageFile.getAbsolutePath();
return imageFile;
}
}
This is the activity_display_image.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"
tools:context=".DisplayImage">
<ImageView
android:contentDescription="#string/bild_ansicht"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:id="#+id/mimageView"/>
</RelativeLayout>
And last but not least, it's source code:
public class DisplayImage extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_image);
imageView = findViewById(R.id.mimageView);
Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("image_path"));
imageView.setImageBitmap(bitmap);
}
}
There are no error messages. I want those buttons to show up. The rest should work just fine.

It seems like that the multiple MAIN entry points are confusing your opening of the app.
Make sure you are opening the app from the correct launcher icon.
You should see two icons from the same app in your launcher. There might be a blank layout for the DisplayImage Activity which you are opening. Thus the blank screen.
I am referring to these in the manifest file
<activity android:name=".DisplayImage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

I am also not that experienced with Android but have you tried removing the intent filter for DisplayImage in your manifest?
You have two activities with this defined :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
That tells Android that it can launch the application from either of them and since DisplayImage is declared first in the manifest, what you are seeing is an empty blank screen as no image has yet been loaded from MainActivity.
Check this out so that you have a better understanding of intents and intent filter
https://developer.android.com/guide/components/intents-filters

Related

Open a new activity for each list item clicked from webview

I am loading html file from asset folder in a webview. There is an unordered list. I want to open a new activity when each list item is clicked. This is what I was trying so far. But it shows file not found error. I am attaching a picture of my desired scenario. Please have a look at that. It will help you understand my problem.Wanted Scenario
wv_topic_details = findViewById(R.id.wv_topic_details);
wv_topic_details.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Intent intent = new Intent(TopicDetails.this, PrachinYug.class);
startActivity(intent);
return true;
}
});
wv_topic_details.loadUrl(file_path);
webSettings = wv_topic_details.getSettings();
webSettings.setBuiltInZoomControls(true); //enable zoom facility
webSettings.setDisplayZoomControls(false); //hide the zoom control switch
AndroidManifest.xml
<activity
android:name=".TopicDetails"
android:parentActivityName=".MainActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="prachin_yug"/>
</intent-filter>
</activity>
<activity
android:name=".PrachinYug"
android:label="#string/prachin_yug"
android:parentActivityName=".MainActivity" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="prachin_yug"/>
</intent-filter>
</activity>
<activity
android:name=".MadhyaYug"
android:label="#string/madhya_yug"
android:parentActivityName=".MainActivity" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="madhya_yug"/>
</intent-filter>
</activity>
<activity
android:name=".AdhunikYug"
android:label="#string/adhunik_yug"
android:parentActivityName=".MainActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="adhunik_yug"/>
</intent-filter>
</activity>
This is the html file. This file is in the TopicDetails class. I want to open PrachinYug, Madhyaug and AdhunikYug class when respective item is clicked in the list. The target classes are not webview. They are android listview.
<p>
আনুমানিক ....... করা যায়। যথা-
</p>
<ul>
<li>প্রাচীন যুগ</li>
<li>মধ্যযুগ</li>
<li>আধুনিক যুগ</li>
</ul>
Main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = findViewById(R.id.webview);
webview.loadUrl("file:///android_asset/yourfile.html");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url{
if (url.equals("Your URL inside yourfile.html")) {
startActivity(new Intent(this, YourActivity.class));
}
return true;
}
});
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webview_contributors"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Activity reopens when a NFC Tag is found

I'm developing an app that should detect NFC tags.
My problem is that my activity reopens every time the app scans a tag. It should just open when the app is closed. But when it is active, I just want the data from the tag.
Manifest:
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.corinna.nfc_testapp" >
<uses-permission android:name="android.permission.NFC" />
<uses-sdk android:minSdkVersion="14"/>
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
</manifest>
Activity onCreate & handleIntent
rotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Fragment ReadOrWrite
fRead = (Fragment_Read) Fragment.instantiate(getApplicationContext(), Fragment_Read.class.getName(), null);
Fragment_ReadOrWrite fReadOrWrite = (Fragment_ReadOrWrite) Fragment.instantiate(getApplicationContext(), Fragment_ReadOrWrite.class.getName(), null);
fReadOrWrite.setFragment_Read(fRead);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.id_activity_main, fReadOrWrite);
fragmentTransaction.commit();
//NFC
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
String type = intent.getType();
if (MIME_TEXT_PLAIN.equals(type)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefReaderTask ndefReader = new NdefReaderTask(fRead);
ndefReader.execute(tag);
} else {
System.out.println("Wrong mime type: " + type);
}
}
}
Just use launchMode="singleTop" in your main activity manifest.
This will ensure that if your activity is at the top of your task stack, it will not be recreated.
Be aware that onCreate is no longer called in this case, so if you want to read the content from intent, you need to override the onNewIntent Activity method.

Runtime Error because of ListView id

i'm still new at learning java and this is my first android app and it used to work but now it just shows me an error whenever i try to run this page :
package com.ds.rssreader;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class StartingPoint extends ListActivity {
Button Add;
Button Delete;
public static String Menu[];
public static int Menu_counter = 0;
ListView Sites;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.starting);
Add = (Button) findViewById(R.id.Add);
Delete = (Button) findViewById(R.id.Delete);
Sites = (ListView) findViewById(R.id.list);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent OpenAddPage = new Intent("com.ds.rssreader.AddPage");
startActivity(OpenAddPage);
}
});
Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent OpenDeletePage = new Intent(
"com.ds.rssreader.DeletePage");
startActivity(OpenDeletePage);
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
and here it's Layout:
<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:padding="25dp"
tools:context="com.ds.rssreader.StartingPoint" >
<Button
android:id="#+id/Add"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Add"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/Delete"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Delete"
tools:ignore="HardcodedText" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Add"
android:clickable="true"
tools:ignore="HardcodedText" >
</ListView>
</RelativeLayout>
and here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ds.rssreader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:name="android.permission.INTERNET"
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.ds.rssreader.Splash"
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.ds.rssreader.StartingPoint"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.ds.rssreader.StartingPoint" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.ds.rssreader.AddPage"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.ds.rssreader.AddPage" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.ds.rssreader.DeletePage"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.ds.rssreader.DeletePage" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.ds.rssreader.RSSFeed"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.ds.rssreader.RSSFeed" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.ds.rssreader.RSSHandler"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.ds.rssreader.RSSHandler" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.ds.rssreader.RSSItem"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.ds.rssreader.RSSItem" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
the error causes the program to crash whenever i try to open this page and it says :
E/AndroidRuntime(1085): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ds.rssreader/com.ds.rssreader.StartingPoint}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
and when i try to debug it, it tells me that there is something's source not found
and for the record when i make any other page opens instead of this page it opens without any problems.
REMEMBER GUYS : I'm new in java so don't say big words try to simplify as much as you can
Thanks
In your layout, change:
android:id="#+id/list"
to
android:id="#id/android:list"
and, in your class change:
Sites = (ListView) findViewById(R.id.list);
to
Sites = (ListView) findViewById(android.R.id.list);

Android app having nullpointerexception when started by uri

I have a Uri scheme which is pretty standard. My manifest is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enayet.minigma"
android:versionCode="11"
android:versionName="1.5.1" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".multiscreen"
android:label="#string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
<data android:scheme="http" android:path="minigma"/>
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/title_activity_settings" >
</activity>
</application>
<uses-sdk
android:maxSdkVersion="19"
android:minSdkVersion="15"
android:targetSdkVersion="19" />
</manifest>
and in my main activity (this is the activity I want the Uri to go to with the information I parse from the Uri) I have a getIntent() in the OnCreate function. The problem is, even though I have an if statement which guards against this, Android gives me the following error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.enayet.minigma/com.enayet.minigma.multiscreen}: java.lang.NullPointerException
This is my OnCreate function:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiscreen);
Intent intent = getIntent();
Uri data = intent.getData();
if (data != null) {
List<String> params = data.getPathSegments();
String message = params.get(0);
EditText msg = (EditText) findViewById(R.id.messageEdit);
if (message != null) msg.setText(message);
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}

Widget configuration activity doesn't start

As I understood, if I define configuration activity for widget, onUpdate() method will not call, until I'll not configure it. But in my case, and I wonder why - configuration activity doesn't start, and update method calls after placing widget. My application have other activities, one of them starts when application run, maybe that's is the reason? Here my code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.watcom.MobileInformer"
android:versionCode="2"
android:versionName="1.01"
>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<application android:label="#string/app_name" android:theme="#style/WatcomTheme" android:icon="#drawable/icon">
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="false"/>
<activity android:name=".AuthorizationActivity"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="com.watcom.MobileInformer.UPDATE_WIDGET"/>
</intent-filter>
</activity>
<activity android:launchMode="singleTop" android:name=".WidgetConfigureActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity android:name=".ProjectsActivity" android:screenOrientation="portrait"/>
<receiver
android:icon="#drawable/icon"
android:label="Watcom informer"
android:name=".MyWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="ru.example.android.widget.ACTION_WIDGET_RECEIVER"/>
</intent-filter>
<intent-filter>
<action android:name="com.watcom.MobileInformer.UPDATE_WIDGET"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_provider" />
</receiver>
</application>
<uses-permission xmlns:android="http://schemas.android.com/apk/res/android"
android:name="android.permission.INTERNET" />
</manifest>
widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="schemas.android.com/apk/res/android"
android:minHeight="320dp"
android:minWidth="100dp"
android:updatePeriodMillis="1800000"
android:initialLayout="#layout/widget_layout"
android:configuration="com.watcom.MobileInformer.WidgetConfigureActivity"/>
WidgetConfigureActivity
public class WidgetConfigureActivity extends Activity {
int widgetId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.login_layout);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras!=null)
widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
setResult(RESULT_OK, resultValue);
finish();
}

Categories

Resources