java code:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity<View> extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
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="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
the error message:
06-18 15:19:12.493: E/AndroidRuntime(799): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
I really do not know the reason about that,and i check the case about the method name,that's ok.
Who can help me?
you have to declare your activity as :
public class MainActivity extends Activity {
}
and also you need to declare your button in your onCreate() method like below:
Button button_send= (Button)findViewById(R.id.your_Button_Id);
update:
Also you have to assign an id for your button in your manifest and refer to it above.
Remove <View>. Also move your initialization of Editext to oncreate(). Need not initialize editext evey time on button click.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private EdiText editext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit_message);
}
....
Related
I have two buttons on my main Activity, which both should lead to another activity, but when I run the code it is showing an error.
MainActivity.java is this:
package com.assignment2.courier;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void second (View v){
Intent i = new Intent(this, adelivery.class);
startActivity(i);
}
}
}
This is the button code in activity_main.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="85dp"
android:layout_marginBottom="234dp"
android:onClick="second"
android:text="Arrange Delivery"
android:textSize="20sp" />
Error:
Failed to compile values file
What is the problem here?
The problem here is that the second function is inside the onCreate
You need to adapt the code as follows:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void second (View v){
Intent i = new Intent(this, adelivery.class);
startActivity(i);
}
}
You should move second method out of the onCreate method; this is the problem in your code.
I am an Android newbie and I was following this tutorial for learning android. The app takes input from the user and prints the text whenever the user presses the Submit button. It is able to take input from the user but it does not output anything when Submit button is pressed. Eclipse doesn't show any error in the code, so I don't know what's wrong. Here is the code(Sorry for pasting so much of code as I wasn't able to make out which file was exactly causing the error):
helloworld/src/com.archit.helloworld/DisplayMessageActivity.java
package com.archit.helloworld;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.archit.helloworld.R;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Create new text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//set the text view as the activity layout
setContentView(textView);
}
/**
* 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);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_display_message,
container, false);
return rootView;
}
}
}
helloworld/src/com.archit.helloworld/MainActivity.java
package com.archit.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
helloworld/res/layout/activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
helloworld/res/layout/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="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
helloworld/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.archit.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.archit.helloworld.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.archit.helloworld.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.helloworld.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.helloworld.MainActivity" />
</activity>
</application>
</manifest>
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
has to be
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Implement onclicklistener Where is your onclickListener
actually you forgot to add startActivity method
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
You have missed to call
startActivity(intent);
in your
sendMessage(View V)
method. So finally it will be
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
calculated.java: (this has to command to show the calculated.xml layout)
public class Calculated extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculated);
}
}
older:
main class:
package com.barth.appie;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
Button button1;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button1 = (Button) findViewById(R.id.buttoncalculate);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText)findViewById(R.id.editText1);
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(), Calculated.class);
startActivityForResult(myIntent, 0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
calculated.xml: (this is what it has to show after the button press)
<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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Furthermore I have <string name="text">Value</string> at strings.xml, and in the other class called calculated.class, I have it so that it outputs the calculated.xml ( which works fine)
What I want: I want to display text in calculated.xml, which is a string made in main.class, and i want the string to be the text filled in textfield called "editText1"
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
Button button1;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button1 = (Button) findViewById(R.id.buttoncalculate);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText)findViewById(R.id.editText1);
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(),Calculated.class);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Calculated.java
public class Calculated extends Activity {
TextView mTextview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculated);
mTextview = (TextView)findViewById(R.id.textView1);
mTextview.setText(getIntent().getStringExtra("mytext"));
}
calculated.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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
To send the data from one activity to another activity
MainActivity.java is first activity from where you want to send data to another activity.
Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("text", text);
startActivity(myIntent);
Calculated.java is second activity which receive the intent data whatever you pass from MainActivity.java
String text = myIntent.getStringExtra("text");
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(text);
In below code, intent from the first activity to the second only carries 1 string and I need it to carry 2. I have tried to use bundle to achieve this but I think my problem is in this line
extras.putString(EXTRA_MESSAGE, message);
The second problem that you might notice is I need to get the text to be colored by using a string entered on the first page, however I need to spend more time researching this and the first problem is more important.
If someone could help me to solve this first problem it would be most appreciated
I have all the relevant code below.
Main Activity
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public final static String EXTRA_MESSAGE_COLOR = "com.example.myfirstapp.MESSAGE2";
#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.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage (View view) {
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
}
Second Activity
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Bundle bundle = getIntent().getExtras();
String messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR);
String message = bundle.getString(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextColor(getResources().getColor(R.color.blue));
textView.setTextSize(100);
textView.setText(message);
textView.setText(messagecolor);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar API
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:
// 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);
}
}
XML File
<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"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:id="#+id/edit_message"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message" />
<EditText
android:id="#+id/edit_message_color"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message_color" />
<Button
android:layout_weight="0"
android:layout_marginLeft="55dp"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="70dp"
android:text="#string/button_send"
android:textColor="#color/royal_blue"
android:textStyle="bold"
android:textSize="30sp"
android:background="#color/pale_green"
android:onClick="sendMessage" />
</LinearLayout>
Just put that extras Bundle in intent extra and then in next activity you will get the correct response
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
extras.putString(EXTRA_MESSAGE_COLOR, messagecolor );
i.putExtras(extras);
startActivity(i);
try this..
public void sendMessage (View view) {
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
Just Edit your sendMessage method to
public void sendMessage (View view)
{
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
startActivity(i);
}
Add id attribute in button in layout file and implement onClick method on this button in your main activity.
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public final static String EXTRA_MESSAGE_COLOR = "com.example.myfirstapp.MESSAGE2";
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findVIewById(R.id.buttonSend);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
sendMessage();
}
)};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage (View view) {
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
}
And your layout code :
<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"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:id="#+id/edit_message"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message" />
<EditText
android:id="#+id/edit_message_color"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message_color" />
<Button
android:layout_weight="0"
android:id="#+id/buttonSend"
android:layout_marginLeft="55dp"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="70dp"
android:text="#string/button_send"
android:textColor="#color/royal_blue"
android:textStyle="bold"
android:textSize="30sp"
android:background="#color/pale_green" />
</LinearLayout>
Use getIntent().getExtras().getStringArray("key") to get array.
I'm trying to use droidParts, specifically the DI part. I'm testing 2 injections, InjectResource and InjectView, but the second one doesn;t seem to be working.
I have a simple default layout with a textView:
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
and here's my sample code:
import org.droidparts.annotation.inject.InjectResource;
import org.droidparts.annotation.inject.InjectView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends org.droidparts.activity.Activity {
#InjectView(R.id.text1) private TextView txtView;
#InjectResource(R.string.app_name) private String appName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("testLog", appName);
txtView.setText("Test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The InjectResource works properly and logs the String, but when it gets to the setText("Test"); line I get a nullPointerException. Is there anything else that needs to be done to use InjectView?
The content view needs to be set in preInject() as onCreate() is called right after the injection has been performed. And Views can't be found without layout information.
So the code should look like this:
#Override
public void onPreInject() {
setContentView(R.layout.activity_main);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txtView.setText("Test");
}