counter
स्मार्टक्यूए का लोगो

Can't work out what is wrong with the code for my basic Android App

"can't work out what is wrong with the code for my basic android app" के लिए कोड उत्तर. हमें मिल 2 कोड उदाहरण पर स्मार्टक्यूए नीचे java श्रेणी।

आपको जो समाधान चाहिए वह खोजें!

हम 50 से अधिक भाषाओं के सामान्य प्रोग्रामिंग मुद्दों के लिए समाधान प्रदान करते हैं, आशा है कि इससे मदद मिलेगी!

उदाहरण #1

द्वारा Kalem 0👍
mikesButton = (Button) findViewById(R.id.mikesButton); /// ref to the location by id

public boolean onLongClick(View v) {    //// Callback method
     TextView Change = (TextView) findViewById(R.id.mikesButton);
     Change.setText("Good job, it changed when you held onto the button!");
     return true;
}
Button mikesButton = new Button(this);
mikesButton.setText("Click Me");
mikesButton.setBackgroundColor(Color.RED);
//mikesButton.setId(1);
Button mikesButton = (Button) findViewById(R.id.mikesButton);
mikesButton.setText("Click Me");
mikesButton.setBackgroundColor(Color.RED);
//mikesButton.setId(1); <-- never do this when inflating from xml R.id.mikesButton is already the id
protected void onCreate(Bundle savedInstanceState) {
    ...
    Button mikesButton = new Button(this); //<-- this means you are using a local to the method an not the class property you define
    ...
}
protected void onCreate(Bundle savedInstanceState) {
    ...
    this.mikesButton = (Button) findViewById(R.id.mikesButton);
    this.mikesText = (TextView) findViewById(R.id.mikesText); <-- Notice that i chaged the id so it should be changed on the activity_main layout
    ...
}
setContentView(R.layout.activity_main);
/////// Sets this activities content to this view & change background color //////////
setContentView(myLayout);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ////////// Button object being created /////////////////
    this.mikesButton = (Button) findViewById(R.id.mikesButton); /// ref to the location by id
    this.mikesButton.setText("Click Me");
    this.mikesButton.setBackgroundColor(Color.RED);

    ///////// Input object called mikesText is added //////////////////////
    this.mikesText = (TextView) findViewById(R.id.mikesText);


    ///////// Set up an event listener for the button //////////////
    this.mikesButton.setOnClickListener(  /// listen for an event on this button
            new Button.OnClickListener() {   //// Interface
                public void onClick(View v) {    //// Callback method
                    MainActivity.this.mikesText.setText("Good job, it changed!");
                }
            }
    );


    ///////////// Listening for multiple events ////////////////////
    this.mikesButton.setOnLongClickListener(
            new Button.OnLongClickListener() {   //// Interface
                public boolean onLongClick(View v) {    //// Callback method
                    MainActivity.this.mikesText.setText("Good job, it changed when you held onto the button!");
                    return true;
                }
            }
    );


    //////////// Set up an event listener for a double tap event //////////////
    this.gestureDetector = new GestureDetectorCompat(this, this); /// an object from GD class to detect gestures
    this.gestureDetector.setOnDoubleTapListener(this); /// Set method to detect double taps


    //TODO: if you want to add a button from code
    Button otherButton = new Button(this);
    otherButton.setText("Hey i was made by code");
    otherButton.setBackgroundColor(Color.BLUE);

    ////////////// Rules of where to place the layout object ///////////////
    RelativeLayout.LayoutParams buttonDeets = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    //////////// Rules of where to place the button object ////////////////
    buttonDeets.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttonDeets.addRule(RelativeLayout.CENTER_VERTICAL);

    RelativeLayout mainLayout = this.findViewById(R.id.mainLayout); //<-- you have to set the id property of the RelativeLayout on the xml file
    mainLayout.addView(otherButton,buttonDeets);

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/mikesButton"
        android:text="Button to press"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:text="Text to change"
        android:id="@+id/mikesText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mikesButton"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:textAppearanceLarge"/>
</RelativeLayout>
////////// Add widgets to layout and their rules///////////
myLayout.addView(mikesButton, buttonDeets); //Second param shows android the rules of how it is layed out
myLayout.addView(mikesText, textdeets);
this.mikesButton = (Button) findViewById(R.id.mikesButton);
// Later ...
this.mikesButton = new Button(this);
// So now you lost the reference to the previous object
this.mikesButton = (Button) findViewById(R.id.mikesButton);
// Later ...
Button otherButton = new Button(this);
// You can still reference the precious object and u know that mikesButton is the one you loaded from the xml layout

उदाहरण #2

द्वारा Geert Berkers 2👍
04-23 08:00:07.524: E/AndroidRuntime(1384): Caused by: java.lang.NullPointerException
    04-23 08:00:07.524: E/AndroidRuntime(1384):     at com.datumdroid.android.ocr.simple.SimpleAndroidOCRActivity.onCreate(SimpleAndroidOCRActivity.java:68)
    04-23 08:00:07.524: E/AndroidRuntime(1384):     at android.app.Activity.performCreate(Activity.java:5104)
    04-23 08:00:07.524: E/AndroidRuntime(1384):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    04-23 08:00:07.524: E/AndroidRuntime(1384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-08 14:36:18.350 28294-28294/geert.berkers.bridgetimer E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
10-08 14:36:18.350 28294-28294/geert.berkers.bridgetimer E/AndroidRuntime:     at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
10-08 14:36:18.350 28294-28294/geert.berkers.bridgetimer E/AndroidRuntime:     at android.view.ViewGroup.addView(ViewGroup.java:3208)
10-08 14:36:18.350 28294-28294/geert.berkers.bridgetimer E/AndroidRuntime:     at android.view.ViewGroup.addView(ViewGroup.java:3188)
10-08 14:36:18.350 28294-28294/geert.berkers.bridgetimer E/AndroidRuntime:     at geert.berkers.bridgetimer.MainActivity.onCreate(MainActivity.java:103)
10-08 14:36:18.350 28294-28294/geert.berkers.bridgetimer E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:4465)
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements   GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener {

    private Button mikesButton;
    private TextView mikesText;
    private GestureDetectorCompat gestureDetector;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ////////// Layout object is added //////////////////
        //RelativeLayout myLayout = new RelativeLayout(this);


        ////////// Button object being created /////////////////
        mikesButton = (Button) findViewById(R.id.mikesButton); /// ref to the location by id
        mikesButton.setText("Click Me");
        mikesButton.setBackgroundColor(Color.RED);
        //mikesButton.setId(1);


        ///////// Set up an event listener for the button //////////////

        mikesButton.setOnClickListener(  /// listen for an event on this button
                new Button.OnClickListener() {   //// Interface
                    public void onClick(View v) {    //// Callback method
                        TextView Change = (TextView) findViewById(R.id.mikesText);
                        Change.setText("Good job, it changed!");
                    }
                }
        );


        ///////////// Listening for multiple events ////////////////////
        mikesButton.setOnLongClickListener(
                new Button.OnLongClickListener() {   //// Interface
                    public boolean onLongClick(View v) {    //// Callback method
                        TextView Change = (TextView) findViewById(R.id.mikesText);
                        Change.setText("Good job, it changed when you held onto the button!");
                        return true;
                    }
                }
        );


        //////////// Set up an event listener for a double tap event //////////////
        mikesButton = (Button) findViewById(R.id.mikesButton); /////// Reference to the button
        this.gestureDetector = new GestureDetectorCompat(this, this); /// an object from GD class to detect gestures
        gestureDetector.setOnDoubleTapListener(this); /// Set method to detect double taps


        ///////// Input object called mikesText is added //////////////////////
        TextView mikesText = (TextView) findViewById(R.id.mikesText);
        //mikesText.setId(2);

/*
        RelativeLayout.LayoutParams textdeets = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );

        ///////// Give rules to position widgets ////////////////////////
        textdeets.addRule(RelativeLayout.ABOVE, mikesButton.getId()); /// Place above the button that is already centered//
        textdeets.addRule(RelativeLayout.CENTER_HORIZONTAL); //// place in the middle of the screen ////
        textdeets.setMargins(0, 0, 0, 50); ///// where to place; left, top, right, bottom ///////


        ////////////// Rules of where to place the layout object ///////////////
        RelativeLayout.LayoutParams buttonDeets = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );


        //////////// Rules of where to place the button object ////////////////
        buttonDeets.addRule(RelativeLayout.CENTER_HORIZONTAL);
        buttonDeets.addRule(RelativeLayout.CENTER_VERTICAL);


        ////////// Add widgets to layout and their rules///////////
        myLayout.addView(mikesButton, buttonDeets); //Second param shows android the rules of how it is layed out
        myLayout.addView(mikesText, textdeets);


        /////// Sets this activities content to this view & change background color //////////
        setContentView(myLayout);
        myLayout.setBackgroundColor(Color.BLUE);
*/

    }

////////////// implement gesture methods from GestureDetector class ///////////////////////

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        mikesButton.setText("onDoubleTap");  /// set message to change text on screen to whatever gesture just occured
        return true; // return true on all gesture methods so the system shows the event was handled
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        mikesButton.setText("onSingleTapConfirmed");  /// set message to change text on screen to whatever gesture just occured
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        mikesButton.setText("onDoubleTapEvent");
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        mikesButton.setText("onDown");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        mikesButton.setText("onSingleTapUp");
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        mikesButton.setText("onScroll");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        mikesButton.setText("onFling");
        return true;
    }


    ////////// Override default method so that system checks if the event is a gesture before checking for common event /////////////
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gestureDetector.onTouchEvent(event); /// add the gesture detector variable to check for
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:id="@+id/mikesButton"
        android:text="Button to press"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:text="Text to change"
        android:id="@+id/mikesText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mikesButton"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:textAppearanceLarge"/>
</RelativeLayout>
प्रश्न और उत्तर स्टैक ओवरफ़्लो से एकत्र किए जाते हैं, cc by-sa 2.5, cc by-sa 3.0 और cc by-sa 4.0 के तहत लाइसेंस प्राप्त होते हैं।

समान प्रश्न