to the point, i am working in a library for android app's, all programing part is almost finish, i am testing a idea.
the idea is put some interfaces in the librery that i am working, and this GUI can be loaded in the main app.
i try to do it but i get to error the first was i did not declare the activity in the AndroidManifest in the main app, the second one is the one a i cant solve, the autogenerated Class R don't capture the GUI that it is in the librery.
it is anyway i can do this or it is imposible.
code to see what i am trying todo
in my librery (movilsecure)
EmisionActivity.java (have the activity_emision.xml in the res of my librery)
in the Main Android App (
import ve.com.idyseg.movilsecure.EmisionActivity;
import ve.com.idyseg.movilsecure.MSMasterControllerTEST;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 captureEvent (View v){
Intent intento = new Intent(this,EmisionActivity.class);
startIntent(intento);
}
}
In Eclipses, right click the project, the click Build Path then click Add Library. Please comment if you're still stuck.
In the library's manifest, in the <activity> node, did you set android:exported="true"?
Example:
<activity
android:name="com.example.app.EmisionActivity"
android:exported="true">
</activity>
Related
I have Action bar and a lot of different Activity, its working, but I don't want to add code in every Activity. How to add one code in one Activity that will work in every Activity? I expect to write only one code in one Activity for all activities.
My main activity:
package ua.in.masterpc.technoguide;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
public class IconsMain extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icons_main);
ImageButton myImageButton = findViewById(R.id.iconPC);
myImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(IconsMain.this, PCProblems.class);
startActivity(intentLoadNewActivity);
}
});
}
#Override//start menu code
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.bar_menu, menu);
return true;
}//end menu code
}
Thank you for answers!
You can create class named BaseActivity which will extend AppCompatActivity and put there the code and functionality you want to use in every other Activity. Then in every of your activities, you will extend BaseActivity instead of AppCompatActivity. By doing that you can avoid duplicating your code. Remember that you will have to put ActionBar in every activity .xml layout file.
create a baseActivity
add function inside it to initialize the toolbar and customize it as you like
open class BaseActivity : AppCompatActivity() {
protected fun initializeToolbar() {
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
}
}
create toolbar_main.xml
extends the baseActivity in any activty after including the toolbar_main.xml as <include layout="#layout/toolbar_main" />
class MainActivity : BaseActivity(){
}
and call the function you created in baseActivity.
Create a layout say, toolbar_layout, and add the the code for your layout, then include the layout in all your activities:
I recently started the learning process of creating android apps, I am using IntelliJ IDEA 13.1.1 and I am following this tutorial of adding action bar buttons and I am getting the error cannot resolve symbol navutils. I have searched online and came across couple of stackoverflow posts (1 and 2) and in those post they seem to be talking about fixing the problem via build.gradle which i have no idea what it is and cant seem to find this file anywhere.
here is my DisplayMessageActivity.java code, so far the app seems to be working just fine, i am able to post the text and see it but this action bar button is driving me crazy.
package com.example.My_First_App;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String received_message = intent.getStringExtra(MyActivity.SOME_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(received_message);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I will really appreciate any help in this matter as i am stuck and cant move forward with the tutorial.
I think you are missing
import android.support.v4.app.NavUtils;
I don't see you importing NavUtils anywhere.
Add import android.support.v4.app.NavUtils; to your imports.
package com.example.climaconproject;
import java.net.InetSocketAddress;
import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import com.nxp.jip.JIPImpl;
import com.nxp.jip.PacketHandlerIPv6;
import com.nxp.jip.exception.JipException;
import com.nxp.jip.service.JenNetIPNetwork;
import com.nxp.jip.service.Node;
import com.nxp.jip.service.Service;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display= (TextView) findViewById(R.id.textView2);
PacketHandlerIPv6 my_packet = new PacketHandlerIPv6();
JIPImpl my_jip = new JIPImpl(my_packet);
Service service =new Service(my_jip);
}
#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;
}
}
and the runtime error is
Fatal exception: receive
java.lang.NullExceptionPointer
at com.nxp.jip.PacketHandlerIPv6$ReceiveWorker.run(PAcketHandlerIPv6.java:223)
When i run the same lines of the code on a java program it works normally but in android the NullPointerException runtime error appears
You need at least the internet permission. Edit your AndroidManifest.xml and add
<uses-permission android:name="android.permission.INTERNET" />
inside the manifest element i.e. outside application.
This question already has answers here:
Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs
(9 answers)
Closed 9 years ago.
Having some trouble with my app.
First some code:
package activities;
import prototype.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class Doodle extends Activity implements OnClickListener {
//buttons
private ImageButton colorBtn;
//dialogs
private Dialog brushDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doodle);
//color button
colorBtn = (ImageButton)findViewById(R.id.color_btn);
colorBtn.setOnClickListener(this);
//dialogs
brushDialog = new Dialog(this);
brushDialog.setTitle("Color chooser:");
brushDialog.setContentView(R.layout.doodlepaintpalette);
Log.i("<------------------------->", "Hello - 3");
}
#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;
}
//user clicked paint
public void paintClicked(View view){
//Code here
brushDialog.dismiss();
}
//Top bar buttons
#Override
public void onClick(View view){
if(view.getId()==R.id.color_btn){
brushDialog.show();
}
}
"doodlepaintpalette" is a linear layout file containing various clickable imagebuttons with different colour codes. when I click the "color_btn" it correctly brings up the dialog, but when I click on one of the imagebuttons, the app crashes with this logcat:
E/AndroidRuntime(1486): java.lang.IllegalStateException: Could not find a method paintClicked(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton
The code worked fine when I had the image buttons in the "doodle" layout but since moving them to their own layout for use with the dialog, its created problems.
any help?
can post more code if need be
You paintClicked() method is not found as the layout is inflated in the dialog, and it looks for the method in there, but your method is in your activity.
You'll find an answer here:
Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs
Here's the code:
package com.example.sample;
import android.os.Bundle;
import android.widget.TextView; // (errorL:" the import android.widget.TextView cannot resolve")
import android.app.Activity;
import android.view.Menu;
public class PlayerActivity extends Activity {
int count=0;
TextView // error:"syntax error on token "TextView",delete this tokon)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.player, menu);
return true;
}
}
Go to Project > Clean on eclipse.
Sometimes eclipse does that to you. First of all, try to exit out from eclipse and restart it, it should fix your issue. If it doesn't try Project->clean project. If it still doesn't work, make sure you haven't mess up with R.java file. If you are not sure, simply create new project and copy, Paste your code.
I hope this helps.