עבור לתוכן

בעיה בקוד [לאנדרואיד (JAVA)]

Featured Replies

פורסם

שלום לכולם

יש בעיה בקוד הזה (לפי אקליפס ולפי ה-ADK)



package android.battun;


import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.content.Context;
import android.view.View;






public class AndroidtesttwoActivity extends Activity


{
EditText helloName;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText helloName;
Button button = (Button)findViewById(R.id.go);

// Register the onClick listener with the implementation above

button.setOnClickListener(mAddListener);
}
// Create an anonymous implementation of OnClickListener

private OnClickListener mAddListener = new OnClickListener()

{

public void onClick(View v)

{

long id = 0;

// do something when the button is clicked

try

{

helloName = (EditText)findViewById(R.id.go);
Context context = getApplicationContext();

CharSequence text = "Hello " + helloName.getText() + "!";

int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);

toast.show();
}

catch (Exception ex)

{

Context context = getApplicationContext();

CharSequence text = ex.toString() + "ID = " + id;

int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);

toast.show();

}

}

};
}



הבעיה היא בשורה:


helloName = (EditText)findViewById(R.id.go);

ובשורה:


Button button = (Button)findViewById(R.id.go);

בשתי השורות הבעיה היא במה שבסוגריים

מה לעשות?

פורסם

1. האם ערכת את ה XML בהתאם?

2. לא נראה לי הגיוני שאת מנסה פעם אחת לתת לכפתור ופעם אחת לתיבת עריכה את אותו ה id תבדוק את עצמך

פורסם

וכמו ש-trex אמר, אין מצב ש-R.id.go שייך גם לכפתור וגם לתיבת טקסט (אלא אם כן יצרת איזה custom control שאיכשהו ממש את שניהם, אבל גם זה לא אפשרי למיטב ידיעתי).

אני מניח שהמתודה findViewById מחזירה לך null ולכן ההמרה נופלת ומתקבלת השגיאה.

תשימי פה את הקוד מ-main.xml שנראה מזה בדיוק ה-go הזה...

פורסם
  • מחבר

אההה וואי איזה דביל אני לא שמתי לב שנתתי לשניהם אותו ID.... :kopfpatsch::facepalm:

ויש לי עוד שאלה איך קוראים למתודה/מחלקה אחרת?

אני רוצה שכשהמשתמש ילחץ על הכפתור יפתח לו חלון (layout) אחר.

(זה לא כמו תכנות רגיל ב-JAVA )


class.method();

דרך אגב מישהו מכיר מדריך טוב לפיתוח אפליקציות לאנדרואיד?

(יודע JAVA מעולה)

פורסם

Apress Beginning android 3

ספר טוב לדעתי

ולגבי השאלה שלך על מנת לפתוח layout אחר כדאי ליצור ולהריץ activity נפרדת

אם אתה לא יודע איך עושים זאת תסתכל בספר עמוד 218

פורסם
  • מחבר

התכוונתי מדריך אינטרנטי (כבר הזמנתי שני ספרים מאמזון)

אני יודע איך ליצור layout ולחבר אותה עם ה-activity מה שאני לא יודע זה איך לקרוא ל-activity

פורסם

u will need to write the activity in the manifest.xml file

like <activity

android:label="selectionMenu"

android:name=".SelectionMenu" >

</activity>

where's name is the class name for the activity (the dot is the package name)

label is just what you will see in the title

in the java code you will need to create an intent like this

Intent i = new Intent(this, SelectionMenu.class); "this" is the class u are calling from so u need to call the constructor outside any function

when you want to load the activity just call

startActivity(i) and the activity will start

פורסם
  • מחבר

לא מצליח ליצור:


Intent i = new Intent(this, SelectionMenu.class);

פורסם

א. תעשה import ל android.content.intent

ב. תכריז מחוץ לפונקציות (לא ב oncreate) בתוך המחלקה (כדי שthis יפנה למופע המחלקה עצמה)

אגב חברים: איזו פונקציה נותנת את הקונטקסט הנוכחי? אם הייתי רוצה ליצור את האינטנט ב oncreate

פורסם
  • מחבר

הבעיה היא פה



this, SelectionMenu.class

פורסם

במקום SelectionMenu.class

תשתמש בשם של המחלקה שלך

זאת דוגמא מהקוד שלי

פורסם
  • מחבר

נו ברור זה לא עובד

פורסם

תראה לי את הקוד שלך

  • 5 חודשים מאוחר יותר...
פורסם

לא מצליח ליצור:


Intent i = new Intent(AndroidtesttwoActivity.this, SelectionMenu.class);

תנסה את התיקון שעשיתי להודעה שלך

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים