문제

I also want to be able to change the image programmatically so that I can attach a listener and change the image at specific points. I'm at the early stages of this and I'm having trouble setting up the view I have;

//inside my activity

LinearLayout mLayout;

//then inside the oncreate

mLayout = (LinearLayout) findViewById(R.id.main1);
setContentView(mLayout);
mLayout.setBackgroundResource(R.drawable.pic);

This compiles fine in eclipse but when I run it the app fails to start.

I'm looking to create a slide show with music.

I have tried the solutions given below but I am still getting an error;

12-06 11:51:11.656: E/AndroidRuntime(238): Uncaught handler: thread main exiting due to uncaught exception

Ultimately I want a slide show that will allow me to set the background image programmatically so I'm trying to get a handle on a view so that I can set the background through a listener.

I have set up a view res/layout/slider.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main2"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <SeekBar android:id="@+id/seek"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <TextView android:id="@+id/progress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/tracking"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

SeekBar mSeekBar;
TextView mProgressText;
TextView mTrackingText;
LinearLayout mLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSeekBar = (SeekBar)findViewById(R.id.seek);
    mSeekBar.setOnSeekBarChangeListener(this);
    mProgressText = (TextView)findViewById(R.id.progress);
    mTrackingText = (TextView)findViewById(R.id.tracking);


    setContentView(R.layout.slider);
    mLayout = (LinearLayout) findViewById(R.id.main2);
    setContentView(mLayout);
    mLayout.setBackgroundResource(R.drawable.mumbai);

    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
            mProgressText.setText(progress + " " +
                    getString(R.string.seekbar_from_touch) + "=" + fromTouch);
            //mLayout = (LinearLayout) findViewById(R.id.main1);
            //mLayout.setBackgroundResource(R.drawable.boats);
    }

        public void onStartTrackingTouch(SeekBar seekBar) {
            mTrackingText.setText(getString(R.string.seekbar_tracking_on));
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            mTrackingText.setText(getString(R.string.seekbar_tracking_off));
        }
}

Hi Pratick as I wrote below I'm still getting an error.

12-06 13:02:34.074: E/AndroidRuntime(808): Uncaught handler: thread main exiting due to uncaught exception

Although I don't think it is very informative. This was after I commented out the second call to setContentView(mLayout).

I've just ran it through the debugger and its erred at mSeekBar.setOnSeekBarChangeListener(this);

I had no success with the suggestions and have tried a new tactic I'm now trying to use a LayoutInflater but I'm getting the same error on the AVD and when I step through the code Just as I think I'm about to leave the line that calls 'inflater.inflate' it gives the error "source not found" and brings up a window in the debugger code pane that allows me to navigate. I've created the 2 views in xml and received no errros when I saved

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PwdDialogActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutInflater inflater = (LayoutInflater) getSystemService
            (Context.LAYOUT_INFLATER_SERVICE);
        final View layout = inflater.inflate(R.layout.password_dialog,
                (ViewGroup)findViewById(R.id.root));
        //layout.setBackgroundResource(R.drawable.boats);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        builder.setTitle(R.string.app_name);
    }
}

I'm wondering if this error and the previous error has nothing to do with the code and is more concerned with my set up. But I have created the project and AVD to run in version 3. I've looked up LayoutInflater and it says it's been about since version 1.

도움이 되었습니까?

해결책

without setting any layout in setContentView() and find the object by it's ID it's always getting null.

You need to first set the layout and then get object id from that layout

setContentView(R.layout.yourlayout); // here set the layout where object or control where defined it
mLayout = (LinearLayout) findViewById(R.id.main1);
setContentView(mLayout); // and then you can set that object

다른 팁

Change

mLayout = (LinearLayout) findViewById(R.id.main1);
setContentView(mLayout);

to

setContentView(R.layout.yourLayoutFile);
mLayout = (LinearLayout) findViewById(R.id.main1);

Reason: findViewById works on the current content view. Before you have set a content view findViewById will always return null. You will therefore get a NullPointerException on the last line you posted.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top